26281dad47e4f045547bcd5bab378a4e0e9971e0
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeDispatcherServices.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP CLAMP\r
4  * ================================================================================\r
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights\r
6  *                             reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END============================================\r
20  * ===================================================================\r
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 \r
24 package org.onap.clamp.clds.client;\r
25 \r
26 import com.att.eelf.configuration.EELFLogger;\r
27 import com.att.eelf.configuration.EELFManager;\r
28 import com.fasterxml.jackson.databind.JsonNode;\r
29 import com.fasterxml.jackson.databind.node.ObjectNode;\r
30 \r
31 import java.util.Date;\r
32 \r
33 import org.json.simple.JSONObject;\r
34 import org.json.simple.parser.JSONParser;\r
35 import org.onap.clamp.clds.config.ClampProperties;\r
36 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;\r
37 import org.onap.clamp.clds.util.LoggingUtils;\r
38 import org.springframework.beans.factory.annotation.Autowired;\r
39 import org.springframework.stereotype.Component;\r
40 \r
41 /**\r
42  * This class implements the communication with DCAE for the service\r
43  * deployments.\r
44  */\r
45 @Component\r
46 public class DcaeDispatcherServices {\r
47 \r
48     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);\r
49     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
50     @Autowired\r
51     private ClampProperties refProp;\r
52     private static final String STATUS_URL_LOG = "Status URL extracted: ";\r
53     private static final String DCAE_URL_PREFIX = "/dcae-deployments/";\r
54     private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";\r
55     private static final String DCAE_REQUEST_FAILED_LOG = "RequestFailed - responseStr=";\r
56     public static final String DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";\r
57     private static final String DCAE_LINK_FIELD = "links";\r
58     private static final String DCAE_STATUS_FIELD = "status";\r
59 \r
60     /**\r
61      * Delete the deployment on DCAE.\r
62      * \r
63      * @param deploymentId\r
64      *            The deployment ID\r
65      * @return Return the URL Status\r
66      */\r
67     public String deleteDeployment(String deploymentId) {\r
68         Date startTime = new Date();\r
69         LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
70         try {\r
71             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
72             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", null, null);\r
73             JSONParser parser = new JSONParser();\r
74             Object obj0 = parser.parse(responseStr);\r
75             JSONObject jsonObj = (JSONObject) obj0;\r
76             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
77             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
78             logger.info(STATUS_URL_LOG + statusUrl);\r
79             LoggingUtils.setResponseContext("0", "Delete deployments success", this.getClass().getName());\r
80             return statusUrl;\r
81         } catch (Exception e) {\r
82             // Log StatusCode during exception in metrics log\r
83             LoggingUtils.setResponseContext("900", "Delete deployments failed", this.getClass().getName());\r
84             LoggingUtils.setErrorContext("900", "Delete deployments error");\r
85             logger.error("Exception occurred during Delete Deployment Operation with DCAE", e);\r
86             throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e);\r
87         } finally {\r
88             LoggingUtils.setTimeContext(startTime, new Date());\r
89             metricsLogger.info("deleteDeployment complete");\r
90         }\r
91     }\r
92 \r
93     /**\r
94      * Get the Operation Status from a specified URL.\r
95      * \r
96      * @param statusUrl\r
97      *            The URL provided by a previous DCAE Query\r
98      * @return The status\r
99      */\r
100     public String getOperationStatus(String statusUrl) {\r
101         // Assigning processing status to monitor operation status further\r
102         String opStatus = "processing";\r
103         Date startTime = new Date();\r
104         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
105         try {\r
106             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);\r
107             JSONParser parser = new JSONParser();\r
108             Object obj0 = parser.parse(responseStr);\r
109             JSONObject jsonObj = (JSONObject) obj0;\r
110             String operationType = (String) jsonObj.get("operationType");\r
111             String status = (String) jsonObj.get("status");\r
112             logger.info("Operation Type - " + operationType + ", Status " + status);\r
113             LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());\r
114             opStatus = status;\r
115         } catch (Exception e) {\r
116             // Log StatusCode during exception in metrics log\r
117             LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());\r
118             LoggingUtils.setErrorContext("900", "Get operation status error");\r
119             logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);\r
120         } finally {\r
121             LoggingUtils.setTimeContext(startTime, new Date());\r
122             metricsLogger.info("getOperationStatus complete");\r
123         }\r
124         return opStatus;\r
125     }\r
126 \r
127     /**\r
128      * This method send a getDeployments operation to DCAE.\r
129      */\r
130     public void getDeployments() {\r
131         Date startTime = new Date();\r
132         LoggingUtils.setTargetContext("DCAE", "getDeployments");\r
133         try {\r
134             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX;\r
135             DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);\r
136             LoggingUtils.setResponseContext("0", "Get deployments success", this.getClass().getName());\r
137         } catch (Exception e) {\r
138             // Log StatusCode during exception in metrics log\r
139             LoggingUtils.setResponseContext("900", "Get deployments failed", this.getClass().getName());\r
140             LoggingUtils.setErrorContext("900", "Get deployments error");\r
141             logger.error("Exception occurred during getDeployments Operation with DCAE", e);\r
142             throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e);\r
143         } finally {\r
144             LoggingUtils.setTimeContext(startTime, new Date());\r
145             metricsLogger.info("getDeployments complete");\r
146         }\r
147     }\r
148 \r
149     /**\r
150      * Returns status URL for createNewDeployment operation.\r
151      *\r
152      * @param deploymentId\r
153      *            The deployment ID\r
154      * @param serviceTypeId\r
155      *            Service type ID\r
156      * @param blueprintInputJson\r
157      *            The value for each blueprint parameters in a flat JSON\r
158      * @return The status URL\r
159      */\r
160     public String createNewDeployment(String deploymentId, String serviceTypeId, JsonNode blueprintInputJson) {\r
161         Date startTime = new Date();\r
162         LoggingUtils.setTargetContext("DCAE", "createNewDeployment");\r
163         try {\r
164             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
165             rootNode.put("serviceTypeId", serviceTypeId);\r
166             if (blueprintInputJson != null) {\r
167                 rootNode.put("inputs", blueprintInputJson);\r
168             }\r
169             String apiBodyString = rootNode.toString();\r
170             logger.info("Dcae api Body String - " + apiBodyString);\r
171             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
172             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "PUT", apiBodyString,\r
173                     "application/json");\r
174             JSONParser parser = new JSONParser();\r
175             Object obj0 = parser.parse(responseStr);\r
176             JSONObject jsonObj = (JSONObject) obj0;\r
177             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
178             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
179             logger.info(STATUS_URL_LOG + statusUrl);\r
180             LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());\r
181             return statusUrl;\r
182         } catch (Exception e) {\r
183             // Log StatusCode during exception in metrics log\r
184             LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());\r
185             LoggingUtils.setErrorContext("900", "Create new deployment error");\r
186             logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);\r
187             throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);\r
188         } finally {\r
189             LoggingUtils.setTimeContext(startTime, new Date());\r
190             metricsLogger.info("createNewDeployment complete");\r
191         }\r
192     }\r
193 \r
194     /**\r
195      * Returns status URL for deleteExistingDeployment operation.\r
196      * \r
197      * @param deploymentId\r
198      *            The deployment ID\r
199      * @param serviceTypeId\r
200      *            The service Type ID\r
201      * @return The status URL\r
202      */\r
203     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {\r
204         Date startTime = new Date();\r
205         LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");\r
206         try {\r
207             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
208             logger.info("Dcae api Body String - " + apiBodyString);\r
209             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
210             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", apiBodyString,\r
211                     "application/json");\r
212             JSONParser parser = new JSONParser();\r
213             Object obj0 = parser.parse(responseStr);\r
214             JSONObject jsonObj = (JSONObject) obj0;\r
215             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
216             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
217             logger.info(STATUS_URL_LOG + statusUrl);\r
218             LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());\r
219             return statusUrl;\r
220         } catch (Exception e) {\r
221             // Log StatusCode during exception in metrics log\r
222             LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());\r
223             LoggingUtils.setErrorContext("900", "Delete existing deployment error");\r
224             logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);\r
225             throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",\r
226                     e);\r
227         } finally {\r
228             LoggingUtils.setTimeContext(startTime, new Date());\r
229             metricsLogger.info("deleteExistingDeployment complete");\r
230         }\r
231     }\r
232 }