2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2018 Nokia
10 * ================================================================================
11 * Modifications Copyright (C) 2018 IBM.
12 * ================================================================================
13 * Modifications Copyright (C) 2019 Ericsson
14 * ================================================================================
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
27 * ============LICENSE_END=========================================================
30 package org.onap.appc.dg.util.impl;
32 import java.util.HashMap;
33 import java.util.HashSet;
35 import java.util.Map.Entry;
37 import java.util.concurrent.ConcurrentHashMap;
39 import org.onap.appc.dg.util.ExecuteNodeAction;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.i18n.Msg;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import com.att.eelf.i18n.EELFResourceManager;
50 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
52 private static final String RESOURCE_TYPE_PARAM = "resourceType";
53 private static final String RESOURCE_KEY_PARAM = "resourceKey";
54 private static final String PREFIX_PARAM = "prefix";
55 private static final String AAI_RESPONSE_STR = "AAIResponse: ";
56 private static final String GET_RESOURCE_RESULT = "getResource_result";
57 private static final String SUCCESS_PARAM = "SUCCESS";
58 private static final String RELATIONSHIP_DATA_LEN_PARAM = "relationship-data_length";
59 private static final String RELATIONSHIP_DATA_STR = "relationship-data[";
60 private static final String VNFF_VM_STR = "VNF.VM[";
61 private static final String VNF_VNFC_STR = "VNF.VNFC[";
62 private static final String GET_VNF_HIERARCHY_RESULT_PARAM = "getVnfHierarchy_result";
63 private static final String ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM = "Error Retrieving VNFC hierarchy";
64 private static final String RELATED_TO_PROPERTY_LEN_PARAM = "related-to-property_length";
65 public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
66 private static EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);
67 private static Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
69 private static Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
70 private static int vmCount = 0;
71 private static Set<String> vmSet;
72 private static String vmURL;
73 private AAIServiceFactory aaiServiceFactory;
75 public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
76 this.aaiServiceFactory = aaiServiceFactory;
80 * Method called in TestDG to test timeout scenario
83 * waitTime time in millisecond DG is going to sleep
86 public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
88 String waitTime = params.get("waitTime");
90 logger.info("DG will waits for " + Long.parseLong(waitTime) + "milliseconds");
91 Thread.sleep(Long.parseLong(waitTime));
92 logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
93 } catch (InterruptedException e) {
94 logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = "
96 Thread.currentThread().interrupt();
101 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
102 String resourceType = params.get(RESOURCE_TYPE_PARAM);
103 String ctxPrefix = params.get(PREFIX_PARAM);
104 String resourceKey = params.get(RESOURCE_KEY_PARAM);
106 if (logger.isDebugEnabled()) {
107 logger.debug("inside getResource");
108 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
112 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().query(resourceType, false, null,
113 resourceKey, ctxPrefix, null, ctx);
114 logger.info(AAI_RESPONSE_STR + response.toString());
115 ctx.setAttribute(GET_RESOURCE_RESULT, response.toString());
116 } catch (SvcLogicException e) {
117 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, ""), e);
120 if (logger.isDebugEnabled()) {
121 logger.debug("exiting getResource======");
126 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
127 String resourceType = params.get(RESOURCE_TYPE_PARAM);
128 String ctxPrefix = params.get(PREFIX_PARAM);
129 String resourceKey = params.get(RESOURCE_KEY_PARAM);
130 String attName = params.get("attributeName");
131 String attValue = params.get("attributeValue");
132 if (logger.isDebugEnabled()) {
133 logger.debug("inside postResource");
134 logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
135 logger.debug("Updating " + attName + " to : " + attValue);
137 Map<String, String> data = new HashMap<>();
138 data.put(attName, attValue);
141 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().update(resourceType, resourceKey,
142 data, ctxPrefix, ctx);
143 logger.info(AAI_RESPONSE_STR + response.toString());
144 ctx.setAttribute("postResource_result", response.toString());
145 } catch (SvcLogicException e) {
146 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attValue), e);
149 if (logger.isDebugEnabled()) {
150 logger.debug("exiting postResource======");
155 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
156 String resourceType = params.get(RESOURCE_TYPE_PARAM);
157 String resourceKey = params.get(RESOURCE_KEY_PARAM);
159 if (logger.isDebugEnabled()) {
160 logger.debug("inside deleteResource");
161 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
165 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
167 logger.info(AAI_RESPONSE_STR + response.toString());
168 ctx.setAttribute("deleteResource_result", response.toString());
169 } catch (SvcLogicException e) {
170 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
172 if (logger.isDebugEnabled()) {
173 logger.debug("exiting deleteResource======");
177 private void getVserverRelations(SvcLogicContext vnfCtx, SvcLogicContext ctx) throws APPCException {
179 logger.debug("Parsing Vserver details from VNF relations");
180 for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
181 if (ctxKeySet.startsWith("vnfRetrived.") && "vserver".equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
182 String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
183 String vserverID = null;
184 String tenantID = null;
185 String cloudOwner = null;
186 String cloudRegionId = null;
187 int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);
189 for (int j = 0; j < relationshipLength; j++) { // loop inside
194 String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
195 String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
197 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
198 if ("vserver.vserver-id".equals(key)) {
201 if ("tenant.tenant-id".equals(key)) {
204 if ("cloud-region.cloud-owner".equals(key)) {
207 if ("cloud-region.cloud-region-id".equals(key)) {
208 cloudRegionId = value;
211 int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
212 for (int j = 0; j < relatedPropertyLength; j++) { // loop inside
213 // related-to-property
217 String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
218 String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
219 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
221 // Retrive VM relations to find vnfc's
222 // VM to VNFC is 1 to 1 relation
223 String vmRetrivalKey = "vserver.vserver-id = '" + vserverID + "' AND tenant.tenant_id = '" + tenantID
224 + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
225 + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
226 Map<String, String> paramsVm = new HashMap<>();
227 paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
228 paramsVm.put(PREFIX_PARAM, "vmRetrived");
229 paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
230 SvcLogicContext vmCtx = getSvcLogicContext();
232 logger.debug("Retrieving VM details from A&AI");
233 getResource(paramsVm, vmCtx);
234 if ((SUCCESS_PARAM).equals(vmCtx.getAttribute(GET_RESOURCE_RESULT))) {
235 if (logger.isDebugEnabled()) {
236 logger.debug("Parsing VNFC details from VM relations");
238 vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
239 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
241 // loop through relationship-list data, to get vnfc
243 for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
244 if (ctxVnfcKeySet.startsWith("vmRetrived.")
245 && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
247 String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
249 relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
251 for (int j = 0; j < relationshipLength; j++) { // loop
260 .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
262 .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
263 if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
264 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
265 vmSet = resolveVmSet(vnfcHierarchyMap, value);
267 vnfcHierarchyMap.put(value, vmSet);
268 break; // VM to VNFC is 1 to 1 relation,
269 // once we got the VNFC name we can
276 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
277 vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
278 logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
279 + vmCtx.getAttribute(GET_RESOURCE_RESULT));
280 logger.warn("Incorrect or Incomplete VNF Hierarchy");
281 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
289 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
290 if (logger.isDebugEnabled()) {
291 logger.debug("Inside getVnfHierarchy======");
293 String resourceKey = params.get(RESOURCE_KEY_PARAM);
294 String retrivalVnfKey = "generic-vnf.vnf-id = '" + resourceKey + "'";
295 Map<String, String> paramsVnf = new HashMap<>();
296 paramsVnf.put(RESOURCE_TYPE_PARAM, "generic-vnf");
297 paramsVnf.put(PREFIX_PARAM, "vnfRetrived");
298 paramsVnf.put(RESOURCE_KEY_PARAM, retrivalVnfKey);
299 logger.debug("Retrieving VNF details from A&AI");
300 // Retrive all the relations of VNF
301 SvcLogicContext vnfCtx = getSvcLogicContext();
302 getResource(paramsVnf, vnfCtx);
303 if (vnfCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
304 trySetHeatStackIDAttribute(ctx, vnfCtx);
305 ctx.setAttribute("vnf.type", vnfCtx.getAttribute("vnfRetrived.vnf-type"));
307 // loop through relationship-list data, to get vserver relations
308 getVserverRelations(vnfCtx, ctx);
309 vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
311 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
313 // code changes for getting vnfcs hirearchy
314 populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
316 ctx.setAttribute("VNF.VNFCCount", Integer.toString(vnfcHierarchyMap.size()));
317 // code changes for getting vnfcs hirearchy
318 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, SUCCESS_PARAM);
319 // Finally set all attributes to ctx
320 for (Entry<String, String> entry : vnfHierarchyMap.entrySet()) {
321 ctx.setAttribute(entry.getKey(), entry.getValue());
324 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
325 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
326 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: "
327 + ctx.getAttribute(GET_RESOURCE_RESULT));
328 logger.warn("Incorrect or Incomplete VNF Hierarchy");
329 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
332 if (logger.isDebugEnabled()) {
333 logger.debug("exiting getVnfHierarchy======");
337 private void trySetHeatStackIDAttribute(SvcLogicContext ctx, SvcLogicContext vnfCtx) {
338 if (vnfCtx.getAttribute("vnfRetrived.heat-stack-id") != null) {
339 ctx.setAttribute("VNF.heat-stack-id", vnfCtx.getAttribute("vnfRetrived.heat-stack-id"));
343 private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {
345 Set<String> vmSet = vnfcHierarchyMap.get(value);
347 vmSet = new HashSet<>();
352 private int getAttribute(SvcLogicContext ctx, String key, String param) {
353 if (ctx.getAttributeKeySet().contains(key + param)) {
354 return Integer.parseInt(ctx.getAttribute(key + param));
359 void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
360 throws APPCException {
361 SvcLogicContext vnfcCtx = new SvcLogicContext();
363 for (Entry<String, Set<String>> entry : vnfcHierarchyMap.entrySet()) {
364 String vnfcRetrivalKey = "vnfc-name = '" + entry.getKey() + "'";
365 Map<String, String> paramsVnfc = new HashMap<>();
366 paramsVnfc.put(RESOURCE_TYPE_PARAM, "vnfc");
367 paramsVnfc.put(PREFIX_PARAM, "vnfcRetrived");
368 paramsVnfc.put(RESOURCE_KEY_PARAM, vnfcRetrivalKey);
370 logger.debug("Retrieving VM details from A&AI");
371 getResource(paramsVnfc, vnfcCtx);
372 if (vnfcCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
373 if (logger.isDebugEnabled()) {
374 logger.debug("Parsing VNFC details from VM relations");
376 // putting required values in the map
377 // vnf.vnfc[vnfcIndex].type
378 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE", vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
380 // vnf.vnfc[vnfcIndex].name
381 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
383 // vnf.vnfc[vnfcIndex].vmCount
384 Set<String> vmSet = entry.getValue();
385 String vmCountinVnfcs = Integer.toString(vmSet.size());
386 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT", vmCountinVnfcs);
388 for (String vmURL : vmSet) {
389 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);
397 protected SvcLogicContext getSvcLogicContext() {
398 return new SvcLogicContext();