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.security.GeneralSecurityException;
\r
34 import java.util.Date;
\r
35 import java.util.List;
\r
37 import javax.ws.rs.BadRequestException;
\r
39 import org.apache.commons.codec.DecoderException;
\r
40 import org.json.simple.JSONArray;
\r
41 import org.json.simple.JSONObject;
\r
42 import org.json.simple.parser.JSONParser;
\r
43 import org.json.simple.parser.ParseException;
\r
44 import org.onap.clamp.clds.config.CldsReferenceProperties;
\r
45 import org.onap.clamp.clds.dao.CldsDao;
\r
46 import org.onap.clamp.clds.model.CldsEvent;
\r
47 import org.onap.clamp.clds.model.CldsModel;
\r
48 import org.onap.clamp.clds.model.DcaeEvent;
\r
49 import org.onap.clamp.clds.model.properties.Global;
\r
50 import org.onap.clamp.clds.model.properties.ModelProperties;
\r
51 import org.onap.clamp.clds.util.LoggingUtils;
\r
52 import org.springframework.beans.factory.annotation.Autowired;
\r
53 import org.springframework.stereotype.Component;
\r
56 * This class implements the communication with DCAE for the service inventory.
\r
59 public class DcaeInventoryServices {
\r
61 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);
\r
62 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
\r
63 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
64 private static final String DCAE_INVENTORY_URL = "DCAE_INVENTORY_URL";
\r
66 private CldsReferenceProperties refProp;
\r
68 private CldsDao cldsDao;
\r
71 * Set the event inventory.
\r
77 * @throws GeneralSecurityException
\r
78 * In case of issue when decryting the DCAE password
\r
79 * @throws ParseException
\r
80 * In case of DCAE Json parse exception
\r
81 * @throws DecoderException
\r
82 * In case of issues with HexString decoding
\r
84 public void setEventInventory(CldsModel cldsModel, String userId)
\r
85 throws GeneralSecurityException, ParseException, DecoderException {
\r
86 String artifactName = cldsModel.getControlName();
\r
87 DcaeEvent dcaeEvent = new DcaeEvent();
\r
88 String isDcaeInfoAvailable = null;
\r
89 Date startTime = new Date();
\r
90 LoggingUtils.setTargetContext("DCAE", "setEventInventory");
\r
91 if (artifactName != null) {
\r
92 artifactName = artifactName + ".yml";
\r
95 // Below are the properties required for calling the dcae inventory
\r
96 ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false,
\r
97 "{}", cldsModel.getPropText());
\r
98 Global global = prop.getGlobal();
\r
99 String invariantServiceUuid = global.getService();
\r
100 List<String> resourceUuidList = global.getResourceVf();
\r
101 String resourceUuid = "";
\r
102 if (resourceUuidList != null && !resourceUuidList.isEmpty()) {
\r
103 resourceUuid = resourceUuidList.get(0);
\r
105 /* Inventory service url is called in this method */
\r
106 isDcaeInfoAvailable = getDcaeInformation(artifactName, invariantServiceUuid, resourceUuid);
\r
107 /* set dcae events */
\r
108 dcaeEvent.setArtifactName(artifactName);
\r
109 dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);
\r
110 LoggingUtils.setResponseContext("0", "Set inventory success", this.getClass().getName());
\r
111 } catch (JsonProcessingException e) {
\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 JSON decoding", e);
\r
115 } catch (IOException ex) {
\r
116 LoggingUtils.setResponseContext("900", "Set inventory failed", this.getClass().getName());
\r
117 LoggingUtils.setErrorContext("900", "Set inventory error");
\r
118 logger.error("Error during DCAE communication", ex);
\r
120 LoggingUtils.setTimeContext(startTime, new Date());
\r
121 metricsLogger.info("setEventInventory complete");
\r
123 /* Null whether the DCAE has items lenght or not */
\r
124 if (isDcaeInfoAvailable != null) {
\r
125 /* Inserting Event in to DB */
\r
126 logger.info(isDcaeInfoAvailable);
\r
127 JSONParser parser = new JSONParser();
\r
128 Object obj0 = parser.parse(isDcaeInfoAvailable);
\r
129 JSONObject jsonObj = (JSONObject) obj0;
\r
130 String oldTypeId = cldsModel.getTypeId();
\r
131 String newTypeId = "";
\r
132 if (jsonObj.get("typeId") != null) {
\r
133 newTypeId = jsonObj.get("typeId").toString();
\r
134 cldsModel.setTypeId(jsonObj.get("typeId").toString());
\r
136 // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml");
\r
137 if (jsonObj.get("typeName") != null) {
\r
138 cldsModel.setTypeName(jsonObj.get("typeName").toString());
\r
140 if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)
\r
141 || cldsModel.getEvent().getActionCd().equalsIgnoreCase(CldsEvent.ACTION_SUBMITDCAE)) {
\r
142 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),
\r
143 CldsEvent.ACTION_STATE_RECEIVED, null);
\r
145 cldsModel.save(cldsDao, userId);
\r
147 logger.info(cldsModel.getName() + " Model is not present in Dcae Inventory Service.");
\r
152 * DO a query to DCAE to get some Information.
\r
154 * @param artifactName
\r
155 * The artifact Name
\r
156 * @param serviceUuid
\r
158 * @param resourceUuid
\r
159 * The resource UUID
\r
160 * @return The DCAE inventory for the artifact
\r
161 * @throws IOException
\r
162 * In case of issues with the stream
\r
163 * @throws ParseException
\r
164 * In case of issues with the Json parsing
\r
166 public String getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid)
\r
167 throws IOException, ParseException {
\r
168 Date startTime = new Date();
\r
169 LoggingUtils.setTargetContext("DCAE", "getDcaeInformation");
\r
170 String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid + "&typeName="
\r
172 String fullUrl = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types" + queryString;
\r
173 logger.info("Dcae Inventory Service full url - " + fullUrl);
\r
174 String daceInventoryResponse = null;
\r
175 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(fullUrl, "GET", null, null);
\r
176 JSONParser parser = new JSONParser();
\r
177 Object obj0 = parser.parse(responseStr);
\r
178 JSONObject jsonObj = (JSONObject) obj0;
\r
179 Long totalCount = (Long) jsonObj.get("totalCount");
\r
180 int numServices = totalCount.intValue();
\r
181 if (numServices == 0) {
\r
182 daceInventoryResponse = null;
\r
183 } else if (numServices > 0) {
\r
184 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
\r
185 JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);
\r
186 daceInventoryResponse = dcaeServiceType0.toString();
\r
187 logger.info(daceInventoryResponse);
\r
189 LoggingUtils.setResponseContext("0", "Get Dcae Information success", this.getClass().getName());
\r
190 LoggingUtils.setTimeContext(startTime, new Date());
\r
191 metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices);
\r
192 return daceInventoryResponse;
\r
196 * Inserts a new DCAEServiceType or updates an existing instance. If the
\r
197 * typeName is same second time(already exists) then the
\r
198 * DCAEServiceTypeRequest is updated
\r
200 * @param blueprintTemplate
\r
201 * blueprint content
\r
203 * owner of the data
\r
205 * The type/artifact Name
\r
206 * @param typeVersion
\r
208 * @param asdcServiceId
\r
210 * @param asdcResourceId
\r
212 * @return The DCAE inventory type id
\r
214 public String createupdateDCAEServiceType(String blueprintTemplate, String owner, String typeName, int typeVersion,
\r
215 String asdcServiceId, String asdcResourceId) {
\r
216 Date startTime = new Date();
\r
217 LoggingUtils.setTargetContext("DCAE", "createDCAEServiceType");
\r
218 String typeId = null;
\r
220 ObjectMapper mapper = new ObjectMapper();
\r
221 ObjectNode dcaeServiceTypeRequest = mapper.createObjectNode();
\r
222 dcaeServiceTypeRequest.put("blueprintTemplate", blueprintTemplate);
\r
223 dcaeServiceTypeRequest.put("owner", owner);
\r
224 dcaeServiceTypeRequest.put("typeName", typeName);
\r
225 dcaeServiceTypeRequest.put("typeVersion", typeVersion);
\r
226 dcaeServiceTypeRequest.put("asdcServiceId", asdcServiceId);
\r
227 dcaeServiceTypeRequest.put("asdcResourceId", asdcResourceId);
\r
228 String apiBodyString = dcaeServiceTypeRequest.toString();
\r
229 logger.info("Dcae api Body String - " + apiBodyString);
\r
230 String url = refProp.getStringValue(DCAE_INVENTORY_URL) + "/dcae-service-types";
\r
231 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, "POST", apiBodyString,
\r
232 "application/json");
\r
233 // If the DCAEServiceTypeRequest is created successfully it will
\r
234 // return a json object responce containing a node for newly created
\r
236 // The newly generated DCAEServiceTypeRequest can then be accessed
\r
237 // via URL: https://<DCAE_INVENTORY_URL>/dcae-service-types/<typeId>
\r
238 JSONParser parser = new JSONParser();
\r
239 Object obj0 = parser.parse(responseStr);
\r
240 JSONObject jsonObj = (JSONObject) obj0;
\r
241 typeId = (String) jsonObj.get("typeId"); // need to save this
\r
245 } catch (IOException | ParseException e) {
\r
246 logger.error("Exception occurred during createupdateDCAEServiceType Operation with DCAE", e);
\r
247 throw new BadRequestException("Exception occurred during createupdateDCAEServiceType Operation with DCAE",
\r
250 if (typeId != null) {
\r
251 LoggingUtils.setResponseContext("0", "Create update DCAE ServiceType success",
\r
252 this.getClass().getName());
\r
254 LoggingUtils.setResponseContext("900", "Create update DCAE ServiceType failed",
\r
255 this.getClass().getName());
\r
256 LoggingUtils.setErrorContext("900", "Create update DCAE ServiceType error");
\r
258 LoggingUtils.setTimeContext(startTime, new Date());
\r
259 metricsLogger.info("createupdateDCAEServiceType complete");
\r