aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java
blob: 2e63a8d50a00a276c4111e78caa508291feb710b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package org.openecomp.sdc.healing.healers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.healing.interfaces.Healer;
import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.util.Collection;
import java.util.Map;


public class ComponentQuestionnaireHealer implements Healer {

  private static final ComponentDao componentDao =
      ComponentDaoFactory.getInstance().createInterface();
  private static final ComputeDao computeDao =
      ComputeDaoFactory.getInstance().createInterface();
  private static final ImageDao imageDao =
      ImageDaoFactory.getInstance().createInterface();

  private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();

  public static final String GENERAL = "general";
  public static final String IMAGE = "image";
  public static final String FORMAT = "format";
  public static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
  public static final String MEMORY_RAM = "MemoryRAM";
  public static final String VM_SIZING = "vmSizing";
  public static final String COMPUTE = "compute";
  public static final String NUM_OF_VMS = "numOfVMs";
  public static final String DISK = "disk";
  public static final String IO_OP_PER_SEC = "IOOperationsPerSec";

  public static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio";
  public static final String COMPUTE_MEMORY_RAM = "memoryRAM";
  public static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec";

  public ComponentQuestionnaireHealer(){

  }
  @Override
  public Object heal(Map<String, Object> healingParams) throws Exception {
    mdcDataDebugMessage.debugEntryMessage(null, null);
    String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
    Version version = (Version) healingParams.get(SdcCommon.VERSION);
    String user = (String) healingParams.get(SdcCommon.USER);
    Collection<ComponentEntity> componentEntities =
        componentDao.list(new ComponentEntity(vspId, version, null));
    componentEntities.forEach(componentEntity -> {
      String questionnaire = componentDao.getQuestionnaireData(vspId, version, componentEntity
          .getId()).getQuestionnaireData();

      if (questionnaire != null) {
        JsonParser jsonParser = new JsonParser();
        JsonObject  json = (JsonObject) jsonParser.parse(questionnaire);

        Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
            version, componentEntity.getId(), null));
        computeEntities.stream().forEach(
            computeEntity -> {
              populateComputeQuestionnaire(json, computeEntity);
            }
        );

        Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
            version, componentEntity.getId(), null));
        imageEntities.stream().forEach(
            imageEntity -> {
              populateImageQuestionnaire(json, imageEntity);
            }
        );

        processDiskAttribute(json, "bootDiskSizePerVM");
        processDiskAttribute(json, "ephemeralDiskSizePerVM");

        String questionnaireData = json.toString();
        componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
            questionnaireData);
      }
    });
    return componentEntities;
  }

  /**
   * Move Disk Atributes from genral/image/  to genral/disk in component questionnaire itself
   * @param json
   * @param diskAttrName
   * @param diskJsonObject
   * @return
   */
  private void processDiskAttribute(JsonObject json, String diskAttrName) {
    boolean isBootDisksizePerVM = isDiskAttributePresent(json, diskAttrName);
    if (isBootDisksizePerVM) {
      JsonObject diskJsonObject = json.getAsJsonObject(GENERAL).getAsJsonObject(DISK);
      if (diskJsonObject == null) {
        diskJsonObject = new JsonObject();
      }

      diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
          .get(diskAttrName).getAsNumber());

      json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
      json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
    }
  }

  private boolean isDiskAttributePresent(JsonObject json, String diskAttrName) {
    return json.getAsJsonObject(GENERAL) != null &&
        json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) != null &&
        json.getAsJsonObject(GENERAL).getAsJsonObject (IMAGE).get(diskAttrName)
            != null;
  }

  /**
   * Move the required attributes from component to Image Questionnaire
   * @param json
   * @param imageEntity
   */
  private void populateImageQuestionnaire(JsonObject json, ImageEntity imageEntity) {
    JsonObject general = getJsonObject(json, GENERAL);
    boolean isImageFormat = general != null && json
        .getAsJsonObject(GENERAL)
        .getAsJsonObject(IMAGE) != null && json.getAsJsonObject(GENERAL).getAsJsonObject
        (IMAGE).get(FORMAT) != null;
    if (isImageFormat) {
      JsonObject image = getJsonObject(general, IMAGE);
      JsonElement jsonElement = image.get(FORMAT);
      JsonObject jsonObject = new JsonObject();
      jsonObject.add(FORMAT, jsonElement);
      imageDao.updateQuestionnaireData(imageEntity.getVspId(), imageEntity.getVersion(), imageEntity
          .getComponentId(),imageEntity.getId(), jsonObject.toString());
      image.remove(FORMAT);
    }
  }

  /**
   * Move the required attributes from component to Compute Questionnaire
   * @param json
   * @param computeEntity
   */
  private void populateComputeQuestionnaire(JsonObject json, ComputeEntity computeEntity) {
    JsonObject compute = getJsonObject(json, COMPUTE);
    JsonObject vmSizing = getJsonObject(compute, VM_SIZING);
    if (compute != null && vmSizing != null) {
      JsonElement ioOperationsPerSec = vmSizing.get(IO_OP_PER_SEC);
      if (ioOperationsPerSec != null) {
        vmSizing.addProperty(COMPUTE_IO_OP_PER_SEC, ioOperationsPerSec.getAsNumber());
        vmSizing.remove(IO_OP_PER_SEC);
      }

      JsonObject numberOfVms = getJsonObject(compute, NUM_OF_VMS);
      if (numberOfVms != null ) {
        JsonElement cpuRatio =  numberOfVms.get(CPU_OVER_SUBSCRIPTION_RATIO);
        if (cpuRatio != null ) {
          vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString());
          numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO);
        }
        JsonElement memoryRam =  numberOfVms.get(MEMORY_RAM);
        if (memoryRam != null ) {
          vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
          numberOfVms.remove(MEMORY_RAM);
        }
      }

      JsonObject computeQuestionnaireJsonObject = new JsonObject();
      computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing);
      String computeQuestionnaire = computeQuestionnaireJsonObject != null ?
          computeQuestionnaireJsonObject.toString() : null;
      computeDao.updateQuestionnaireData(computeEntity.getVspId(), computeEntity.getVersion(),
          computeEntity.getComponentId(), computeEntity.getId(), computeQuestionnaire);
      compute.remove(VM_SIZING);

    }
  }

  private JsonObject getJsonObject(JsonObject json, String name) {
    return json.getAsJsonObject(name);
  }
}