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