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 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.dg.util.impl;
30 import java.util.HashMap;
31 import java.util.HashSet;
33 import java.util.Map.Entry;
35 import java.util.concurrent.ConcurrentHashMap;
37 import org.onap.appc.dg.util.ExecuteNodeAction;
38 import org.onap.appc.exceptions.APPCException;
39 import org.onap.appc.i18n.Msg;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import com.att.eelf.i18n.EELFResourceManager;
48 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
50 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);
51 private static final String RESOURCE_TYPE_PARAM = "resourceType";
52 private static final String RESOURCE_KEY_PARAM = "resourceKey";
53 private static final String PREFIX_PARAM = "prefix";
54 private static final String AAI_RESPONSE_STR = "AAIResponse: ";
55 private static final String GET_RESOURCE_RESULT = "getResource_result";
56 private static final String SUCCESS_PARAM = "SUCCESS";
57 private static final String RELATIONSHIP_DATA_LEN_PARAM = "relationship-data_length";
58 private static final String RELATIONSHIP_DATA_STR = "relationship-data[";
59 private static final String VNFF_VM_STR = "VNF.VM[";
60 private static final String VNF_VNFC_STR = "VNF.VNFC[";
61 private static final String GET_VNF_HIERARCHY_RESULT_PARAM = "getVnfHierarchy_result";
62 private static final String ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM = "Error Retrieving VNFC hierarchy";
63 private static final String RELATED_TO_PROPERTY_LEN_PARAM = "related-to-property_length";
64 public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
65 private static Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
67 private static Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
68 private static int vmCount = 0;
69 private static Set<String> vmSet;
70 private static String vmURL;
71 private AAIServiceFactory aaiServiceFactory;
73 public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
74 this.aaiServiceFactory = aaiServiceFactory;
78 * Method called in TestDG to test timeout scenario
81 * waitTime time in millisecond DG is going to sleep
84 public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
86 String waitTime = params.get("waitTime");
88 logger.info("DG will waits for " + Long.parseLong(waitTime) + "milliseconds");
89 Thread.sleep(Long.parseLong(waitTime));
90 logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
91 } catch (InterruptedException e) {
92 logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = "
98 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
99 String resourceType = params.get(RESOURCE_TYPE_PARAM);
100 String ctxPrefix = params.get(PREFIX_PARAM);
101 String resourceKey = params.get(RESOURCE_KEY_PARAM);
103 if (logger.isDebugEnabled()) {
104 logger.debug("inside getResorce");
105 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
109 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().query(resourceType, false, null,
110 resourceKey, ctxPrefix, null, ctx);
111 logger.info(AAI_RESPONSE_STR + response.toString());
112 ctx.setAttribute(GET_RESOURCE_RESULT, response.toString());
113 } catch (SvcLogicException e) {
114 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, ""), e);
117 if (logger.isDebugEnabled()) {
118 logger.debug("exiting getResource======");
123 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
124 String resourceType = params.get(RESOURCE_TYPE_PARAM);
125 String ctxPrefix = params.get(PREFIX_PARAM);
126 String resourceKey = params.get(RESOURCE_KEY_PARAM);
127 String attName = params.get("attributeName");
128 String attValue = params.get("attributeValue");
129 if (logger.isDebugEnabled()) {
130 logger.debug("inside postResource");
131 logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
132 logger.debug("Updating " + attName + " to : " + attValue);
134 Map<String, String> data = new HashMap<>();
135 data.put(attName, attValue);
138 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().update(resourceType, resourceKey,
139 data, ctxPrefix, ctx);
140 logger.info(AAI_RESPONSE_STR + response.toString());
141 ctx.setAttribute("postResource_result", response.toString());
142 } catch (SvcLogicException e) {
143 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attValue), e);
146 if (logger.isDebugEnabled()) {
147 logger.debug("exiting postResource======");
152 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
153 String resourceType = params.get(RESOURCE_TYPE_PARAM);
154 String resourceKey = params.get(RESOURCE_KEY_PARAM);
156 if (logger.isDebugEnabled()) {
157 logger.debug("inside deleteResource");
158 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
162 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
164 logger.info(AAI_RESPONSE_STR + response.toString());
165 ctx.setAttribute("deleteResource_result", response.toString());
166 } catch (SvcLogicException e) {
167 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
169 if (logger.isDebugEnabled()) {
170 logger.debug("exiting deleteResource======");
174 private void getVserverRelations(SvcLogicContext vnfCtx, SvcLogicContext ctx) throws APPCException {
176 logger.debug("Parsing Vserver details from VNF relations");
177 for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
178 if (ctxKeySet.startsWith("vnfRetrived.") && "vserver".equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
179 String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
180 String vserverID = null;
181 String tenantID = null;
182 String cloudOwner = null;
183 String cloudRegionId = null;
184 int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);
186 for (int j = 0; j < relationshipLength; j++) { // loop inside
191 String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
192 String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
193 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
194 if ("vserver.vserver-id".equals(key)) {
197 if ("tenant.tenant-id".equals(key)) {
200 if ("cloud-region.cloud-owner".equals(key)) {
203 if ("cloud-region.cloud-region-id".equals(key)) {
204 cloudRegionId = value;
207 int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
208 for (int j = 0; j < relatedPropertyLength; j++) { // loop inside
209 // related-to-property
213 String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
214 String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
215 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
217 // Retrive VM relations to find vnfc's
218 // VM to VNFC is 1 to 1 relation
219 String vmRetrivalKey = "vserver.vserver-id = '" + vserverID + "' AND tenant.tenant_id = '" + tenantID
220 + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
221 + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
222 Map<String, String> paramsVm = new HashMap<>();
223 paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
224 paramsVm.put(PREFIX_PARAM, "vmRetrived");
225 paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
226 SvcLogicContext vmCtx = new SvcLogicContext();
228 logger.debug("Retrieving VM details from A&AI");
229 getResource(paramsVm, vmCtx);
230 if ((SUCCESS_PARAM).equals(vmCtx.getAttribute(GET_RESOURCE_RESULT))) {
231 if (logger.isDebugEnabled()) {
232 logger.debug("Parsing VNFC details from VM relations");
234 vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
235 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
237 // loop through relationship-list data, to get vnfc
239 for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
240 if (ctxVnfcKeySet.startsWith("vmRetrived.")
241 && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
243 String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
245 relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
247 for (int j = 0; j < relationshipLength; j++) { // loop
256 .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
258 .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
259 if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
260 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
261 vmSet = resolveVmSet(vnfcHierarchyMap, value);
263 vnfcHierarchyMap.put(value, vmSet);
264 break; // VM to VNFC is 1 to 1 relation,
265 // once we got the VNFC name we can
272 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
273 vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
274 logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
275 + vmCtx.getAttribute(GET_RESOURCE_RESULT));
276 logger.warn("Incorrect or Incomplete VNF Hierarchy");
277 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
285 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
286 if (logger.isDebugEnabled()) {
287 logger.debug("Inside getVnfHierarchy======");
289 String resourceKey = params.get(RESOURCE_KEY_PARAM);
290 String retrivalVnfKey = "generic-vnf.vnf-id = '" + resourceKey + "'";
291 Map<String, String> paramsVnf = new HashMap<>();
292 paramsVnf.put(RESOURCE_TYPE_PARAM, "generic-vnf");
293 paramsVnf.put(PREFIX_PARAM, "vnfRetrived");
294 paramsVnf.put(RESOURCE_KEY_PARAM, retrivalVnfKey);
295 logger.debug("Retrieving VNF details from A&AI");
296 // Retrive all the relations of VNF
297 SvcLogicContext vnfCtx = new SvcLogicContext();
298 getResource(paramsVnf, vnfCtx);
299 if (vnfCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
300 trySetHeatStackIDAttribute(ctx, vnfCtx);
301 ctx.setAttribute("vnf.type", vnfCtx.getAttribute("vnfRetrived.vnf-type"));
303 // loop through relationship-list data, to get vserver relations
304 getVserverRelations(vnfCtx, ctx);
305 vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
307 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
309 // code changes for getting vnfcs hirearchy
310 populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
312 ctx.setAttribute("VNF.VNFCCount", Integer.toString(vnfcHierarchyMap.size()));
313 // code changes for getting vnfcs hirearchy
314 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, SUCCESS_PARAM);
315 // Finally set all attributes to ctx
316 for (Entry<String, String> entry : vnfHierarchyMap.entrySet()) {
317 ctx.setAttribute(entry.getKey(), entry.getValue());
320 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
321 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
322 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: "
323 + ctx.getAttribute(GET_RESOURCE_RESULT));
324 logger.warn("Incorrect or Incomplete VNF Hierarchy");
325 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
328 if (logger.isDebugEnabled()) {
329 logger.debug("exiting getVnfHierarchy======");
333 private void trySetHeatStackIDAttribute(SvcLogicContext ctx, SvcLogicContext vnfCtx) {
334 if (vnfCtx.getAttribute("vnfRetrived.heat-stack-id") != null) {
335 ctx.setAttribute("VNF.heat-stack-id", vnfCtx.getAttribute("vnfRetrived.heat-stack-id"));
339 private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {
341 Set<String> vmSet = vnfcHierarchyMap.get(value);
343 vmSet = new HashSet<>();
348 private int getAttribute(SvcLogicContext ctx, String key, String param) {
349 if (ctx.getAttributeKeySet().contains(key + param)) {
350 return Integer.parseInt(ctx.getAttribute(key + param));
355 void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
356 throws APPCException {
357 SvcLogicContext vnfcCtx = new SvcLogicContext();
359 for (Entry<String, Set<String>> entry : vnfcHierarchyMap.entrySet()) {
360 String vnfcRetrivalKey = "vnfc-name = '" + entry.getKey() + "'";
361 Map<String, String> paramsVnfc = new HashMap<>();
362 paramsVnfc.put(RESOURCE_TYPE_PARAM, "vnfc");
363 paramsVnfc.put(PREFIX_PARAM, "vnfcRetrived");
364 paramsVnfc.put(RESOURCE_KEY_PARAM, vnfcRetrivalKey);
366 logger.debug("Retrieving VM details from A&AI");
367 getResource(paramsVnfc, vnfcCtx);
368 if (vnfcCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
369 if (logger.isDebugEnabled()) {
370 logger.debug("Parsing VNFC details from VM relations");
372 // putting required values in the map
373 // vnf.vnfc[vnfcIndex].type
374 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE", vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
376 // vnf.vnfc[vnfcIndex].name
377 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
379 // vnf.vnfc[vnfcIndex].vmCount
380 Set<String> vmSet = entry.getValue();
381 String vmCountinVnfcs = Integer.toString(vmSet.size());
382 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT", vmCountinVnfcs);
384 for (String vmURL : vmSet) {
385 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);