4198bf4424a0728cf48b400d76bf8ee19d905327
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.util.impl;
24
25 import org.openecomp.appc.dg.util.ExecuteNodeAction;
26 import org.openecomp.appc.exceptions.APPCException;
27 import org.openecomp.appc.i18n.Msg;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.att.eelf.i18n.EELFResourceManager;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
32 import org.openecomp.sdnc.sli.SvcLogicException;
33 import org.openecomp.sdnc.sli.SvcLogicResource;
34 import org.openecomp.sdnc.sli.aai.AAIClient;
35 import org.openecomp.sdnc.sli.aai.AAIService;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
39
40 import java.util.*;
41 import java.util.concurrent.ConcurrentHashMap;
42
43
44 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
45
46     private AAIService aaiService;
47     protected static AAIClient client;
48     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);
49
50     public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
51     public ExecuteNodeActionImpl() {
52     }
53
54     /**
55      * initialize the SDNC adapter (AAIService) by building the context.
56      */
57     private void initialize() {
58         getAAIservice();
59     }
60
61     private void getAAIservice() {
62         BundleContext bctx = FrameworkUtil.getBundle(AAIService.class).getBundleContext();
63         // Get AAIadapter reference
64         ServiceReference sref = bctx.getServiceReference(AAIService.class.getName());
65         if (sref != null) {
66             logger.info("AAIService from bundlecontext");
67             aaiService = (AAIService) bctx.getService(sref);
68
69         } else {
70             logger.info("AAIService error from bundlecontext");
71             logger.error(EELFResourceManager.format(Msg.AAI_CONNECTION_FAILED, "AAIService"));
72         }
73     }
74
75     /**
76      * Method called in TestDG to test timeout scenario
77      *
78      * @param params waitTime time in millisecond DG is going to sleep
79      * @param ctx
80      * @throws APPCException
81      */
82     @Override public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
83         try {
84             String waitTime = params.get("waitTime");
85
86             logger.info("DG will waits for " + Long.parseLong(waitTime) + "milliseconds");
87             Thread.sleep(Long.parseLong(waitTime));
88             logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
89         } catch (InterruptedException e) {
90             e.printStackTrace();
91         }
92     }
93
94     @Override public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
95         initialize();
96         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
97                 params.get("resourceKey");
98         if (logger.isDebugEnabled()) {
99             logger.debug("inside getResorce");
100             logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
101         }
102         client = aaiService;
103         try {
104             SvcLogicResource.QueryStatus response =
105                     client.query(resourceType, false, null, resourceKey, ctx_prefix, null, ctx);
106             logger.info("AAIResponse: " + response.toString());
107             ctx.setAttribute("getResource_result", response.toString());
108         } catch (SvcLogicException e) {
109             logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, "", e.getMessage()));
110         }
111         if (logger.isDebugEnabled()) {
112             logger.debug("exiting getResource======");
113         }
114     }
115
116     @Override public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
117         initialize();
118         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
119                 params.get("resourceKey"), att_name = params.get("attributeName"), att_value =
120                 params.get("attributeValue");
121         if (logger.isDebugEnabled()) {
122             logger.debug("inside postResource");
123             logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
124             logger.debug("Updating " + att_name + " to : " + att_value);
125         }
126         Map<String, String> data = new HashMap<String, String>();
127         data.put(att_name, att_value);
128         client = aaiService;
129
130         try {
131             SvcLogicResource.QueryStatus response = client.update(resourceType, resourceKey, data, ctx_prefix, ctx);
132             logger.info("AAIResponse: " + response.toString());
133             ctx.setAttribute("postResource_result", response.toString());
134         } catch (SvcLogicException e) {
135             logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, att_value, e.getMessage()));
136         }
137         if (logger.isDebugEnabled()) {
138             logger.debug("exiting postResource======");
139         }
140     }
141
142     @Override public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
143         initialize();
144         String resourceType = params.get("resourceType"), resourceKey = params.get("resourceKey");
145
146         if (logger.isDebugEnabled()) {
147             logger.debug("inside deleteResource");
148             logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
149         }
150         client = aaiService;
151         try {
152             SvcLogicResource.QueryStatus response = client.delete(resourceType, resourceKey, ctx);
153             logger.info("AAIResponse: " + response.toString());
154             ctx.setAttribute("deleteResource_result", response.toString());
155         } catch (SvcLogicException e) {
156             logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey, e.getMessage()));
157         }
158         if (logger.isDebugEnabled()) {
159             logger.debug("exiting deleteResource======");
160         }
161     }
162
163     @Override public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
164         if (logger.isDebugEnabled()) {
165             logger.debug("Inside getVnfHierarchy======");
166         }
167         //String ctx_prefix = params.get("prefix");
168         String resourceKey = params.get("resourceKey");
169        // String retrivalVnfKey = "vnf-id = '" + resourceKey + "' AND relationship-key = 'vserver.vserver-id'";
170         String retrivalVnfKey = "generic-vnf.vnf-id = '" + resourceKey + "'";
171         Map<String, String> paramsVnf = new HashMap<String, String>();
172         paramsVnf.put("resourceType", "generic-vnf");
173         paramsVnf.put("prefix", "vnfRetrived");
174         paramsVnf.put("resourceKey", retrivalVnfKey);
175         logger.debug("Retrieving VNF details from A&AI");
176         //Retrive all the relations of VNF
177         SvcLogicContext vnfCtx = new SvcLogicContext();
178         getResource(paramsVnf, vnfCtx);
179         if (vnfCtx.getAttribute("getResource_result").equals("SUCCESS")) {
180             if (vnfCtx.getAttribute("vnfRetrived.heat-stack-id") != null) {
181                 ctx.setAttribute("VNF.heat-stack-id", vnfCtx.getAttribute("vnfRetrived.heat-stack-id"));
182             }
183             ctx.setAttribute("vnf.type",vnfCtx.getAttribute("vnfRetrived.vnf-type"));
184             Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<String, String>();
185
186             Map<String, Set<String>> vnfcHierarchyMap = new HashMap<String, Set<String>>();
187             int vmCount = 0;
188             int vnfcCount = 0;
189             Set<String> vmSet = null;
190             String vmURL = "";
191             logger.debug("Parsing Vserver details from VNF relations");
192             for (String ctxKeySet : vnfCtx
193                     .getAttributeKeySet()) {     //loop through relationship-list data, to get vserver relations
194                 if (ctxKeySet.startsWith("vnfRetrived.") && vnfCtx.getAttribute(ctxKeySet).equalsIgnoreCase("vserver")) {
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 = 0;
201                     if (vnfCtx.getAttributeKeySet().contains(vmKey + "relationship-data_length")) {
202                         relationshipLength = Integer.parseInt(vnfCtx.getAttribute(vmKey + "relationship-data_length"));
203                     }
204
205                     for (int j = 0; j
206                             < relationshipLength; j++) {      //loop inside relationship data, to get vserver-id and tenant-id
207                         String key = vnfCtx.getAttribute(vmKey + "relationship-data[" + j + "].relationship-key");
208                         String value = vnfCtx.getAttribute(vmKey + "relationship-data[" + j + "].relationship-value");
209                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "]." + key, value);
210                         if ("vserver.vserver-id".equals(key)) {
211                             vserverID = value;
212                         }
213                         if ("tenant.tenant-id".equals(key)) {
214                             tenantID = value;
215                         }
216                         if ("cloud-region.cloud-owner".equals(key)) {
217                             cloudOwner = value;
218                         }
219                         if ("cloud-region.cloud-region-id".equals(key)) {
220                             cloudRegionId = value;
221                         }
222                     }
223                     int relatedPropertyLength = 0;
224                     if (vnfCtx.getAttributeKeySet().contains(vmKey + "related-to-property_length")) {
225                         relatedPropertyLength =
226                                 Integer.parseInt(vnfCtx.getAttribute(vmKey + "related-to-property_length"));
227                     }
228                     for (int j = 0;
229                          j < relatedPropertyLength; j++) {   //loop inside related-to-property data, to get vserver-name
230                         String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
231                         String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
232                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "]." + key, value);
233                     }
234                     //Retrive VM relations to find vnfc's
235                     //VM to VNFC is 1 to 1 relation
236                     String vmRetrivalKey = "vserver.vserver-id = '" + vserverID + "' AND tenant.tenant_id = '" + tenantID + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";   
237                     Map<String, String> paramsVm = new HashMap<String, String>();
238                     paramsVm.put("resourceType", "vserver");
239                     paramsVm.put("prefix", "vmRetrived");
240                     paramsVm.put("resourceKey", vmRetrivalKey);
241                     SvcLogicContext vmCtx = new SvcLogicContext();
242
243                     logger.debug("Retrieving VM details from A&AI");
244                     getResource(paramsVm, vmCtx);
245                     if (vmCtx.getAttribute("getResource_result").equals("SUCCESS")) {
246                         if (logger.isDebugEnabled()) {
247                             logger.debug("Parsing VNFC details from VM relations");
248                         }
249                         vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
250                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "].URL",vmURL);
251                         for (String ctxVnfcKeySet : vmCtx
252                                 .getAttributeKeySet()) {    //loop through relationship-list data, to get vnfc relations
253                             if (ctxVnfcKeySet.startsWith("vmRetrived.") && vmCtx.getAttribute(ctxVnfcKeySet)
254                                     .equalsIgnoreCase("vnfc")) {
255                                 String vnfcKey = ctxVnfcKeySet.substring(0,
256                                         ctxVnfcKeySet.length() - "related-to".length());
257                                 relationshipLength = 0;
258                                 if (vmCtx.getAttributeKeySet().contains(vnfcKey + "relationship-data_length")) {
259                                     relationshipLength = Integer.parseInt(
260                                             vmCtx.getAttribute(vnfcKey + "relationship-data_length"));
261                                 }
262                                 for (int j = 0; j
263                                         < relationshipLength; j++) {          //loop through relationship data, to get vnfc name
264                                     String key = vmCtx.getAttribute(
265                                             vnfcKey + "relationship-data[" + j + "].relationship-key");
266                                     String value = vmCtx.getAttribute(
267                                             vnfcKey + "relationship-data[" + j + "].relationship-value");
268                                     if (key.equalsIgnoreCase("vnfc.vnfc-name")) {
269                                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "].VNFC", value);
270                                         vmSet = vnfcHierarchyMap.get(value);
271                                         if(vmSet == null){
272                                             vmSet = new HashSet<>();
273                                         }
274                                         vmSet.add(vmURL);
275                                         vnfcHierarchyMap.put(value,vmSet);
276                                         break; //VM to VNFC is 1 to 1 relation, once we got the VNFC name we can break the loop
277                                     }
278                                 }
279                             }
280                         }
281                     } else {
282                         ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "Error Retrieving VNFC hierarchy");
283                         vnfHierarchyMap.put("getVnfHierarchy_result", "FAILURE");
284                         logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
285                                 + vmCtx.getAttribute("getResource_result"));
286                         logger.warn("Incorrect or Incomplete VNF Hierarchy");
287                         throw new APPCException("Error Retrieving VNFC hierarchy");
288                     }
289                     vmCount++;
290                 }
291             }
292             vnfHierarchyMap.put("VNF.VMCount", vmCount + "");
293             if (vmCount == 0) {
294                 ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
295             }
296             //code changes for getting vnfcs hirearchy
297             populateVnfcsDetailsinContext(vnfcHierarchyMap,ctx);
298             //vnf,vnfcCount
299             ctx.setAttribute("VNF.VNFCCount",
300                     Integer.toString(vnfcHierarchyMap.size()));
301             //code changes for getting vnfcs hirearchy
302             ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
303             //Finally set all attributes to ctx
304             for (String attribute : vnfHierarchyMap.keySet()) {
305                 ctx.setAttribute(attribute, vnfHierarchyMap.get(attribute));
306             }
307         } else {
308             ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
309             ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "Error Retrieving VNFC hierarchy");
310             logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
311                     .getAttribute("getResource_result"));
312             logger.warn("Incorrect or Incomplete VNF Hierarchy");
313             throw new APPCException("Error Retrieving VNFC hierarchy");
314         }
315         if (logger.isDebugEnabled()) {
316             logger.debug("exiting getVnfHierarchy======");
317         }
318     }
319
320     private void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx) throws APPCException {
321 //        int vnfcCount = vnfcHierarchyMap.size();
322         SvcLogicContext vnfcCtx = new SvcLogicContext();
323         int vnfcCounter = 0;
324         for (String vnfcName : vnfcHierarchyMap.keySet()) {
325             String vnfcRetrivalKey = "vnfc-name = '" + vnfcName + "'";
326             Map<String, String> paramsVnfc = new HashMap<String, String>();
327             paramsVnfc.put("resourceType", "vnfc");
328             paramsVnfc.put("prefix", "vnfcRetrived");
329             paramsVnfc.put("resourceKey", vnfcRetrivalKey);
330
331             logger.debug("Retrieving VM details from A&AI");
332             getResource(paramsVnfc, vnfcCtx);
333             if (vnfcCtx.getAttribute("getResource_result").equals("SUCCESS")) {
334                 if (logger.isDebugEnabled()) {
335                     logger.debug("Parsing VNFC details from VM relations");
336                 }
337                 //putting required values in the map
338                 //vnf.vnfc[vnfcIndex].type
339                 ctx.setAttribute("VNF.VNFC[" + vnfcCounter + "].TYPE",
340                         vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));
341
342                 // vnf.vnfc[vnfcIndex].name
343                 ctx.setAttribute("VNF.VNFC[" + vnfcCounter + "].NAME",
344                         vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));
345
346                 //vnf.vnfc[vnfcIndex].vmCount
347                 Set<String> vmSet = vnfcHierarchyMap.get(vnfcName);
348                 String vmCountinVnfcs = Integer.toString(vmSet.size());
349                 ctx.setAttribute("VNF.VNFC[" + vnfcCounter + "].VM_COUNT",
350                         vmCountinVnfcs);
351                 int vmCount =0;
352                 for(String vmURL:vmSet){
353                     ctx.setAttribute("VNF.VNFC[" + vnfcCounter + "].VM[" + vmCount++ + "].URL",vmURL);
354                 }
355
356             }
357             vnfcCounter++;
358         }
359     }
360 }