1 package org.openecomp.sdc.healing.healers;
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;
20 import java.util.Collection;
22 import java.util.Objects;
25 public class ComponentQuestionnaireHealer implements Healer {
27 private static final ComponentDao componentDao =
28 ComponentDaoFactory.getInstance().createInterface();
29 private static final ComputeDao computeDao =
30 ComputeDaoFactory.getInstance().createInterface();
31 private static final ImageDao imageDao =
32 ImageDaoFactory.getInstance().createInterface();
34 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
36 public static final String GENERAL = "general";
37 public static final String IMAGE = "image";
38 public static final String FORMAT = "format";
39 public static final String CPU_OVER_SUBSCRIPTION_RATIO = "CpuOverSubscriptionRatio";
40 public static final String MEMORY_RAM = "MemoryRAM";
41 public static final String VM_SIZING = "vmSizing";
42 public static final String COMPUTE = "compute";
43 public static final String NUM_OF_VMS = "numOfVMs";
44 public static final String DISK = "disk";
45 public static final String IO_OP_PER_SEC = "IOOperationsPerSec";
47 public static final String COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO = "cpuOverSubscriptionRatio";
48 public static final String COMPUTE_MEMORY_RAM = "memoryRAM";
49 public static final String COMPUTE_IO_OP_PER_SEC = "ioOperationsPerSec";
51 public ComponentQuestionnaireHealer(){
55 public Object heal(Map<String, Object> healingParams) throws Exception {
56 mdcDataDebugMessage.debugEntryMessage(null, null);
57 String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
58 Version version = (Version) healingParams.get(SdcCommon.VERSION);
59 String user = (String) healingParams.get(SdcCommon.USER);
60 Collection<ComponentEntity> componentEntities =
61 componentDao.list(new ComponentEntity(vspId, version, null));
62 componentEntities.forEach(componentEntity -> {
63 ComponentEntity componentQuestionnaireData =
64 componentDao.getQuestionnaireData(vspId, version, componentEntity.getId());
65 String questionnaire = Objects.isNull(componentQuestionnaireData) ? null
66 : componentQuestionnaireData.getQuestionnaireData();
68 if (questionnaire != null) {
69 JsonParser jsonParser = new JsonParser();
70 JsonObject json = (JsonObject) jsonParser.parse(questionnaire);
72 Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
73 version, componentEntity.getId(), null));
74 computeEntities.stream().forEach(
76 populateComputeQuestionnaire(json, computeEntity);
80 Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
81 version, componentEntity.getId(), null));
82 imageEntities.stream().forEach(
84 populateImageQuestionnaire(json, imageEntity);
88 processDiskAttribute(json, "bootDiskSizePerVM");
89 processDiskAttribute(json, "ephemeralDiskSizePerVM");
91 String questionnaireData = json.toString();
92 componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
96 return componentEntities;
100 * Move Disk Atributes from genral/image/ to genral/disk in component questionnaire itself
102 * @param diskAttrName
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();
113 diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
114 .get(diskAttrName).getAsNumber());
116 json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
117 json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
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)
129 * Move the required attributes from component to Image Questionnaire
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;
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);
151 * Move the required attributes from component to Compute Questionnaire
153 * @param computeEntity
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);
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);
172 JsonElement memoryRam = numberOfVms.get(MEMORY_RAM);
173 if (memoryRam != null ) {
174 vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
175 numberOfVms.remove(MEMORY_RAM);
179 JsonObject computeQuestionnaireJsonObject = new JsonObject();
180 computeQuestionnaireJsonObject.add(VM_SIZING, vmSizing);
181 String computeQuestionnaire = computeQuestionnaireJsonObject.toString();
182 computeDao.updateQuestionnaireData(computeEntity.getVspId(), computeEntity.getVersion(),
183 computeEntity.getComponentId(), computeEntity.getId(), computeQuestionnaire);
184 compute.remove(VM_SIZING);
189 private JsonObject getJsonObject(JsonObject json, String name) {
190 return json.getAsJsonObject(name);