2e63a8d50a00a276c4111e78caa508291feb710b
[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.openecomp.sdc.common.utils.SdcCommon;
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.VendorSoftwareProductDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
20 import org.openecomp.sdc.versioning.dao.types.Version;
21
22 import java.util.Collection;
23 import java.util.Map;
24
25
26 public class ComponentQuestionnaireHealer implements Healer {
27
28   private static final ComponentDao componentDao =
29       ComponentDaoFactory.getInstance().createInterface();
30   private static final ComputeDao computeDao =
31       ComputeDaoFactory.getInstance().createInterface();
32   private static final ImageDao imageDao =
33       ImageDaoFactory.getInstance().createInterface();
34
35   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
36
37   public static final String GENERAL = "general";
38   public static final String IMAGE = "image";
39   public static final String FORMAT = "format";
40   public static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
41   public static final String MEMORY_RAM = "MemoryRAM";
42   public static final String VM_SIZING = "vmSizing";
43   public static final String COMPUTE = "compute";
44   public static final String NUM_OF_VMS = "numOfVMs";
45   public static final String DISK = "disk";
46   public static final String IO_OP_PER_SEC = "IOOperationsPerSec";
47
48   public static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio";
49   public static final String COMPUTE_MEMORY_RAM = "memoryRAM";
50   public static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec";
51
52   public ComponentQuestionnaireHealer(){
53
54   }
55   @Override
56   public Object heal(Map<String, Object> healingParams) throws Exception {
57     mdcDataDebugMessage.debugEntryMessage(null, null);
58     String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
59     Version version = (Version) healingParams.get(SdcCommon.VERSION);
60     String user = (String) healingParams.get(SdcCommon.USER);
61     Collection<ComponentEntity> componentEntities =
62         componentDao.list(new ComponentEntity(vspId, version, null));
63     componentEntities.forEach(componentEntity -> {
64       String questionnaire = componentDao.getQuestionnaireData(vspId, version, componentEntity
65           .getId()).getQuestionnaireData();
66
67       if (questionnaire != null) {
68         JsonParser jsonParser = new JsonParser();
69         JsonObject  json = (JsonObject) jsonParser.parse(questionnaire);
70
71         Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
72             version, componentEntity.getId(), null));
73         computeEntities.stream().forEach(
74             computeEntity -> {
75               populateComputeQuestionnaire(json, computeEntity);
76             }
77         );
78
79         Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
80             version, componentEntity.getId(), null));
81         imageEntities.stream().forEach(
82             imageEntity -> {
83               populateImageQuestionnaire(json, imageEntity);
84             }
85         );
86
87         processDiskAttribute(json, "bootDiskSizePerVM");
88         processDiskAttribute(json, "ephemeralDiskSizePerVM");
89
90         String questionnaireData = json.toString();
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    * @param json
101    * @param diskAttrName
102    * @param diskJsonObject
103    * @return
104    */
105   private void processDiskAttribute(JsonObject json, String diskAttrName) {
106     boolean isBootDisksizePerVM = isDiskAttributePresent(json, diskAttrName);
107     if (isBootDisksizePerVM) {
108       JsonObject diskJsonObject = json.getAsJsonObject(GENERAL).getAsJsonObject(DISK);
109       if (diskJsonObject == null) {
110         diskJsonObject = new JsonObject();
111       }
112
113       diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
114           .get(diskAttrName).getAsNumber());
115
116       json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
117       json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
118     }
119   }
120
121   private boolean isDiskAttributePresent(JsonObject json, String diskAttrName) {
122     return json.getAsJsonObject(GENERAL) != null &&
123         json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE) != null &&
124         json.getAsJsonObject(GENERAL).getAsJsonObject (IMAGE).get(diskAttrName)
125             != null;
126   }
127
128   /**
129    * Move the required attributes from component to Image Questionnaire
130    * @param json
131    * @param imageEntity
132    */
133   private void populateImageQuestionnaire(JsonObject json, ImageEntity imageEntity) {
134     JsonObject general = getJsonObject(json, GENERAL);
135     boolean isImageFormat = general != null && json
136         .getAsJsonObject(GENERAL)
137         .getAsJsonObject(IMAGE) != null && json.getAsJsonObject(GENERAL).getAsJsonObject
138         (IMAGE).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       imageDao.updateQuestionnaireData(imageEntity.getVspId(), imageEntity.getVersion(), imageEntity
145           .getComponentId(),imageEntity.getId(), jsonObject.toString());
146       image.remove(FORMAT);
147     }
148   }
149
150   /**
151    * Move the required attributes from component to Compute Questionnaire
152    * @param json
153    * @param computeEntity
154    */
155   private void populateComputeQuestionnaire(JsonObject json, ComputeEntity computeEntity) {
156     JsonObject compute = getJsonObject(json, COMPUTE);
157     JsonObject vmSizing = getJsonObject(compute, VM_SIZING);
158     if (compute != null && vmSizing != null) {
159       JsonElement ioOperationsPerSec = vmSizing.get(IO_OP_PER_SEC);
160       if (ioOperationsPerSec != null) {
161         vmSizing.addProperty(COMPUTE_IO_OP_PER_SEC, ioOperationsPerSec.getAsNumber());
162         vmSizing.remove(IO_OP_PER_SEC);
163       }
164
165       JsonObject numberOfVms = getJsonObject(compute, NUM_OF_VMS);
166       if (numberOfVms != null ) {
167         JsonElement cpuRatio =  numberOfVms.get(CPU_OVER_SUBSCRIPTION_RATIO);
168         if (cpuRatio != null ) {
169           vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString());
170           numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO);
171         }
172         JsonElement memoryRam =  numberOfVms.get(MEMORY_RAM);
173         if (memoryRam != null ) {
174           vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
175           numberOfVms.remove(MEMORY_RAM);
176         }
177       }
178
179       JsonObject computeQuestionnaireJsonObject = new JsonObject();
180       computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing);
181       String computeQuestionnaire = computeQuestionnaireJsonObject != null ?
182           computeQuestionnaireJsonObject.toString() : null;
183       computeDao.updateQuestionnaireData(computeEntity.getVspId(), computeEntity.getVersion(),
184           computeEntity.getComponentId(), computeEntity.getId(), computeQuestionnaire);
185       compute.remove(VM_SIZING);
186
187     }
188   }
189
190   private JsonObject getJsonObject(JsonObject json, String name) {
191     return json.getAsJsonObject(name);
192   }
193 }