2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 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 java.io.BufferedReader;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStreamReader;
\r
29 import java.net.HttpURLConnection;
\r
30 import java.net.URL;
\r
31 import java.util.List;
\r
33 import javax.ws.rs.BadRequestException;
\r
35 import org.json.simple.JSONArray;
\r
36 import org.json.simple.JSONObject;
\r
37 import org.json.simple.parser.JSONParser;
\r
38 import org.json.simple.parser.ParseException;
\r
39 import org.onap.clamp.clds.dao.CldsDao;
\r
40 import org.onap.clamp.clds.model.CldsEvent;
\r
41 import org.onap.clamp.clds.model.CldsModel;
\r
42 import org.onap.clamp.clds.model.DcaeEvent;
\r
43 import org.onap.clamp.clds.model.prop.Global;
\r
44 import org.onap.clamp.clds.model.prop.ModelProperties;
\r
45 import org.onap.clamp.clds.model.refprop.RefProp;
\r
46 import org.springframework.beans.factory.annotation.Autowired;
\r
48 import com.att.eelf.configuration.EELFLogger;
\r
49 import com.att.eelf.configuration.EELFManager;
\r
50 import com.fasterxml.jackson.core.JsonProcessingException;
\r
52 public class DcaeInventoryServices {
\r
53 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class);
\r
54 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
\r
57 private RefProp refProp;
\r
60 private CldsDao cldsDao;
\r
63 private SdcCatalogServices sdcCatalogServices;
\r
65 public void setEventInventory(CldsModel cldsModel, String userId) throws Exception {
\r
66 String artifactName = cldsModel.getControlName();
\r
67 DcaeEvent dcaeEvent = new DcaeEvent();
\r
68 String isDcaeInfoAvailable = null;
\r
69 if (artifactName != null) {
\r
70 artifactName = artifactName + ".yml";
\r
74 * Below are the properties required for calling the dcae inventory
\r
77 ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false, "{}",
\r
78 cldsModel.getPropText());
\r
79 Global global = prop.getGlobal();
\r
80 String invariantServiceUuid = global.getService();
\r
81 List<String> resourceUuidList = global.getResourceVf();
\r
82 String serviceUuid = sdcCatalogServices.getServiceUuidFromServiceInvariantId(invariantServiceUuid);
\r
83 String resourceUuid = "";
\r
84 if (resourceUuidList != null && resourceUuidList.size() > 0) {
\r
85 resourceUuid = resourceUuidList.get(0).toString();
\r
87 /* Invemtory service url is called in this method */
\r
88 isDcaeInfoAvailable = getDcaeInformation(artifactName, serviceUuid, resourceUuid);
\r
90 /* set dcae events */
\r
91 dcaeEvent.setArtifactName(artifactName);
\r
92 dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION);
\r
94 } catch (JsonProcessingException e) {
\r
96 logger.error("JsonProcessingException" + e);
\r
97 } catch (IOException e) {
\r
100 logger.error("IOException :" + e);
\r
102 /* Null whether the DCAE has items lenght or not */
\r
103 if (isDcaeInfoAvailable != null) {
\r
104 /* Inserting Event in to DB */
\r
105 logger.info(isDcaeInfoAvailable);
\r
106 JSONParser parser = new JSONParser();
\r
107 Object obj0 = parser.parse(isDcaeInfoAvailable);
\r
108 JSONObject jsonObj = (JSONObject) obj0;
\r
109 String oldTypeId = cldsModel.getTypeId();
\r
110 String newTypeId = "";
\r
111 if (jsonObj.get("typeId") != null) {
\r
112 newTypeId = jsonObj.get("typeId").toString();
\r
113 cldsModel.setTypeId(jsonObj.get("typeId").toString());
\r
115 // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml");
\r
116 if (jsonObj.get("typeName") != null) {
\r
117 cldsModel.setTypeName(jsonObj.get("typeName").toString());
\r
119 if(oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)){
\r
120 CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(),
\r
121 CldsEvent.ACTION_STATE_RECEIVED, null);
\r
123 cldsModel.save(cldsDao, userId);
\r
125 logger.info(cldsModel.getName() + " Model is not present in Dcae Inventory Service.");
\r
129 public String getDcaeInformation(String artifactName, String serviceUUID, String resourceUUID)
\r
130 throws IOException, ParseException {
\r
131 String queryString = "?sdcResourceId=" + resourceUUID + "&sdcServiceId=" + serviceUUID + "&typeName="
\r
133 String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString;
\r
134 logger.info("Dcae Inventory Service full url - " + fullUrl);
\r
135 String daceInventoryResponse = null;
\r
136 URL inventoryUrl = new URL(fullUrl);
\r
138 HttpURLConnection conn = (HttpURLConnection) inventoryUrl.openConnection();
\r
139 conn.setRequestMethod("GET");
\r
140 boolean requestFailed = true;
\r
141 int responseCode = conn.getResponseCode();
\r
142 if (responseCode == 200) {
\r
143 requestFailed = false;
\r
146 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
\r
147 String inputLine = null;
\r
148 StringBuffer response = new StringBuffer();
\r
149 String responseStr = null;
\r
150 while ((inputLine = in.readLine()) != null) {
\r
151 response.append(inputLine);
\r
154 responseStr = response.toString();
\r
155 if (responseStr != null) {
\r
156 if (requestFailed) {
\r
157 logger.error("requestFailed - responseStr=" + response);
\r
158 throw new BadRequestException(responseStr);
\r
161 String jsonResponseString = response.toString();
\r
162 JSONParser parser = new JSONParser();
\r
163 Object obj0 = parser.parse(jsonResponseString);
\r
165 JSONObject jsonObj = (JSONObject) obj0;
\r
167 Long totalCount = (Long) jsonObj.get("totalCount");
\r
169 int numServices = totalCount.intValue();
\r
170 if (numServices == 0) {
\r
171 daceInventoryResponse = null;
\r
172 } else if (numServices > 0) {
\r
173 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
\r
174 JSONObject dcaeServiceType0 = (JSONObject) itemsArray.get(0);
\r
175 daceInventoryResponse = dcaeServiceType0.toString();
\r
176 logger.info(daceInventoryResponse.toString());
\r
178 return daceInventoryResponse;
\r