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.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;
21 import java.util.Collection;
23 import java.util.Objects;
26 public class ComponentQuestionnaireHealer implements Healer {
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;
46 public ComponentQuestionnaireHealer() {
47 this.componentDao = ComponentDaoFactory.getInstance().createInterface();
48 this.computeDao = ComputeDaoFactory.getInstance().createInterface();
49 this.imageDao = ImageDaoFactory.getInstance().createInterface();
52 public ComponentQuestionnaireHealer(ComponentDao componentDao,
53 ComputeDao computeDao, ImageDao imageDao) {
54 this.componentDao = componentDao;
55 this.computeDao = computeDao;
56 this.imageDao = imageDao;
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();
73 if (StringUtils.isNotBlank(questionnaire)) {
74 JsonParser jsonParser = new JsonParser();
75 JsonObject json = (JsonObject) jsonParser.parse(questionnaire);
77 Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
78 version, componentEntity.getId(), null));
79 populateComputeQuestionnaire(json, computeEntities);
81 Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
82 version, componentEntity.getId(), null));
83 populateImageQuestionnaire(json, imageEntities);
85 processDiskAttribute(json, "bootDiskSizePerVM");
86 processDiskAttribute(json, "ephemeralDiskSizePerVM");
88 String questionnaireData = json.toString();
89 componentEntity.setQuestionnaireData(questionnaireData); //Added to validate data in Junit
91 componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
95 return componentEntities;
99 * Move Disk Atributes from genral/image/ to genral/disk in component questionnaire itself
101 * @param json Component Json
102 * @param diskAttrName Name of disk attribute
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();
112 diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
113 .get(diskAttrName).getAsNumber());
115 json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
116 json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
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)
128 * Move the required attributes from component to Image Questionnaire
130 * @param json Component Json
131 * @param imageEntities All images present in component
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;
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);
151 private void populateComputeQuestionnaire(JsonObject json, Collection<ComputeEntity>
153 JsonObject compute = getJsonObject(json, COMPUTE);
154 if (compute != null) {
155 JsonObject vmSizing = handleVmSizing(compute);
156 vmSizing = handleNumOfVm(compute, vmSizing);
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));
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);
178 compute.remove(VM_SIZING);
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();
191 if (cpuRatio != null) {
192 vmSizing.addProperty(COMPUTE_CPU_OVER_SUBSCRIPTION_RATIO, cpuRatio.getAsString());
193 numberOfVms.remove(CPU_OVER_SUBSCRIPTION_RATIO);
195 if (memoryRam != null) {
196 vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
197 numberOfVms.remove(MEMORY_RAM);
203 private JsonObject getJsonObject(JsonObject json, String name) {
204 return json.getAsJsonObject(name);