X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Floop%2Fcomponents%2Fexternal%2FDcaeComponent.java;h=8fce5cafd8b6f3a75650cab9a79390ce98ad9e94;hb=4de90de028ffefb93aa67d364edffa5edd58d33c;hp=5d62e715118ff5034ceddf16aa41a840d95dfd42;hpb=9e01ce3b97e602fa7236bd9bc8a484807382f83b;p=clamp.git diff --git a/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java index 5d62e715..8fce5caf 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/DcaeComponent.java @@ -25,9 +25,17 @@ package org.onap.clamp.loop.components.external; import com.google.gson.JsonObject; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.UUID; import org.apache.camel.Exchange; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse; import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.Loop; @@ -39,12 +47,15 @@ public class DcaeComponent extends ExternalComponent { private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters"; private static final String DCAE_SERVICETYPE_ID = "serviceTypeId"; private static final String DCAE_INPUTS = "inputs"; + private static final String SINGLE_BLUEPRINT_POLICYID = "loop template blueprint"; + + private String name; public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED", "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"); public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState( "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices " - + "defined in the DCAE blueprint and it's currently processing the request"); + + "defined in the DCAE blueprint and it's currently processing the request"); public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState( "MICROSERVICE_INSTALLATION_FAILED", "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed"); @@ -53,7 +64,7 @@ public class DcaeComponent extends ExternalComponent { "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully"); public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState( "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices " - + "defined in the DCAE blueprint and it's currently processing the request"); + + "defined in the DCAE blueprint and it's currently processing the request"); public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState( "MICROSERVICE_UNINSTALLATION_FAILED", "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed"); @@ -65,16 +76,23 @@ public class DcaeComponent extends ExternalComponent { public DcaeComponent() { super(BLUEPRINT_DEPLOYED); + this.name = "DCAE"; + } + + public DcaeComponent(String name) { + super(BLUEPRINT_DEPLOYED); + this.name = "DCAE_" + name; } @Override public String getComponentName() { - return "DCAE"; + return name; } + /** * Convert the json response to a DcaeOperationStatusResponse. - * + * * @param responseBody The DCAE response Json paylaod * @return The dcae object provisioned */ @@ -85,6 +103,7 @@ public class DcaeComponent extends ExternalComponent { return null; } } + /** * Generate the deployment id, it's random. * @@ -113,9 +132,9 @@ public class DcaeComponent extends ExternalComponent { */ public static String getDeployPayload(Loop loop) { JsonObject globalProp = loop.getGlobalPropertiesJson(); - JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER); + JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(SINGLE_BLUEPRINT_POLICYID); - String serviceTypeId = loop.getDcaeBlueprintId(); + String serviceTypeId = loop.getLoopTemplate().getDcaeBlueprintId(); JsonObject rootObject = new JsonObject(); rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId); @@ -128,15 +147,16 @@ public class DcaeComponent extends ExternalComponent { /** * Return the deploy payload for DCAE. * - * @param loop The loop object - * @param microServiceName The micro service name + * @param loop The loop object + * @param microServicePolicy The micro service policy * @return The payload used to send deploy closed loop request */ - public static String getDeployPayload(Loop loop, String microServiceName) { + public static String getDeployPayload(Loop loop, MicroServicePolicy microServicePolicy) { JsonObject globalProp = loop.getGlobalPropertiesJson(); - JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName); + JsonObject deploymentProp = + globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServicePolicy.getName()); - String serviceTypeId = loop.getDcaeBlueprintId(); + String serviceTypeId = microServicePolicy.getDcaeBlueprintId(); JsonObject rootObject = new JsonObject(); rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId); @@ -154,7 +174,19 @@ public class DcaeComponent extends ExternalComponent { */ public static String getUndeployPayload(Loop loop) { JsonObject rootObject = new JsonObject(); - rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId()); + rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getLoopTemplate().getDcaeBlueprintId()); + return rootObject.toString(); + } + + /** + * Return the uninstallation payload for DCAE. + * + * @param policy The microServicePolicy object + * @return The payload in string (json) + */ + public static String getUndeployPayload(MicroServicePolicy policy) { + JsonObject rootObject = new JsonObject(); + rootObject.addProperty(DCAE_SERVICETYPE_ID, policy.getDcaeBlueprintId()); return rootObject.toString(); } @@ -185,4 +217,26 @@ public class DcaeComponent extends ExternalComponent { } return this.getState(); } + + /** + * Convert the json response to a DcaeInventoryResponse. + * + * @param responseBody The DCAE response Json paylaod + * @return list of DcaeInventoryResponse + * @throws ParseException In case of issues with the Json parsing + */ + public static List convertToDcaeInventoryResponse(String responseBody) + throws ParseException { + JSONParser parser = new JSONParser(); + JSONObject jsonObj = (JSONObject) parser.parse(responseBody); + JSONArray itemsArray = (JSONArray) jsonObj.get("items"); + Iterator it = itemsArray.iterator(); + List inventoryResponseList = new LinkedList<>(); + while (it.hasNext()) { + JSONObject item = (JSONObject) it.next(); + DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class); + inventoryResponseList.add(response); + } + return inventoryResponseList; + } }