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;
24 public class ComponentQuestionnaireHealer implements Healer {
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();
33 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
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";
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";
50 public ComponentQuestionnaireHealer(){
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();
65 if (questionnaire != null) {
66 JsonParser jsonParser = new JsonParser();
67 JsonObject json = (JsonObject) jsonParser.parse(questionnaire);
69 Collection<ComputeEntity> computeEntities = computeDao.list(new ComputeEntity(vspId,
70 version, componentEntity.getId(), null));
71 computeEntities.stream().forEach(
73 populateComputeQuestionnaire(json, computeEntity);
77 Collection<ImageEntity> imageEntities = imageDao.list(new ImageEntity(vspId,
78 version, componentEntity.getId(), null));
79 imageEntities.stream().forEach(
81 populateImageQuestionnaire(json, imageEntity);
85 processDiskAttribute(json, "bootDiskSizePerVM");
86 processDiskAttribute(json, "ephemeralDiskSizePerVM");
88 String questionnaireData = json.toString();
89 componentDao.updateQuestionnaireData(vspId, version, componentEntity.getId(),
93 return componentEntities;
97 * Move Disk Atributes from genral/image/ to genral/disk in component questionnaire itself
100 * @param diskJsonObject
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();
111 diskJsonObject.addProperty(diskAttrName, json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE)
112 .get(diskAttrName).getAsNumber());
114 json.getAsJsonObject(GENERAL).add(DISK, diskJsonObject);
115 json.getAsJsonObject(GENERAL).getAsJsonObject(IMAGE).remove(diskAttrName);
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)
127 * Move the required attributes from component to Image Questionnaire
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;
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);
149 * Move the required attributes from component to Compute Questionnaire
151 * @param computeEntity
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);
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);
170 JsonElement memoryRam = numberOfVms.get(MEMORY_RAM);
171 if (memoryRam != null ) {
172 vmSizing.addProperty(COMPUTE_MEMORY_RAM, memoryRam.getAsString());
173 numberOfVms.remove(MEMORY_RAM);
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);
187 private JsonObject getJsonObject(JsonObject json, String name) {
188 return json.getAsJsonObject(name);