33b9866dbd0b6acecad25ccbb98b75b8848c383c
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
16  * 
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  * 
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.
24  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.dg.util.impl;
29
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35 import java.util.concurrent.ConcurrentHashMap;
36
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;
43
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import com.att.eelf.i18n.EELFResourceManager;
47
48 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
49
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<>();
66
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;
72
73     public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
74         this.aaiServiceFactory = aaiServiceFactory;
75     }
76
77     /**
78      * Method called in TestDG to test timeout scenario
79      *
80      * @param params
81      *            waitTime time in millisecond DG is going to sleep
82      */
83     @Override
84     public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
85         try {
86             String waitTime = params.get("waitTime");
87
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 = "
93                     + e.getMessage());
94         }
95     }
96
97     @Override
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);
102
103         if (logger.isDebugEnabled()) {
104             logger.debug("inside getResorce");
105             logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
106         }
107
108         try {
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);
115         }
116
117         if (logger.isDebugEnabled()) {
118             logger.debug("exiting getResource======");
119         }
120     }
121
122     @Override
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);
133         }
134         Map<String, String> data = new HashMap<>();
135         data.put(attName, attValue);
136
137         try {
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);
144         }
145
146         if (logger.isDebugEnabled()) {
147             logger.debug("exiting postResource======");
148         }
149     }
150
151     @Override
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);
155
156         if (logger.isDebugEnabled()) {
157             logger.debug("inside deleteResource");
158             logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
159         }
160
161         try {
162             SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
163                     ctx);
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);
168         }
169         if (logger.isDebugEnabled()) {
170             logger.debug("exiting deleteResource======");
171         }
172     }
173
174     private void getVserverRelations(SvcLogicContext vnfCtx, SvcLogicContext ctx) throws APPCException {
175
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);
185
186                 for (int j = 0; j < relationshipLength; j++) { // loop inside
187                                                                 // relationship
188                                                                 // data, to get
189                                                                 // vserver-id
190                                                                 // and tenant-id
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)) {
195                         vserverID = value;
196                     }
197                     if ("tenant.tenant-id".equals(key)) {
198                         tenantID = value;
199                     }
200                     if ("cloud-region.cloud-owner".equals(key)) {
201                         cloudOwner = value;
202                     }
203                     if ("cloud-region.cloud-region-id".equals(key)) {
204                         cloudRegionId = value;
205                     }
206                 }
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
210                                                                     // data, to
211                                                                     // get
212                                                                     // vserver-name
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);
216                 }
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();
227
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");
233                     }
234                     vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
235                     vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
236
237                     // loop through relationship-list data, to get vnfc
238                     // relations
239                     for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
240                         if (ctxVnfcKeySet.startsWith("vmRetrived.")
241                                 && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
242
243                             String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
244
245                             relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
246
247                             for (int j = 0; j < relationshipLength; j++) { // loop
248                                                                             // through
249                                                                             // relationship
250                                                                             // data,
251                                                                             // to
252                                                                             // get
253                                                                             // vnfc
254                                                                             // name
255                                 String key = vmCtx
256                                         .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
257                                 String value = vmCtx
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);
262                                     vmSet.add(vmURL);
263                                     vnfcHierarchyMap.put(value, vmSet);
264                                     break; // VM to VNFC is 1 to 1 relation,
265                                             // once we got the VNFC name we can
266                                             // break the loop
267                                 }
268                             }
269                         }
270                     }
271                 } else {
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);
278                 }
279                 vmCount++;
280             }
281         }
282     }
283
284     @Override
285     public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
286         if (logger.isDebugEnabled()) {
287             logger.debug("Inside getVnfHierarchy======");
288         }
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"));
302
303             // loop through relationship-list data, to get vserver relations
304             getVserverRelations(vnfCtx, ctx);
305             vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
306             if (vmCount == 0) {
307                 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
308             }
309             // code changes for getting vnfcs hirearchy
310             populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
311             // vnf,vnfcCount
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());
318             }
319         } else {
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);
326         }
327
328         if (logger.isDebugEnabled()) {
329             logger.debug("exiting getVnfHierarchy======");
330         }
331     }
332
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"));
336         }
337     }
338
339     private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {
340
341         Set<String> vmSet = vnfcHierarchyMap.get(value);
342         if (vmSet == null) {
343             vmSet = new HashSet<>();
344         }
345         return vmSet;
346     }
347
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));
351         }
352         return 0;
353     }
354
355     void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
356             throws APPCException {
357         SvcLogicContext vnfcCtx = new SvcLogicContext();
358         int vnfcCounter = 0;
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);
365
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");
371                 }
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"));
375
376                 // vnf.vnfc[vnfcIndex].name
377                 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
378
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);
383                 int vmCount = 0;
384                 for (String vmURL : vmSet) {
385                     ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);
386                 }
387
388             }
389             vnfcCounter++;
390         }
391     }
392 }