Improve Logs in the methods.
[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.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     public static final String        DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";\r
54     private static final String       DCAE_LINK_FIELD              = "links";\r
55     private static final String       DCAE_STATUS_FIELD            = "status";\r
56 \r
57     /**\r
58      * Delete the deployment on DCAE.\r
59      * \r
60      * @param deploymentId\r
61      *            The deployment ID\r
62      * @return Return the URL Status\r
63      */\r
64     public String deleteDeployment(String deploymentId) {\r
65         Date startTime = new Date();\r
66         LoggingUtils.setTargetContext("DCAE", "deleteDeployment");\r
67         try {\r
68             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
69             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", null, null);\r
70             JSONParser parser = new JSONParser();\r
71             Object obj0 = parser.parse(responseStr);\r
72             JSONObject jsonObj = (JSONObject) obj0;\r
73             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
74             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
75             logger.info(STATUS_URL_LOG + statusUrl);\r
76             LoggingUtils.setResponseContext("0", "Delete deployments success", this.getClass().getName());\r
77             return statusUrl;\r
78         } catch (Exception e) {\r
79                 //Log StatusCode during exception in metrics log \r
80             LoggingUtils.setResponseContext("900", "Delete deployments failed", this.getClass().getName());\r
81             LoggingUtils.setErrorContext("900", "Delete deployments error");\r
82             logger.error("Exception occurred during Delete Deployment Operation with DCAE", e);\r
83             throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e);\r
84         } finally {\r
85             LoggingUtils.setTimeContext(startTime, new Date());\r
86             metricsLogger.info("deleteDeployment complete");\r
87         }\r
88     }\r
89 \r
90     /**\r
91      * Get the Operation Status from a specified URL.\r
92      * \r
93      * @param statusUrl\r
94      *            The URL provided by a previous DCAE Query\r
95      * @return The status\r
96      * \r
97      */\r
98     public String getOperationStatus(String statusUrl) {\r
99         // Assigning processing status to monitor operation status further\r
100         String opStatus = "processing";\r
101         Date startTime = new Date();\r
102         LoggingUtils.setTargetContext("DCAE", "getOperationStatus");\r
103         try {\r
104             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);\r
105             JSONParser parser = new JSONParser();\r
106             Object obj0 = parser.parse(responseStr);\r
107             JSONObject jsonObj = (JSONObject) obj0;\r
108             String operationType = (String) jsonObj.get("operationType");\r
109             String status = (String) jsonObj.get("status");\r
110             logger.info("Operation Type - " + operationType + ", Status " + status);\r
111             LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());\r
112             opStatus = status;\r
113         } catch (Exception e) {\r
114                 //Log StatusCode during exception in metrics log \r
115             LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());\r
116             LoggingUtils.setErrorContext("900", "Get operation status error");\r
117             logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);\r
118         } finally {\r
119             LoggingUtils.setTimeContext(startTime, new Date());\r
120             metricsLogger.info("getOperationStatus complete");\r
121         }\r
122         return opStatus;\r
123     }\r
124 \r
125     /**\r
126      * This method send a getDeployments operation to DCAE.\r
127      * \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      * @return The status URL\r
156      */\r
157     public String createNewDeployment(String deploymentId, String serviceTypeId) {\r
158         Date startTime = new Date();\r
159         LoggingUtils.setTargetContext("DCAE", "createNewDeployment");\r
160         try {\r
161             ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");\r
162             rootNode.put("serviceTypeId", serviceTypeId);\r
163             String apiBodyString = rootNode.toString();\r
164             logger.info("Dcae api Body String - " + apiBodyString);\r
165             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
166             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "PUT", apiBodyString,\r
167                     "application/json");\r
168             JSONParser parser = new JSONParser();\r
169             Object obj0 = parser.parse(responseStr);\r
170             JSONObject jsonObj = (JSONObject) obj0;\r
171             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
172             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
173             logger.info(STATUS_URL_LOG + statusUrl);\r
174             LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());\r
175             return statusUrl;\r
176         } catch (Exception e) {\r
177                 //Log StatusCode during exception in metrics log \r
178             LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());\r
179             LoggingUtils.setErrorContext("900", "Create new deployment error");\r
180             logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);\r
181             throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);\r
182         } finally {\r
183             LoggingUtils.setTimeContext(startTime, new Date());\r
184             metricsLogger.info("createNewDeployment complete");\r
185         }\r
186     }\r
187 \r
188     /**\r
189      * Returns status URL for deleteExistingDeployment operation.\r
190      * \r
191      * @param deploymentId\r
192      *            The deployment ID\r
193      * @param serviceTypeId\r
194      *            The service Type ID\r
195      * @return The status URL\r
196      */\r
197     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {\r
198         Date startTime = new Date();\r
199         LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");\r
200         try {\r
201             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
202             logger.info("Dcae api Body String - " + apiBodyString);\r
203             String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;\r
204             String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "DELETE", apiBodyString,\r
205                     "application/json");\r
206             JSONParser parser = new JSONParser();\r
207             Object obj0 = parser.parse(responseStr);\r
208             JSONObject jsonObj = (JSONObject) obj0;\r
209             JSONObject linksObj = (JSONObject) jsonObj.get(DCAE_LINK_FIELD);\r
210             String statusUrl = (String) linksObj.get(DCAE_STATUS_FIELD);\r
211             logger.info(STATUS_URL_LOG + statusUrl);\r
212             LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());\r
213             return statusUrl;\r
214         } catch (Exception e) {\r
215                 //Log StatusCode during exception in metrics log \r
216             LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());\r
217             LoggingUtils.setErrorContext("900", "Delete existing deployment error");\r
218             logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);\r
219             throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",\r
220                     e);\r
221         } finally {\r
222             LoggingUtils.setTimeContext(startTime, new Date());\r
223             metricsLogger.info("deleteExistingDeployment complete");\r
224         }\r
225     }\r
226 }