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