Fix Sonar vulnerabilities
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeInventoryServices.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  * Modifications copyright (c) 2018 Nokia\r
21  * ===================================================================\r
22  *\r
23  */\r
24 \r
25 package org.onap.clamp.clds.client;\r
26 \r
27 import com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 \r
30 import java.io.IOException;\r
31 import java.util.Date;\r
32 \r
33 import org.json.simple.JSONArray;\r
34 import org.json.simple.JSONObject;\r
35 import org.json.simple.parser.JSONParser;\r
36 import org.json.simple.parser.ParseException;\r
37 import org.onap.clamp.clds.config.ClampProperties;\r
38 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;\r
39 import org.onap.clamp.clds.util.JsonUtils;\r
40 import org.onap.clamp.clds.util.LoggingUtils;\r
41 import org.onap.clamp.util.HttpConnectionManager;\r
42 import org.springframework.beans.factory.annotation.Autowired;\r
43 import org.springframework.stereotype.Component;\r
44 \r
45 /**\r
46  * This class implements the communication with DCAE for the service inventory.\r
47  */\r
48 @Component\r
49 public class DcaeInventoryServices {\r
50 \r
51     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
52     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
53     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
54     public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
55     public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
56     public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
57     private final ClampProperties refProp;\r
58     private final HttpConnectionManager httpConnectionManager;\r
59 \r
60     /**\r
61      * Constructor.\r
62      */\r
63     @Autowired\r
64     public DcaeInventoryServices(ClampProperties refProp, HttpConnectionManager httpConnectionManager) {\r
65         this.refProp = refProp;\r
66         this.httpConnectionManager = httpConnectionManager;\r
67     }\r
68 \r
69     private int getTotalCountFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
70         JSONParser parser = new JSONParser();\r
71         Object obj0 = parser.parse(responseStr);\r
72         JSONObject jsonObj = (JSONObject) obj0;\r
73         Long totalCount = (Long) jsonObj.get("totalCount");\r
74         return totalCount.intValue();\r
75     }\r
76 \r
77     private DcaeInventoryResponse getItemsFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
78         JSONParser parser = new JSONParser();\r
79         Object obj0 = parser.parse(responseStr);\r
80         JSONObject jsonObj = (JSONObject) obj0;\r
81         JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
82         JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
83         return JsonUtils.GSON.fromJson(dcaeServiceType0.toString(), DcaeInventoryResponse.class);\r
84     }\r
85 \r
86     /**\r
87      * DO a query to DCAE to get some Information.\r
88      *\r
89      * @param artifactName The artifact Name\r
90      * @param serviceUuid  The service UUID\r
91      * @param resourceUuid The resource UUID\r
92      * @return The DCAE inventory for the artifact in DcaeInventoryResponse\r
93      * @throws IOException    In case of issues with the stream\r
94      * @throws ParseException In case of issues with the Json parsing\r
95      */\r
96     public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
97             throws IOException, ParseException, InterruptedException {\r
98         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
99         String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="\r
100                 + artifactName;\r
101         String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;\r
102         logger.info("Dcae Inventory Service full url - " + fullUrl);\r
103         DcaeInventoryResponse response = queryDcaeInventory(fullUrl);\r
104         LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());\r
105         Date startTime = new Date();\r
106         LoggingUtils.setTimeContext(startTime, new Date());\r
107         return response;\r
108     }\r
109 \r
110     private DcaeInventoryResponse queryDcaeInventory(String fullUrl)\r
111             throws IOException, InterruptedException, ParseException {\r
112         int retryInterval = 0;\r
113         int retryLimit = 1;\r
114         if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) {\r
115             retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT));\r
116         }\r
117         if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
118             retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
119         }\r
120         for (int i = 0; i < retryLimit; i++) {\r
121             metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
122             String response = httpConnectionManager.doHttpRequest(fullUrl, "GET", null, null, "DCAE", null, null);\r
123             int totalCount = getTotalCountFromDcaeInventoryResponse(response);\r
124             metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
125             if (totalCount > 0) {\r
126                 logger.info("getDcaeInformation, answer from DCAE inventory:" + response);\r
127                 return getItemsFromDcaeInventoryResponse(response);\r
128             }\r
129             logger.info(\r
130                     "Dcae inventory totalCount returned is 0, so waiting " + retryInterval + "ms before retrying ...");\r
131             // wait for a while and try to connect to DCAE again\r
132             Thread.sleep(retryInterval);\r
133         }\r
134         logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL");\r
135         return null;\r
136     }\r
137 }\r