Merge "Update Documentation"
[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.node.ObjectNode;\r
29 \r
30 import java.util.Date;\r
31 \r
32 import org.json.simple.JSONObject;\r
33 import org.json.simple.parser.JSONParser;\r
34 import org.onap.clamp.clds.config.ClampProperties;\r
35 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;\r
36 import org.onap.clamp.clds.util.LoggingUtils;\r
37 import org.springframework.beans.factory.annotation.Autowired;\r
38 import org.springframework.stereotype.Component;\r
39 \r
40 /**\r
41  * This class implements the communication with DCAE for the service\r
42  * deployments.\r
43  */\r
44 @Component\r
45 public class DcaeDispatcherServices {\r
46 \r
47     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);\r
48     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
49     @Autowired\r
50     private ClampProperties refProp;\r
51     private static final String STATUS_URL_LOG = "Status URL extracted: ";\r
52     private static final String DCAE_URL_PREFIX = "/dcae-deployments/";\r
53     private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";\r
54     private static final String DCAE_REQUEST_FAILED_LOG = "RequestFailed - responseStr=";\r
55     public static final String DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";\r
56     private static final String DCAE_LINK_FIELD = "links";\r
57     private static final String DCAE_STATUS_FIELD = "status";\r
58 \r
59     /**\r
60      * Delete the deployment on DCAE.\r
61      * \r
62      * @param deploymentId\r
63      *            The deployment ID\r
64      * @return Return the URL Status\r
65      */\r
66     public String deleteDeployment(String deploymentId) {\r
67         Date startTime = new Date();\r
68         LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
69         try {\r
70             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
71             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", null, null);\r
72             JSONParser parser = new JSONParser();\r
73             Object obj0 = parser.parse(responseStr);\r
74             JSONObject jsonObj = (JSONObject) obj0;\r
75             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
76             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
77             logger.info(STATUS_URL_LOG + statusUrl);\r
78             LoggingUtils.setResponseContext("0", "Delete deployments success", this.getClass().getName());\r
79             return statusUrl;\r
80         } catch (Exception e) {\r
81             // Log StatusCode during exception in metrics log\r
82             LoggingUtils.setResponseContext("900", "Delete deployments failed", this.getClass().getName());\r
83             LoggingUtils.setErrorContext("900", "Delete deployments error");\r
84             logger.error("Exception occurred during Delete Deployment Operation with DCAE", e);\r
85             throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e);\r
86         } finally {\r
87             LoggingUtils.setTimeContext(startTime, new Date());\r
88             metricsLogger.info("deleteDeployment complete");\r
89         }\r
90     }\r
91 \r
92     /**\r
93      * Get the Operation Status from a specified URL.\r
94      * \r
95      * @param statusUrl\r
96      *            The URL provided by a previous DCAE Query\r
97      * @return The status\r
98      */\r
99     public String getOperationStatus(String statusUrl) {\r
100         // Assigning processing status to monitor operation status further\r
101         String opStatus = "processing";\r
102         Date startTime = new Date();\r
103         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
104         try {\r
105             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);\r
106             JSONParser parser = new JSONParser();\r
107             Object obj0 = parser.parse(responseStr);\r
108             JSONObject jsonObj = (JSONObject) obj0;\r
109             String operationType = (String) jsonObj.get("operationType");\r
110             String status = (String) jsonObj.get("status");\r
111             logger.info("Operation Type - " + operationType + ", Status " + status);\r
112             LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());\r
113             opStatus = status;\r
114         } catch (Exception e) {\r
115             // Log StatusCode during exception in metrics log\r
116             LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());\r
117             LoggingUtils.setErrorContext("900", "Get operation status error");\r
118             logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);\r
119         } finally {\r
120             LoggingUtils.setTimeContext(startTime, new Date());\r
121             metricsLogger.info("getOperationStatus complete");\r
122         }\r
123         return opStatus;\r
124     }\r
125 \r
126     /**\r
127      * This method send a getDeployments operation to DCAE.\r
128      */\r
129     public void getDeployments() {\r
130         Date startTime = new Date();\r
131         LoggingUtils.setTargetContext("DCAE", "getDeployments");\r
132         try {\r
133             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX;\r
134             DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);\r
135             LoggingUtils.setResponseContext("0", "Get deployments success", this.getClass().getName());\r
136         } catch (Exception e) {\r
137             // Log StatusCode during exception in metrics log\r
138             LoggingUtils.setResponseContext("900", "Get deployments failed", this.getClass().getName());\r
139             LoggingUtils.setErrorContext("900", "Get deployments error");\r
140             logger.error("Exception occurred during getDeployments Operation with DCAE", e);\r
141             throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e);\r
142         } finally {\r
143             LoggingUtils.setTimeContext(startTime, new Date());\r
144             metricsLogger.info("getDeployments complete");\r
145         }\r
146     }\r
147 \r
148     /**\r
149      * Returns status URL for createNewDeployment operation.\r
150      *\r
151      * @param deploymentId\r
152      *            The deployment ID\r
153      * @param serviceTypeId\r
154      *            Service type ID\r
155      * @param blueprintInput\r
156      *            The value for each blueprint parameters in a flat JSON\r
157      * @return The status URL\r
158      */\r
159     public String createNewDeployment(String deploymentId, String serviceTypeId) {\r
160         Date startTime = new Date();\r
161         LoggingUtils.setTargetContext("DCAE", "createNewDeployment");\r
162         try {\r
163             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
164             rootNode.put("serviceTypeId", serviceTypeId);\r
165             String apiBodyString = rootNode.toString();\r
166             logger.info("Dcae api Body String - " + apiBodyString);\r
167             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
168             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "PUT", apiBodyString,\r
169                     "application/json");\r
170             JSONParser parser = new JSONParser();\r
171             Object obj0 = parser.parse(responseStr);\r
172             JSONObject jsonObj = (JSONObject) obj0;\r
173             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
174             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
175             logger.info(STATUS_URL_LOG + statusUrl);\r
176             LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());\r
177             return statusUrl;\r
178         } catch (Exception e) {\r
179             // Log StatusCode during exception in metrics log\r
180             LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());\r
181             LoggingUtils.setErrorContext("900", "Create new deployment error");\r
182             logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);\r
183             throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);\r
184         } finally {\r
185             LoggingUtils.setTimeContext(startTime, new Date());\r
186             metricsLogger.info("createNewDeployment complete");\r
187         }\r
188     }\r
189 \r
190     /**\r
191      * Returns status URL for deleteExistingDeployment operation.\r
192      * \r
193      * @param deploymentId\r
194      *            The deployment ID\r
195      * @param serviceTypeId\r
196      *            The service Type ID\r
197      * @return The status URL\r
198      */\r
199     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {\r
200         Date startTime = new Date();\r
201         LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");\r
202         try {\r
203             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
204             logger.info("Dcae api Body String - " + apiBodyString);\r
205             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
206             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", apiBodyString,\r
207                     "application/json");\r
208             JSONParser parser = new JSONParser();\r
209             Object obj0 = parser.parse(responseStr);\r
210             JSONObject jsonObj = (JSONObject) obj0;\r
211             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
212             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
213             logger.info(STATUS_URL_LOG + statusUrl);\r
214             LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());\r
215             return statusUrl;\r
216         } catch (Exception e) {\r
217             // Log StatusCode during exception in metrics log\r
218             LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());\r
219             LoggingUtils.setErrorContext("900", "Delete existing deployment error");\r
220             logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);\r
221             throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",\r
222                     e);\r
223         } finally {\r
224             LoggingUtils.setTimeContext(startTime, new Date());\r
225             metricsLogger.info("deleteExistingDeployment complete");\r
226         }\r
227     }\r
228 }