2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
\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
12 * http://www.apache.org/licenses/LICENSE-2.0
\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
24 package org.onap.clamp.clds.client;
\r
26 import com.att.eelf.configuration.EELFLogger;
\r
27 import com.att.eelf.configuration.EELFManager;
\r
28 import com.fasterxml.jackson.core.JsonProcessingException;
\r
29 import com.fasterxml.jackson.databind.ObjectMapper;
\r
30 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
32 import java.io.IOException;
\r
33 import java.util.Date;
\r
34 import java.util.List;
\r
36 import javax.ws.rs.BadRequestException;
\r
38 import org.json.simple.JSONArray;
\r
39 import org.json.simple.JSONObject;
\r
40 import org.json.simple.parser.JSONParser;
\r
41 import org.json.simple.parser.ParseException;
\r
42 import org.onap.clamp.clds.config.ClampProperties;
\r
43 import org.onap.clamp.clds.dao.CldsDao;
\r
44 import org.onap.clamp.clds.model.CldsEvent;
\r
45 import org.onap.clamp.clds.model.CldsModel;
\r
46 import org.onap.clamp.clds.model.DcaeEvent;
\r
47 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
\r
48 import org.onap.clamp.clds.model.properties.Global;
\r
49 import org.onap.clamp.clds.model.properties.ModelProperties;
\r
50 import org.onap.clamp.clds.util.LoggingUtils;
\r
51 import org.springframework.beans.factory.annotation.Autowired;
\r
52 import org.springframework.stereotype.Component;
\r
55 * This class implements the communication with DCAE for the service inventory.
\r
58 public class DcaeInventoryServices {
\r
60 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);
\r
61 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
\r
62 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
63 public static final String DCAE_INVENTORY_URL = "dcae.inventory.url";
\r
64 public static final String DCAE_TYPE_NAME = "typeName";
\r
65 public static final String DCAE_TYPE_ID = "typeId";
\r
67 private ClampProperties refProp;
\r
69 private CldsDao cldsDao;
\r
72 * Set the event inventory.
\r
78 * @throws ParseException
\r
79 * In case of DCAE Json parse exception
\r
81 public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException {
\r
82 String artifactName = cldsModel.getControlName();
\r
83 DcaeEvent dcaeEvent = new DcaeEvent();
\r
84 DcaeInventoryResponse dcaeResponse = null;
\r
85 Date startTime = new Date();
\r
86 LoggingUtils.setTargetContext("DCAE", "setEventInventory");
\r
87 if (artifactName != null) {
\r
88 artifactName = artifactName + ".yml";
\r
91 // Below are the properties required for calling the dcae inventory
\r
92 ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false,
\r
93 "{}", cldsModel.getPropText());
\r
94 Global global = prop.getGlobal();
\r
95 String invariantServiceUuid = global.getService();
\r
96 List<String> resourceUuidList = global.getResourceVf();
\r
97 String resourceUuid = "";
\r
98 if (resourceUuidList != null && !resourceUuidList.isEmpty()) {
\r
99 resourceUuid = resourceUuidList.get(0);
\r
101 /* Inventory service url is called in this method */
\r
102 dcaeResponse = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);
\r
103 /* set dcae events */
\r
104 dcaeEvent.setArtifactName(artifactName);
\r
105 dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);
\r
106 LoggingUtils.setResponseContext("0", "Set inventory success", this.getClass().getName());
\r
107 } catch (JsonProcessingException e) {
\r
108 LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName());
\r
109 LoggingUtils.setErrorContext("900", "Set inventory error");
\r
110 logger.error("Error during JSON decoding", e);
\r
111 } catch (IOException ex) {
\r
112 LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName());
\r
113 LoggingUtils.setErrorContext("900", "Set inventory error");
\r
114 logger.error("Error during DCAE communication", ex);
\r
116 LoggingUtils.setTimeContext(startTime, new Date());
\r
117 metricsLogger.info("setEventInventory complete");
\r
119 this.analyzeAndSaveDcaeResponse(dcaeResponse, cldsModel, dcaeEvent, userId);
\r
122 private void analyzeAndSaveDcaeResponse(DcaeInventoryResponse dcaeResponse, CldsModel cldsModel,
\r
123 DcaeEvent dcaeEvent, String userId) throws ParseException {
\r
124 if (dcaeResponse != null) {
\r
125 logger.info("Dcae Response for query on inventory: " + dcaeResponse);
\r
126 String oldTypeId = cldsModel.getTypeId();
\r
127 String newTypeId = "";
\r
128 if (dcaeResponse.getTypeId() != null) {
\r
129 newTypeId = dcaeResponse.getTypeId();
\r
130 cldsModel.setTypeId(dcaeResponse.getTypeId());
\r
132 if (dcaeResponse.getTypeName() != null) {
\r
133 cldsModel.setTypeName(dcaeResponse.getTypeName());
\r
135 if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)
\r
136 || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {
\r
137 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),
\r
138 CldsEvent.ACTION_STATE_RECEIVED, null);
\r
140 cldsModel.save(cldsDao, userId);
\r
142 logger.info(cldsModel.getName() + " Model is not present in Dcae Inventory Service.");
\r
147 * DO a query to DCAE to get some Information.
\r
149 * @param artifactName
\r
150 * The artifact Name
\r
151 * @param serviceUuid
\r
153 * @param resourceUuid
\r
154 * The resource UUID
\r
155 * @return The DCAE inventory for the artifact in DcaeInventoryResponse
\r
156 * @throws IOException
\r
157 * In case of issues with the stream
\r
158 * @throws ParseException
\r
159 * In case of issues with the Json parsing
\r
161 public DcaeInventoryResponse getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)
\r
162 throws IOException, ParseException {
\r
163 Date startTime = new Date();
\r
164 LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");
\r
165 String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="
\r
167 String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;
\r
168 logger.info("Dcae Inventory Service full url - " + fullUrl);
\r
169 String dcaeInventoryResponse = null;
\r
170 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);
\r
171 JSONParser parser = new JSONParser();
\r
172 Object obj0 = parser.parse(responseStr);
\r
173 JSONObject jsonObj = (JSONObject) obj0;
\r
174 Long totalCount = (Long) jsonObj.get("totalCount");
\r
175 int numServices = totalCount.intValue();
\r
176 if (numServices > 0) {
\r
177 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
\r
178 JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);
\r
179 dcaeInventoryResponse = dcaeServiceType0.toString();
\r
180 logger.info("getDcaeInformation, answer from DCAE inventory:" + dcaeInventoryResponse);
\r
182 LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());
\r
183 LoggingUtils.setTimeContext(startTime, new Date());
\r
184 metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);
\r
185 return new ObjectMapper().readValue(dcaeInventoryResponse, DcaeInventoryResponse.class);
\r
189 * Inserts a new DCAEServiceType or updates an existing instance. If the
\r
190 * typeName is same second time(already exists) then the
\r
191 * DCAEServiceTypeRequest is updated
\r
193 * @param blueprintTemplate
\r
194 * blueprint content
\r
196 * owner of the data
\r
198 * The type/artifact Name
\r
199 * @param typeVersion
\r
201 * @param asdcServiceId
\r
203 * @param asdcResourceId
\r
205 * @return The DCAE inventory type id
\r
207 public String createupdateDCAEServiceType(String blueprintTemplate, String owner, String typeName, int typeVersion,
\r
208 String asdcServiceId, String asdcResourceId) {
\r
209 Date startTime = new Date();
\r
210 LoggingUtils.setTargetContext("DCAE", "createDCAEServiceType");
\r
211 String typeId = null;
\r
213 ObjectMapper mapper = new ObjectMapper();
\r
214 ObjectNode dcaeServiceTypeRequest = mapper.createObjectNode();
\r
215 dcaeServiceTypeRequest.put("blueprintTemplate", blueprintTemplate);
\r
216 dcaeServiceTypeRequest.put("owner", owner);
\r
217 dcaeServiceTypeRequest.put("typeName", typeName);
\r
218 dcaeServiceTypeRequest.put("typeVersion", typeVersion);
\r
219 dcaeServiceTypeRequest.put("asdcServiceId", asdcServiceId);
\r
220 dcaeServiceTypeRequest.put("asdcResourceId", asdcResourceId);
\r
221 String apiBodyString = dcaeServiceTypeRequest.toString();
\r
222 logger.info("Dcae api Body String - " + apiBodyString);
\r
223 String url = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types";
\r
224 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,
\r
225 "application/json");
\r
226 // If the DCAEServiceTypeRequest is created successfully it will
\r
227 // return a json object responce containing a node for newly created
\r
229 // The newly generated DCAEServiceTypeRequest can then be accessed
\r
230 // via URL: https://<DCAE_INVENTORY_URL>/dcae-service-types/<typeId>
\r
231 JSONParser parser = new JSONParser();
\r
232 Object obj0 = parser.parse(responseStr);
\r
233 JSONObject jsonObj = (JSONObject) obj0;
\r
234 typeId = (String) jsonObj.get("typeId"); // need to save this
\r
238 } catch (IOException | ParseException e) {
\r
239 logger.error("Exception occurred during createupdateDCAEServiceType Operation with DCAE", e);
\r
240 throw new BadRequestException("Exception occurred during createupdateDCAEServiceType Operation with DCAE",
\r
243 if (typeId != null) {
\r
244 LoggingUtils.setResponseContext("0", "Create update DCAE ServiceType success",
\r
245 this.getClass().getName());
\r
247 LoggingUtils.setResponseContext("900", "Create update DCAE ServiceType failed",
\r
248 this.getClass().getName());
\r
249 LoggingUtils.setErrorContext("900", "Create update DCAE ServiceType error");
\r
251 LoggingUtils.setTimeContext(startTime, new Date());
\r
252 metricsLogger.info("createupdateDCAEServiceType complete");
\r