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