summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentQuestionnaireHealer.java
blob: 83395e015a91c7a59a5edca03d2c02177d540f27 (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
194
195
196
197
198
199
200
201
202
package org.openecomp.sdc.healing.healers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringUtils;
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.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.Objects;


public class ComponentQuestionnaireHealer implements Healer {

  private static final String GENERAL = "general";
  private static final String IMAGE = "image";
  private static final String FORMAT = "format";
  private static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
  private static final String MEMORY_RAM = "MemoryRAM";
  private static final String VM_SIZING = "vmSizing";
  private static final String COMPUTE = "compute";
  private static final String NUM_OF_VMS = "numOfVMs";
  private static final String DISK = "disk";
  private static final String IO_OP_PER_SEC = "IOOperationsPerSec";
  private static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio";
  private static final String COMPUTE_MEMORY_RAM = "memoryRAM";
  private static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec";
  private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
  private final ComponentDao componentDao;
  private final ComputeDao computeDao;
  private final ImageDao imageDao;

  public ComponentQuestionnaireHealer() {
    this.componentDao = ComponentDaoFactory.getInstance().createInterface();
    this.computeDao = ComputeDaoFactory.getInstance().createInterface();
    this.imageDao = ImageDaoFactory.getInstance().createInterface();
  }

  public ComponentQuestionnaireHealer(ComponentDao componentDao,
                                      ComputeDao computeDao, ImageDao imageDao) {
    this.componentDao = componentDao;
    this.computeDao = computeDao;
    this.imageDao = imageDao;
  }

  @Override
  public Object heal(String vspId, Version version) throws Exception {
    mdcDataDebugMessage.debugEntryMessage("VSP ID", vspId);

    Collection<ComponentEntity> componentEntities =
        componentDao.list(new ComponentEntity(vspId, version, null));
    componentEntities.forEach(componentEntity -> {
      ComponentEntity componentQuestionnaireData =
          componentDao.getQuestionnaireData(vspId, version, componentEntity.getId());
      String questionnaire = Objects.isNull(componentQuestionnaireData) ? null
          : componentQuestionnaireData.getQuestionnaireData();

      if (StringUtils.isNotBlank(questionnaire)) {
        JsonParser jsonParser = new JsonParser();
        JsonObject json = (JsonObject) jsonParser.parse(questionnaire);

        Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
            version, componentEntity.getId(), null));
        populateComputeQuestionnaire(json, computeEntities);

        Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
            version, componentEntity.getId(), null));
        populateImageQuestionnaire(json, imageEntities);

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

        String questionnaireData = json.toString();
        componentEntity.setQuestionnaireData(questionnaireData); //Added to validate data in Junit

        componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
            questionnaireData);
      }
    });
    return componentEntities;
  }

  /**
   * Move Disk Atributes from genral/image/  to genral/disk in component questionnaire itself
   *
   * @param json Component Json
   * @param diskAttrName Name of disk attribute
   */
  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 Component Json
   * @param imageEntities All images present in component
   */
  private void populateImageQuestionnaire(JsonObject json, Collection<ImageEntity> imageEntities) {
    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);
      imageEntities.forEach(imageEntity -> imageDao.updateQuestionnaireData(imageEntity.getVspId(),
          imageEntity.getVersion(), imageEntity.getComponentId(),
          imageEntity.getId(), jsonObject.toString()));
      image.remove(FORMAT);
    }
  }

  private void populateComputeQuestionnaire(JsonObject json, Collection<ComputeEntity>
      computeEntities) {
    JsonObject compute = getJsonObject(json, COMPUTE);
    if (compute != null) {
      JsonObject vmSizing = handleVmSizing(compute);
      vmSizing = handleNumOfVm(compute, vmSizing);

      if (vmSizing != null) {
        JsonObject computeQuestionnaireJsonObject = new JsonObject();
        computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing);
        String computeQuestionnaire = computeQuestionnaireJsonObject.toString();
        computeEntities.forEach(
            computeEntity -> computeDao.updateQuestionnaireData(computeEntity.getVspId(),
                computeEntity.getVersion(), computeEntity.getComponentId(),
                computeEntity.getId(), computeQuestionnaire));
      }
    }
  }

  private JsonObject handleVmSizing(JsonObject compute) {
    JsonObject vmSizing = getJsonObject(compute, VM_SIZING);
    if (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);
      }
      compute.remove(VM_SIZING);
    }
    return vmSizing;
  }

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

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