e22b425ac5934d840d7eb013d3d6b9f2d67e8a08
[clamp.git] / src / main / java / org / onap / clamp / loop / components / external / DcaeComponent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.components.external;
25
26 import com.google.gson.JsonObject;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.UUID;
31 import org.apache.camel.Exchange;
32 import org.json.simple.JSONArray;
33 import org.json.simple.JSONObject;
34 import org.json.simple.parser.JSONParser;
35 import org.json.simple.parser.ParseException;
36
37 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
38 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.Loop;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
42
43 public class DcaeComponent extends ExternalComponent {
44
45     private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
46     private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
47     private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
48     private static final String DCAE_INPUTS = "inputs";
49
50     public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
51             "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
52     public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
53             "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
54                     + "defined in the DCAE blueprint and it's currently processing the request");
55     public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
56             "MICROSERVICE_INSTALLATION_FAILED",
57             "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
58     public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
59             "MICROSERVICE_INSTALLED_SUCCESSFULLY",
60             "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
61     public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
62             "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
63                     + "defined in the DCAE blueprint and it's currently processing the request");
64     public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
65             "MICROSERVICE_UNINSTALLATION_FAILED",
66             "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
67     public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
68             "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
69             "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
70     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
71             "There was an error during the request done to DCAE, look at the logs or try again");
72
73     public DcaeComponent() {
74         super(BLUEPRINT_DEPLOYED);
75     }
76
77     @Override
78     public String getComponentName() {
79         return "DCAE";
80     }
81
82     /**
83      * Convert the json response to a DcaeOperationStatusResponse.
84      * 
85      * @param responseBody The DCAE response Json paylaod
86      * @return The dcae object provisioned
87      */
88     public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
89         if (responseBody != null && !responseBody.isEmpty()) {
90             return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
91         } else {
92             return null;
93         }
94     }
95     /**
96      * Generate the deployment id, it's random.
97      *
98      * @return The deployment id
99      */
100     public static String generateDeploymentId() {
101         return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
102     }
103
104     /**
105      * This method prepare the url returned by DCAE to check the status if fine. It
106      * extracts it from the dcaeResponse.
107      *
108      * @param dcaeResponse The dcae response object
109      * @return the Right Url modified if needed
110      */
111     public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
112         return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
113     }
114
115     /**
116      * Return the deploy payload for DCAE.
117      *
118      * @param loop The loop object
119      * @return The payload used to send deploy closed loop request
120      */
121     public static String getDeployPayload(Loop loop) {
122         JsonObject globalProp = loop.getGlobalPropertiesJson();
123         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
124
125         String serviceTypeId = loop.getDcaeBlueprintId();
126
127         JsonObject rootObject = new JsonObject();
128         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
129         if (deploymentProp != null) {
130             rootObject.add(DCAE_INPUTS, deploymentProp);
131         }
132         return rootObject.toString();
133     }
134
135     /**
136      * Return the deploy payload for DCAE.
137      *
138      * @param loop The loop object
139      * @param microServiceName The micro service name
140      * @return The payload used to send deploy closed loop request
141      */
142     public static String getDeployPayload(Loop loop, String microServiceName) {
143         JsonObject globalProp = loop.getGlobalPropertiesJson();
144         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName);
145
146         String serviceTypeId = loop.getDcaeBlueprintId();
147
148         JsonObject rootObject = new JsonObject();
149         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
150         if (deploymentProp != null) {
151             rootObject.add(DCAE_INPUTS, deploymentProp);
152         }
153         return rootObject.toString();
154     }
155
156     /**
157      * Return the uninstallation payload for DCAE.
158      *
159      * @param loop The loop object
160      * @return The payload in string (json)
161      */
162     public static String getUndeployPayload(Loop loop) {
163         JsonObject rootObject = new JsonObject();
164         rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
165         return rootObject.toString();
166     }
167
168     @Override
169     public ExternalComponentState computeState(Exchange camelExchange) {
170
171         DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
172                 .getProperty("dcaeResponse");
173
174         if (dcaeResponse == null) {
175             setState(BLUEPRINT_DEPLOYED);
176         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
177             setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
178         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
179             setState(PROCESSING_MICROSERVICE_INSTALLATION);
180         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
181             setState(MICROSERVICE_INSTALLATION_FAILED);
182         } else if (dcaeResponse.getOperationType().equals("uninstall")
183                 && dcaeResponse.getStatus().equals("succeeded")) {
184             setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
185         } else if (dcaeResponse.getOperationType().equals("uninstall")
186                 && dcaeResponse.getStatus().equals("processing")) {
187             setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
188         } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
189             setState(MICROSERVICE_UNINSTALLATION_FAILED);
190         } else {
191             setState(IN_ERROR);
192         }
193         return this.getState();
194     }
195
196     /**
197      * Convert the json response to a DcaeInventoryResponse.
198      *
199      * @param responseBody The DCAE response Json paylaod
200      * @return list of DcaeInventoryResponse
201      * @throws ParseException In case of issues with the Json parsing
202      */
203     public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
204             throws ParseException {
205         JSONParser parser = new JSONParser();
206         JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
207         JSONArray itemsArray = (JSONArray) jsonObj.get("items");
208         Iterator it = itemsArray.iterator();
209         List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
210         while (it.hasNext()) {
211             JSONObject item = (JSONObject) it.next();
212             DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
213             inventoryResponseList.add(response);
214         }
215         return inventoryResponseList;
216     }
217 }