4f517d211f9d30752f4f098eeb69537e555c1cdc
[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  * 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
18  * 
19  *      http://www.apache.org/licenses/LICENSE-2.0
20  * 
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.
26  * 
27  * ============LICENSE_END=========================================================
28  */
29
30 package org.onap.appc.dg.util.impl;
31
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37 import java.util.concurrent.ConcurrentHashMap;
38
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;
45
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import com.att.eelf.i18n.EELFResourceManager;
49
50 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
51
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<>();
68
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;
74
75     public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
76         this.aaiServiceFactory = aaiServiceFactory;
77     }
78
79     /**
80      * Method called in TestDG to test timeout scenario
81      *
82      * @param params
83      *            waitTime time in millisecond DG is going to sleep
84      */
85     @Override
86     public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
87         try {
88             String waitTime = params.get("waitTime");
89
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 = "
95                     + e.getMessage(), e);
96             Thread.currentThread().interrupt();
97         }
98     }
99
100     @Override
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);
105
106         if (logger.isDebugEnabled()) {
107             logger.debug("inside getResource");
108             logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
109         }
110
111         try {
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);
118         }
119
120         if (logger.isDebugEnabled()) {
121             logger.debug("exiting getResource======");
122         }
123     }
124
125     @Override
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);
136         }
137         Map<String, String> data = new HashMap<>();
138         data.put(attName, attValue);
139
140         try {
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);
147         }
148
149         if (logger.isDebugEnabled()) {
150             logger.debug("exiting postResource======");
151         }
152     }
153
154     @Override
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);
158
159         if (logger.isDebugEnabled()) {
160             logger.debug("inside deleteResource");
161             logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
162         }
163
164         try {
165             SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
166                     ctx);
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);
171         }
172         if (logger.isDebugEnabled()) {
173             logger.debug("exiting deleteResource======");
174         }
175     }
176
177     private void getVserverRelations(SvcLogicContext vnfCtx, SvcLogicContext ctx) throws APPCException {
178
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);
188
189                 for (int j = 0; j < relationshipLength; j++) { // loop inside
190                                                                 // relationship
191                                                                 // data, to get
192                                                                 // vserver-id
193                                                                 // and tenant-id
194                     String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
195                     String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
196
197                     vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
198                     if ("vserver.vserver-id".equals(key)) {
199                         vserverID = value;
200                     }
201                     if ("tenant.tenant-id".equals(key)) {
202                         tenantID = value;
203                     }
204                     if ("cloud-region.cloud-owner".equals(key)) {
205                         cloudOwner = value;
206                     }
207                     if ("cloud-region.cloud-region-id".equals(key)) {
208                         cloudRegionId = value;
209                     }
210                 }
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
214                                                                     // data, to
215                                                                     // get
216                                                                     // vserver-name
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);
220                 }
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();
231
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");
237                     }
238                     vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
239                     vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);
240
241                     // loop through relationship-list data, to get vnfc
242                     // relations
243                     for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
244                         if (ctxVnfcKeySet.startsWith("vmRetrived.")
245                                 && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {
246
247                             String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());
248
249                             relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);
250
251                             for (int j = 0; j < relationshipLength; j++) { // loop
252                                                                             // through
253                                                                             // relationship
254                                                                             // data,
255                                                                             // to
256                                                                             // get
257                                                                             // vnfc
258                                                                             // name
259                                 String key = vmCtx
260                                         .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
261                                 String value = vmCtx
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);
266                                     vmSet.add(vmURL);
267                                     vnfcHierarchyMap.put(value, vmSet);
268                                     break; // VM to VNFC is 1 to 1 relation,
269                                             // once we got the VNFC name we can
270                                             // break the loop
271                                 }
272                             }
273                         }
274                     }
275                 } else {
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);
282                 }
283                 vmCount++;
284             }
285         }
286     }
287
288     @Override
289     public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
290         if (logger.isDebugEnabled()) {
291             logger.debug("Inside getVnfHierarchy======");
292         }
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"));
306
307             // loop through relationship-list data, to get vserver relations
308             getVserverRelations(vnfCtx, ctx);
309             vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
310             if (vmCount == 0) {
311                 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
312             }
313             // code changes for getting vnfcs hirearchy
314             populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
315             // vnf,vnfcCount
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());
322             }
323         } else {
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);
330         }
331
332         if (logger.isDebugEnabled()) {
333             logger.debug("exiting getVnfHierarchy======");
334         }
335     }
336
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"));
340         }
341     }
342
343     private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {
344
345         Set<String> vmSet = vnfcHierarchyMap.get(value);
346         if (vmSet == null) {
347             vmSet = new HashSet<>();
348         }
349         return vmSet;
350     }
351
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));
355         }
356         return 0;
357     }
358
359     void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
360             throws APPCException {
361         SvcLogicContext vnfcCtx = new SvcLogicContext();
362         int vnfcCounter = 0;
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);
369
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");
375                 }
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"));
379
380                 // vnf.vnfc[vnfcIndex].name
381                 ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
382
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);
387                 int vmCount = 0;
388                 for (String vmURL : vmSet) {
389                     ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);
390                 }
391
392             }
393             vnfcCounter++;
394         }
395     }
396
397     protected SvcLogicContext getSvcLogicContext() {
398         return new SvcLogicContext();
399     }
400 }