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