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 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dg.util.impl;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.att.eelf.i18n.EELFResourceManager;
31 import java.util.HashMap;
32 import java.util.HashSet;
34 import java.util.Map.Entry;
36 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;
45 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);
48 private static final String RESOURCE_TYPE_PARAM = "resourceType";
49 private static final String RESOURCE_KEY_PARAM = "resourceKey";
50 private static final String PREFIX_PARAM = "prefix";
51 private static final String AAI_RESPONSE_STR = "AAIResponse: ";
52 private static final String GET_RESOURCE_RESULT = "getResource_result";
53 private static final String SUCCESS_PARAM = "SUCCESS";
54 private static final String RELATIONSHIP_DATA_LEN_PARAM = "relationship-data_length";
55 private static final String RELATIONSHIP_DATA_STR = "relationship-data[";
56 private static final String VNFF_VM_STR = "VNF.VM[";
57 private static final String VNF_VNFC_STR = "VNF.VNFC[";
58 private static final String GET_VNF_HIERARCHY_RESULT_PARAM = "getVnfHierarchy_result";
59 private static final String ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM = "Error Retrieving VNFC hierarchy";
60 private static final String RELATED_TO_PROPERTY_LEN_PARAM = "related-to-property_length";
61 public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
63 private AAIServiceFactory aaiServiceFactory;
65 public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
66 this.aaiServiceFactory = aaiServiceFactory;
70 * Method called in TestDG to test timeout scenario
72 * @param params waitTime time in millisecond DG is going to sleep
75 public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
77 String waitTime = params.get("waitTime");
79 logger.info("DG will waits for " + Long.parseLong(waitTime) + "milliseconds");
80 Thread.sleep(Long.parseLong(waitTime));
81 logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
82 } catch (InterruptedException e) {
83 logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = " + e
89 public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
90 String resourceType = params.get(RESOURCE_TYPE_PARAM);
91 String ctxPrefix = params.get(PREFIX_PARAM);
92 String resourceKey = params.get(RESOURCE_KEY_PARAM);
94 if (logger.isDebugEnabled()) {
95 logger.debug("inside getResorce");
96 logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
100 SvcLogicResource.QueryStatus response =
101 aaiServiceFactory.getAAIService().query(resourceType, false, null, resourceKey, ctxPrefix, null, ctx);
102 logger.info(AAI_RESPONSE_STR + response.toString());
103 ctx.setAttribute(GET_RESOURCE_RESULT, response.toString());
104 } catch (SvcLogicException e) {
105 logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, ""), e);
108 if (logger.isDebugEnabled()) {
109 logger.debug("exiting getResource======");
114 public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
115 String resourceType = params.get(RESOURCE_TYPE_PARAM);
116 String ctxPrefix = params.get(PREFIX_PARAM);
117 String resourceKey = params.get(RESOURCE_KEY_PARAM);
118 String attName = params.get("attributeName");
119 String attValue = params.get("attributeValue");
120 if (logger.isDebugEnabled()) {
121 logger.debug("inside postResource");
122 logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
123 logger.debug("Updating " + attName + " to : " + attValue);
125 Map<String, String> data = new HashMap<>();
126 data.put(attName, attValue);
129 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService()
130 .update(resourceType, resourceKey, data, ctxPrefix, ctx);
131 logger.info(AAI_RESPONSE_STR + response.toString());
132 ctx.setAttribute("postResource_result", response.toString());
133 } catch (SvcLogicException e) {
134 logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attValue), e);
137 if (logger.isDebugEnabled()) {
138 logger.debug("exiting postResource======");
143 public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
144 String resourceType = params.get(RESOURCE_TYPE_PARAM);
145 String resourceKey = params.get(RESOURCE_KEY_PARAM);
147 if (logger.isDebugEnabled()) {
148 logger.debug("inside deleteResource");
149 logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
153 SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService()
154 .delete(resourceType, resourceKey, ctx);
155 logger.info(AAI_RESPONSE_STR + response.toString());
156 ctx.setAttribute("deleteResource_result", response.toString());
157 } catch (SvcLogicException e) {
158 logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
160 if (logger.isDebugEnabled()) {
161 logger.debug("exiting deleteResource======");
166 public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
167 if (logger.isDebugEnabled()) {
168 logger.debug("Inside getVnfHierarchy======");
170 String resourceKey = params.get(RESOURCE_KEY_PARAM);
171 String retrivalVnfKey = "generic-vnf.vnf-id = '" + resourceKey + "'";
172 Map<String, String> paramsVnf = new HashMap<>();
173 paramsVnf.put(RESOURCE_TYPE_PARAM, "generic-vnf");
174 paramsVnf.put(PREFIX_PARAM, "vnfRetrived");
175 paramsVnf.put(RESOURCE_KEY_PARAM, retrivalVnfKey);
176 logger.debug("Retrieving VNF details from A&AI");
177 //Retrive all the relations of VNF
178 SvcLogicContext vnfCtx = new SvcLogicContext();
179 getResource(paramsVnf, vnfCtx);
180 if (vnfCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
181 trySetHeatStackIDAttribute(ctx, vnfCtx);
182 ctx.setAttribute("vnf.type", vnfCtx.getAttribute("vnfRetrived.vnf-type"));
183 Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
185 Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
189 logger.debug("Parsing Vserver details from VNF relations");
191 //loop through relationship-list data, to get vserver relations
192 for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
193 if (ctxKeySet.startsWith("vnfRetrived.") && "vserver"
194 .equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
195 String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
196 String vserverID = null;
197 String tenantID = null;
198 String cloudOwner = null;
199 String cloudRegionId = null;
200 int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);
203 < relationshipLength;
204 j++) { //loop inside relationship data, to get vserver-id and tenant-id
205 String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
206 String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
207 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
208 if ("vserver.vserver-id".equals(key)) {
211 if ("tenant.tenant-id".equals(key)) {
214 if ("cloud-region.cloud-owner".equals(key)) {
217 if ("cloud-region.cloud-region-id".equals(key)) {
218 cloudRegionId = value;
221 int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
223 j < relatedPropertyLength; j++) { //loop inside related-to-property data, to get vserver-name
224 String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
225 String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
226 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
228 //Retrive VM relations to find vnfc's
229 //VM to VNFC is 1 to 1 relation
230 String vmRetrivalKey = "vserver.vserver-id = '" + vserverID
231 + "' AND tenant.tenant_id = '" + tenantID
232 + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
233 + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
234 Map<String, String> paramsVm = new HashMap<>();
235 paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
236 paramsVm.put(PREFIX_PARAM, "vmRetrived");
237 paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
238 SvcLogicContext vmCtx = new SvcLogicContext();
240 logger.debug("Retrieving VM details from A&AI");
241 getResource(paramsVm, vmCtx);
242 if (vmCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
243 if (logger.isDebugEnabled()) {
244 logger.debug("Parsing VNFC details from VM relations");
246 vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
247 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
249 //loop through relationship-list data, to get vnfc relations
250 for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
251 if (ctxVnfcKeySet.startsWith("vmRetrived.")
252 && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
255 ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
257 relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
260 < relationshipLength;
261 j++) { //loop through relationship data, to get vnfc name
262 String key = vmCtx.getAttribute(
263 vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
264 String value = vmCtx.getAttribute(
265 vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
266 if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
267 vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
268 vmSet = resolveVmSet(vnfcHierarchyMap, value);
270 vnfcHierarchyMap.put(value, vmSet);
271 break; //VM to VNFC is 1 to 1 relation, once we got the VNFC name we can break the loop
277 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
278 vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
279 logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
280 + vmCtx.getAttribute(GET_RESOURCE_RESULT));
281 logger.warn("Incorrect or Incomplete VNF Hierarchy");
282 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
287 vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
289 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
291 //code changes for getting vnfcs hirearchy
292 populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
294 ctx.setAttribute("VNF.VNFCCount",
295 Integer.toString(vnfcHierarchyMap.size()));
296 //code changes for getting vnfcs hirearchy
297 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, SUCCESS_PARAM);
298 //Finally set all attributes to ctx
299 for (Entry<String, String> entry: vnfHierarchyMap.entrySet()) {
300 ctx.setAttribute(entry.getKey(), entry.getValue());
303 ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
304 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
305 logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
306 .getAttribute(GET_RESOURCE_RESULT));
307 logger.warn("Incorrect or Incomplete VNF Hierarchy");
308 throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
311 if (logger.isDebugEnabled()) {
312 logger.debug("exiting getVnfHierarchy======");
316 private void trySetHeatStackIDAttribute(SvcLogicContext ctx, SvcLogicContext vnfCtx) {
317 if (vnfCtx.getAttribute("vnfRetrived.heat-stack-id") != null) {
318 ctx.setAttribute("VNF.heat-stack-id", vnfCtx.getAttribute("vnfRetrived.heat-stack-id"));
322 private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {
324 Set<String> vmSet = vnfcHierarchyMap.get(value);
326 vmSet = new HashSet<>();
331 private int getAttribute(SvcLogicContext ctx, String key, String param) {
332 if (ctx.getAttributeKeySet().contains(key + param)) {
333 return Integer.parseInt(ctx.getAttribute(key + param));
338 void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
339 throws APPCException {
340 SvcLogicContext vnfcCtx = new SvcLogicContext();
342 for (Entry<String, Set<String>> entry : vnfcHierarchyMap.entrySet()) {
343 String vnfcRetrivalKey = "vnfc-name = '" + entry.getKey() + "'";
344 Map<String, String> paramsVnfc = new HashMap<>();
345 paramsVnfc.put(RESOURCE_TYPE_PARAM, "vnfc");
346 paramsVnfc.put(PREFIX_PARAM, "vnfcRetrived");
347 paramsVnfc.put(RESOURCE_KEY_PARAM, vnfcRetrivalKey);
349 logger.debug("Retrieving VM details from A&AI");
350 getResource(paramsVnfc, vnfcCtx);
351 if (vnfcCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
352 if (logger.isDebugEnabled()){
353 logger.debug("Parsing VNFC details from VM relations");
355 //putting required values in the map
356 //vnf.vnfc[vnfcIndex].type
357 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE",
358 vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
360 // vnf.vnfc[vnfcIndex].name
361 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME",
362 vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
364 //vnf.vnfc[vnfcIndex].vmCount
365 Set<String> vmSet = entry.getValue();
366 String vmCountinVnfcs = Integer.toString(vmSet.size());
367 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT",
370 for (String vmURL : vmSet) {
371 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);