50e76781b358b99f25e03fd10a6485e036bfebbe
[sdc.git] /
1 package org.openecomp.sdc.healing.healers;
2
3 import com.google.gson.JsonElement;
4 import com.google.gson.JsonObject;
5 import com.google.gson.JsonParser;
6 import org.apache.commons.lang3.StringUtils;
7 import org.openecomp.sdc.common.utils.SdcCommon;
8 import org.openecomp.sdc.healing.interfaces.Healer;
9 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDaoFactory;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDaoFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
19 import org.openecomp.sdc.versioning.dao.types.Version;
20
21 import java.util.Collection;
22 import java.util.Map;
23 import java.util.Objects;
24
25
26 public class ComponentQuestionnaireHealer implements Healer {
27
28   private static final String GENERAL = "general";
29   private static final String IMAGE = "image";
30   private static final String FORMAT = "format";
31   private static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
32   private static final String MEMORY_RAM = "MemoryRAM";
33   private static final String VM_SIZING = "vmSizing";
34   private static final String COMPUTE = "compute";
35   private static final String NUM_OF_VMS = "numOfVMs";
36   private static final String DISK = "disk";
37   private static final String IO_OP_PER_SEC = "IOOperationsPerSec";
38   private static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio";
39   private static final String COMPUTE_MEMORY_RAM = "memoryRAM";
40   private static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec";
41   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
42   private final ComponentDao componentDao;
43   private final ComputeDao computeDao;
44   private final ImageDao imageDao;
45
46   public ComponentQuestionnaireHealer() {
47     this.componentDao = ComponentDaoFactory.getInstance().createInterface();
48     this.computeDao = ComputeDaoFactory.getInstance().createInterface();
49     this.imageDao = ImageDaoFactory.getInstance().createInterface();
50   }
51
52   public ComponentQuestionnaireHealer(ComponentDao componentDao,
53                                       ComputeDao computeDao, ImageDao imageDao) {
54     this.componentDao = componentDao;
55     this.computeDao = computeDao;
56     this.imageDao = imageDao;
57   }
58
59   @Override
60   public Object heal(Map<String, Object> healingParams) throws Exception {
61     mdcDataDebugMessage.debugEntryMessage("VSP ID",
62         (String) healingParams.get(SdcCommon.VSP_ID));
63     String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
64     Version version = (Version) healingParams.get(SdcCommon.VERSION);
65     Collection<ComponentEntity> componentEntities =
66         componentDao.list(new ComponentEntity(vspId, version, null));
67     componentEntities.forEach(componentEntity -> {
68       ComponentEntity componentQuestionnaireData =
69           componentDao.getQuestionnaireData(vspId, version, componentEntity.getId());
70       String questionnaire = Objects.isNull(componentQuestionnaireData) ? null
71           : componentQuestionnaireData.getQuestionnaireData();
72
73       if (StringUtils.isNotBlank(questionnaire)) {
74         JsonParser jsonParser = new JsonParser();
75         JsonObject json = (JsonObject) jsonParser.parse(questionnaire);
76
77         Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
78             version, componentEntity.getId(), null));
79         populateComputeQuestionnaire(json, computeEntities);
80
81         Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
82             version, componentEntity.getId(), null));
83         populateImageQuestionnaire(json, imageEntities);
84
85         processDiskAttribute(json, "bootDiskSizePerVM");
86         processDiskAttribute(json, "ephemeralDiskSizePerVM");
87
88         String questionnaireData = json.toString();
89         componentEntity.setQuestionnaireData(questionnaireData); //Added to validate data in Junit
90
91         componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
92             questionnaireData);
93       }
94     });
95     return componentEntities;
96   }
97
98   /**
99    * Move Disk Atributes from genral/image/  to genral/disk in component questionnaire itself
100    *
101    * @param json Component Json
102    * @param diskAttrName Name of disk attribute
103    */
104   private void processDiskAttribute(JsonObject json, String diskAttrName) {
105     boolean isBootDisksizePerVM = isDiskAttributePresent(json, diskAttrName);
106     if (isBootDisksizePerVM) {
107       JsonObject diskJsonObject = json.getAsJsonObject(GENERAL).getAsJsonObject(DISK);
108       if (diskJsonObject == null) {
109         diskJsonObject = new JsonObject();
110       }
111
112       diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
113           .get(diskAttrName).getAsNumber());
114
115       json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
116       json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
117     }
118   }
119
120   private boolean isDiskAttributePresent(JsonObject json, String diskAttrName) {
121     return json.getAsJsonObject(GENERAL) != null
122         && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) != null
123         && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).get(diskAttrName)
124             != null;
125   }
126
127   /**
128    * Move the required attributes from component to Image Questionnaire
129    *
130    * @param json Component Json
131    * @param imageEntities All images present in component
132    */
133   private void populateImageQuestionnaire(JsonObject json, Collection<ImageEntity> imageEntities) {
134     JsonObject general = getJsonObject(json, GENERAL);
135     boolean isImageFormat = general != null && json
136         .getAsJsonObject(GENERAL)
137         .getAsJsonObject(IMAGE) != null && json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
138         .get(FORMAT) != null;
139     if (isImageFormat) {
140       JsonObject image = getJsonObject(general, IMAGE);
141       JsonElement jsonElement = image.get(FORMAT);
142       JsonObject jsonObject = new JsonObject();
143       jsonObject.add(FORMAT, jsonElement);
144       imageEntities.forEach(imageEntity -> imageDao.updateQuestionnaireData(imageEntity.getVspId(),
145           imageEntity.getVersion(), imageEntity.getComponentId(),
146           imageEntity.getId(), jsonObject.toString()));
147       image.remove(FORMAT);
148     }
149   }
150
151   private void populateComputeQuestionnaire(JsonObject json, Collection<ComputeEntity>
152       computeEntities) {
153     JsonObject compute = getJsonObject(json, COMPUTE);
154     if (compute != null) {
155       JsonObject vmSizing = handleVmSizing(compute);
156       vmSizing = handleNumOfVm(compute, vmSizing);
157
158       if (vmSizing != null) {
159         JsonObject computeQuestionnaireJsonObject = new JsonObject();
160         computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing);
161         String computeQuestionnaire = computeQuestionnaireJsonObject.toString();
162         computeEntities.forEach(
163             computeEntity -> computeDao.updateQuestionnaireData(computeEntity.getVspId(),
164                 computeEntity.getVersion(), computeEntity.getComponentId(),
165                 computeEntity.getId(), computeQuestionnaire));
166       }
167     }
168   }
169
170   private JsonObject handleVmSizing(JsonObject compute) {
171     JsonObject vmSizing = getJsonObject(compute, VM_SIZING);
172     if (vmSizing != null) {
173       JsonElement ioOperationsPerSec = vmSizing.get(IO_OP_PER_SEC);
174       if (ioOperationsPerSec != null) {
175         vmSizing.addProperty(COMPUTE_IO_OP_PER_SEC, ioOperationsPerSec.getAsNumber());
176         vmSizing.remove(IO_OP_PER_SEC);
177       }
178       compute.remove(VM_SIZING);
179     }
180     return vmSizing;
181   }
182
183   private JsonObject handleNumOfVm(JsonObject compute, JsonObject vmSizing) {
184     JsonObject numberOfVms = getJsonObject(compute, NUM_OF_VMS);
185     if (numberOfVms != null) {
186       JsonElement cpuRatio = numberOfVms.get(CPU_OVER_SUBSCRIPTION_RATIO);
187       JsonElement memoryRam = numberOfVms.get(MEMORY_RAM);
188       if (vmSizing == null && (cpuRatio != null || memoryRam != null)) {
189         vmSizing = new JsonObject();
190       }
191       if (cpuRatio != null) {
192         vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString());
193         numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO);
194       }
195       if (memoryRam != null) {
196         vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
197         numberOfVms.remove(MEMORY_RAM);
198       }
199     }
200     return vmSizing;
201   }
202
203   private JsonObject getJsonObject(JsonObject json, String name) {
204     return json.getAsJsonObject(name);
205   }
206 }