89332bbd0b50494436100a5ee3c24b2baaf92eaf
[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
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.UUID;
32
33 import org.apache.camel.Exchange;
34 import org.json.simple.JSONArray;
35 import org.json.simple.JSONObject;
36 import org.json.simple.parser.JSONParser;
37 import org.json.simple.parser.ParseException;
38 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
39 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
40 import org.onap.clamp.clds.util.JsonUtils;
41 import org.onap.clamp.loop.Loop;
42 import org.onap.clamp.policy.microservice.MicroServicePolicy;
43
44 public class DcaeComponent extends ExternalComponent {
45
46     private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
47     private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
48     private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
49     private static final String DCAE_INPUTS = "inputs";
50
51     private String name;
52
53     public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
54             "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
55     public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
56             "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
57             + "defined in the DCAE blueprint and it's currently processing the request");
58     public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
59             "MICROSERVICE_INSTALLATION_FAILED",
60             "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
61     public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
62             "MICROSERVICE_INSTALLED_SUCCESSFULLY",
63             "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
64     public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
65             "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
66             + "defined in the DCAE blueprint and it's currently processing the request");
67     public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
68             "MICROSERVICE_UNINSTALLATION_FAILED",
69             "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
70     public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
71             "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
72             "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
73     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
74             "There was an error during the request done to DCAE, look at the logs or try again");
75
76     public DcaeComponent() {
77         super(BLUEPRINT_DEPLOYED);
78         this.name = "DCAE";
79     }
80
81     public DcaeComponent(String name) {
82         super(BLUEPRINT_DEPLOYED);
83         this.name = "DCAE_" + name;
84     }
85
86     @Override
87     public String getComponentName() {
88         return name;
89     }
90
91
92     /**
93      * Convert the json response to a DcaeOperationStatusResponse.
94      *
95      * @param responseBody The DCAE response Json paylaod
96      * @return The dcae object provisioned
97      */
98     public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
99         if (responseBody != null && !responseBody.isEmpty()) {
100             return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
101         } else {
102             return null;
103         }
104     }
105
106     /**
107      * Generate the deployment id, it's random.
108      *
109      * @return The deployment id
110      */
111     public static String generateDeploymentId() {
112         return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
113     }
114
115     /**
116      * This method prepare the url returned by DCAE to check the status if fine. It
117      * extracts it from the dcaeResponse.
118      *
119      * @param dcaeResponse The dcae response object
120      * @return the Right Url modified if needed
121      */
122     public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
123         return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
124     }
125
126     /**
127      * Return the deploy payload for DCAE.
128      *
129      * @param loop The loop object
130      * @return The payload used to send deploy closed loop request
131      */
132     public static String getDeployPayload(Loop loop) {
133         JsonObject globalProp = loop.getGlobalPropertiesJson();
134         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
135
136         String serviceTypeId = loop.getLoopTemplate().getDcaeBlueprintId();
137
138         JsonObject rootObject = new JsonObject();
139         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
140         if (deploymentProp != null) {
141             rootObject.add(DCAE_INPUTS, deploymentProp);
142         }
143         return rootObject.toString();
144     }
145
146     /**
147      * Return the deploy payload for DCAE.
148      *
149      * @param loop               The loop object
150      * @param microServicePolicy The micro service policy
151      * @return The payload used to send deploy closed loop request
152      */
153     public static String getDeployPayload(Loop loop, MicroServicePolicy microServicePolicy) {
154         JsonObject globalProp = loop.getGlobalPropertiesJson();
155         JsonObject deploymentProp =
156                 globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServicePolicy.getName());
157
158         String serviceTypeId = microServicePolicy.getDcaeBlueprintId();
159
160         JsonObject rootObject = new JsonObject();
161         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
162         if (deploymentProp != null) {
163             rootObject.add(DCAE_INPUTS, deploymentProp);
164         }
165         return rootObject.toString();
166     }
167
168     /**
169      * Return the uninstallation payload for DCAE.
170      *
171      * @param loop The loop object
172      * @return The payload in string (json)
173      */
174     public static String getUndeployPayload(Loop loop) {
175         JsonObject rootObject = new JsonObject();
176         rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getLoopTemplate().getDcaeBlueprintId());
177         return rootObject.toString();
178     }
179
180     /**
181      * Return the uninstallation payload for DCAE.
182      *
183      * @param policy The microServicePolicy object
184      * @return The payload in string (json)
185      */
186     public static String getUndeployPayload(MicroServicePolicy policy) {
187         JsonObject rootObject = new JsonObject();
188         rootObject.addProperty(DCAE_SERVICETYPE_ID, policy.getDcaeBlueprintId());
189         return rootObject.toString();
190     }
191
192     @Override
193     public ExternalComponentState computeState(Exchange camelExchange) {
194
195         DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
196                 .getProperty("dcaeResponse");
197
198         if (dcaeResponse == null) {
199             setState(BLUEPRINT_DEPLOYED);
200         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
201             setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
202         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
203             setState(PROCESSING_MICROSERVICE_INSTALLATION);
204         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
205             setState(MICROSERVICE_INSTALLATION_FAILED);
206         } else if (dcaeResponse.getOperationType().equals("uninstall")
207                 && dcaeResponse.getStatus().equals("succeeded")) {
208             setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
209         } else if (dcaeResponse.getOperationType().equals("uninstall")
210                 && dcaeResponse.getStatus().equals("processing")) {
211             setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
212         } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
213             setState(MICROSERVICE_UNINSTALLATION_FAILED);
214         } else {
215             setState(IN_ERROR);
216         }
217         return this.getState();
218     }
219
220     /**
221      * Convert the json response to a DcaeInventoryResponse.
222      *
223      * @param responseBody The DCAE response Json paylaod
224      * @return list of DcaeInventoryResponse
225      * @throws ParseException In case of issues with the Json parsing
226      */
227     public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
228             throws ParseException {
229         JSONParser parser = new JSONParser();
230         JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
231         JSONArray itemsArray = (JSONArray) jsonObj.get("items");
232         Iterator it = itemsArray.iterator();
233         List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
234         while (it.hasNext()) {
235             JSONObject item = (JSONObject) it.next();
236             DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
237             inventoryResponseList.add(response);
238         }
239         return inventoryResponseList;
240     }
241 }