Add "code moved" warning to old CLAMP repo readme
[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.apache.camel.CamelContext;\r
34 import org.apache.camel.Exchange;\r
35 import org.apache.camel.builder.ExchangeBuilder;\r
36 import org.json.simple.JSONArray;\r
37 import org.json.simple.JSONObject;\r
38 import org.json.simple.parser.JSONParser;\r
39 import org.json.simple.parser.ParseException;\r
40 import org.onap.clamp.clds.config.ClampProperties;\r
41 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;\r
42 import org.onap.clamp.clds.util.JsonUtils;\r
43 import org.onap.clamp.clds.util.LoggingUtils;\r
44 import org.springframework.beans.factory.annotation.Autowired;\r
45 import org.springframework.stereotype.Component;\r
46 \r
47 /**\r
48  * This class implements the communication with DCAE for the service inventory.\r
49  */\r
50 @Component\r
51 public class DcaeInventoryServices {\r
52 \r
53     @Autowired\r
54     CamelContext camelContext;\r
55 \r
56     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);\r
57     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();\r
58     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();\r
59     public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";\r
60     public static final String DCAE_INVENTORY_RETRY_INTERVAL = "dcae.intentory.retry.interval";\r
61     public static final String DCAE_INVENTORY_RETRY_LIMIT = "dcae.intentory.retry.limit";\r
62     private final ClampProperties refProp;\r
63 \r
64     /**\r
65      * Constructor.\r
66      */\r
67     @Autowired\r
68     public DcaeInventoryServices(ClampProperties refProp) {\r
69         this.refProp = refProp;\r
70     }\r
71 \r
72     private int getTotalCountFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
73         JSONParser parser = new JSONParser();\r
74         Object obj0 = parser.parse(responseStr);\r
75         JSONObject jsonObj = (JSONObject) obj0;\r
76         Long totalCount = (Long) jsonObj.get("totalCount");\r
77         return totalCount.intValue();\r
78     }\r
79 \r
80     private DcaeInventoryResponse getItemsFromDcaeInventoryResponse(String responseStr) throws ParseException {\r
81         JSONParser parser = new JSONParser();\r
82         Object obj0 = parser.parse(responseStr);\r
83         JSONObject jsonObj = (JSONObject) obj0;\r
84         JSONArray itemsArray = (JSONArray) jsonObj.get("items");\r
85         JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);\r
86         return JsonUtils.GSON.fromJson(dcaeServiceType0.toString(), DcaeInventoryResponse.class);\r
87     }\r
88 \r
89     /**\r
90      * DO a query to DCAE to get some Information.\r
91      *\r
92      * @param artifactName The artifact Name\r
93      * @param serviceUuid  The service UUID\r
94      * @param resourceUuid The resource UUID\r
95      * @return The DCAE inventory for the artifact in DcaeInventoryResponse\r
96      * @throws IOException    In case of issues with the stream\r
97      * @throws ParseException In case of issues with the Json parsing\r
98      */\r
99     public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)\r
100             throws IOException, ParseException, InterruptedException {\r
101         LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");\r
102 \r
103         int retryInterval = 0;\r
104         int retryLimit = 1;\r
105         if (refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT) != null) {\r
106             retryLimit = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_LIMIT));\r
107         }\r
108         if (refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL) != null) {\r
109             retryInterval = Integer.valueOf(refProp.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL));\r
110         }\r
111         for (int i = 0; i < retryLimit; i++) {\r
112             Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
113                     .withProperty("blueprintResourceId", resourceUuid).withProperty("blueprintServiceId", serviceUuid)\r
114                     .withProperty("blueprintName", artifactName).build();\r
115             metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory");\r
116 \r
117             Exchange exchangeResponse = camelContext.createProducerTemplate()\r
118                     .send("direct:get-dcae-blueprint-inventory", myCamelExchange);\r
119 \r
120             if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
121                 String dcaeResponse = (String) exchangeResponse.getIn().getBody();\r
122                 int totalCount = getTotalCountFromDcaeInventoryResponse(dcaeResponse);\r
123                 metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount);\r
124                 if (totalCount > 0) {\r
125                     logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeResponse);\r
126                     LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());\r
127                     Date startTime = new Date();\r
128                     LoggingUtils.setTimeContext(startTime, new Date());\r
129                     return getItemsFromDcaeInventoryResponse(dcaeResponse);\r
130                 } else {\r
131                     logger.info("Dcae inventory totalCount returned is 0, so waiting " + retryInterval\r
132                             + "ms before retrying ...");\r
133                     // wait for a while and try to connect to DCAE again\r
134                     Thread.sleep(retryInterval);\r
135                 }\r
136             }\r
137         }\r
138         logger.warn("Dcae inventory totalCount returned is still 0, after " + retryLimit + " attempts, returning NULL");\r
139         return null;\r
140     }\r
141 }\r