Change nexus values to properties
[appc.git] / app-c / appc / appc-dg-util / appc-dg-util-bundle / src / main / java / org / openecomp / appc / dg / util / impl / ExecuteNodeActionImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.dg.util.impl;
23
24 import org.openecomp.appc.adapter.netconf.util.Constants;
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.openecomp.sdnc.sli.provider.ExecuteNodeExecutor;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.concurrent.ConcurrentHashMap;
44
45
46 public class ExecuteNodeActionImpl implements ExecuteNodeAction {
47
48     private AAIService aaiService;
49     protected static AAIClient client;
50     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);
51
52     public ExecuteNodeActionImpl() {
53         // getAAIservice();
54     }
55
56     /**
57      * initialize the SDNC adapter (AAIService) by building the context.
58      */
59     private void initialize() {
60         getAAIservice();
61     }
62
63     private void getAAIservice() {
64         BundleContext bctx = FrameworkUtil.getBundle(AAIService.class).getBundleContext();
65         // Get AAIadapter reference
66         ServiceReference sref = bctx.getServiceReference(AAIService.class.getName());
67         if (sref != null) {
68             logger.info("AAIService from bundlecontext");
69             aaiService = (AAIService) bctx.getService(sref);
70
71         } else {
72             logger.info("AAIService error from bundlecontext");
73             logger.error(EELFResourceManager.format(Msg.AAI_CONNECTION_FAILED, sref.toString()));
74         }
75     }
76
77     /**
78      * Method called in TestDG to test timeout scenario
79      *
80      * @param params waitTime time in millisecond DG is going to sleep
81      * @param ctx
82      * @throws APPCException
83      */
84     @Override 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             e.printStackTrace();
93         }
94     }
95
96     @Override public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
97         initialize();
98         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
99                         params.get("resourceKey");
100         if (logger.isDebugEnabled()) {
101             logger.debug("inside getResorce");
102             logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
103         }
104         client = aaiService;
105         try {
106             SvcLogicResource.QueryStatus response =
107                             client.query(resourceType, false, null, resourceKey, ctx_prefix, null, ctx);
108             logger.info("AAIResponse: " + response.toString());
109             ctx.setAttribute("getResource_result", response.toString());
110         } catch (SvcLogicException e) {
111             logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, "", e.getMessage()));
112         }
113         if (logger.isDebugEnabled()) {
114             logger.debug("exiting getResource======");
115         }
116     }
117
118     @Override public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
119         initialize();
120         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
121                         params.get("resourceKey"), att_name = params.get("attributeName"), att_value =
122                         params.get("attributeValue");
123         if (logger.isDebugEnabled()) {
124             logger.debug("inside postResource");
125             logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
126             logger.debug("Updating " + att_name + " to : " + att_value);
127         }
128         Map<String, String> data = new HashMap<String, String>();
129         data.put(att_name, att_value);
130         client = aaiService;
131
132         try {
133             SvcLogicResource.QueryStatus response = client.update(resourceType, resourceKey, data, ctx_prefix, ctx);
134             logger.info("AAIResponse: " + response.toString());
135             ctx.setAttribute("postResource_result", response.toString());
136         } catch (SvcLogicException e) {
137             logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, att_value, e.getMessage()));
138         }
139         if (logger.isDebugEnabled()) {
140             logger.debug("exiting postResource======");
141         }
142     }
143
144     @Override public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
145         initialize();
146         String resourceType = params.get("resourceType"), resourceKey = params.get("resourceKey");
147
148         if (logger.isDebugEnabled()) {
149             logger.debug("inside deleteResource");
150             logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
151         }
152         client = aaiService;
153         try {
154             SvcLogicResource.QueryStatus response = client.delete(resourceType, resourceKey, ctx);
155             logger.info("AAIResponse: " + response.toString());
156             ctx.setAttribute("deleteResource_result", response.toString());
157         } catch (SvcLogicException e) {
158             logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey, e.getMessage()));
159         }
160         if (logger.isDebugEnabled()) {
161             logger.debug("exiting deleteResource======");
162         }
163     }
164
165     @Override public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
166         if (logger.isDebugEnabled()) {
167             logger.debug("Inside getVnfHierarchy======");
168         }
169         //String ctx_prefix = params.get("prefix");
170         String resourceKey = params.get("resourceKey");
171         String retrivalVnfKey = "vnf-id = '" + resourceKey + "' AND relationship-key = 'vserver.vserver-id'";
172         Map<String, String> paramsVnf = new HashMap<String, String>();
173         paramsVnf.put("resourceType", "generic-vnf:relationship-list");
174         paramsVnf.put("prefix", "vnfRetrived");
175         paramsVnf.put("resourceKey", retrivalVnfKey);
176         logger.debug("Retrieving VNF details from A&AI");
177         //Retrive all the relations of VNF
178         getResource(paramsVnf, ctx);
179         if (ctx.getAttribute("getResource_result").equals("SUCCESS")) {
180             if (ctx.getAttribute("vnfRetrived.heat-stack-id") != null) {
181                 ctx.setAttribute("VNF.heat-stack-id", ctx.getAttribute("vnfRetrived.heat-stack-id"));
182             }
183             Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<String, String>();
184             int vmCount = 0;
185             logger.debug("Parsing Vserver details from VNF relations");
186             for (String ctxKeySet : ctx
187                             .getAttributeKeySet()) {     //loop through relationship-list data, to get vserver relations
188                 if (ctxKeySet.startsWith("vnfRetrived.") && ctx.getAttribute(ctxKeySet).equalsIgnoreCase("vserver")) {
189                     String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
190                     String vserverID = null;
191                     String tenantID = null;
192                     int relationshipLength = 0;
193                     if (ctx.getAttributeKeySet().contains(vmKey + "relationship-data_length")) {
194                         relationshipLength = Integer.parseInt(ctx.getAttribute(vmKey + "relationship-data_length"));
195                     }
196
197                     for (int j = 0; j
198                                     < relationshipLength; j++) {      //loop inside relationship data, to get vserver-id and tenant-id
199                         String key = ctx.getAttribute(vmKey + "relationship-data[" + j + "].relationship-key");
200                         String value = ctx.getAttribute(vmKey + "relationship-data[" + j + "].relationship-value");
201                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "]." + key, value);
202                         if ("vserver.vserver-id".equals(key)) {
203                             vserverID = value;
204                         }
205                         if ("tenant.tenant-id".equals(key)) {
206                             tenantID = value;
207                         }
208                     }
209                     int relatedPropertyLength = 0;
210                     if (ctx.getAttributeKeySet().contains(vmKey + "related-to-property_length")) {
211                         relatedPropertyLength =
212                                         Integer.parseInt(ctx.getAttribute(vmKey + "related-to-property_length"));
213                     }
214                     for (int j = 0;
215                          j < relatedPropertyLength; j++) {   //loop inside related-to-property data, to get vserver-name
216                         String key = ctx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
217                         String value = ctx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
218                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "]." + key, value);
219                     }
220                     //Retrive VM relations to find vnfc's
221                     //VM to VNFC is 1 to 1 relation
222                     String vmRetrivalKey = "vserver-id = '" + vserverID + "' AND tenant_id = '" + tenantID + "'";
223                     Map<String, String> paramsVm = new HashMap<String, String>();
224                     paramsVm.put("resourceType", "vserver:relationship-list");
225                     paramsVm.put("prefix", "vmRetrived");
226                     paramsVm.put("resourceKey", vmRetrivalKey);
227                     SvcLogicContext vmCtx = new SvcLogicContext();
228
229                     logger.debug("Retrieving VM details from A&AI");
230                     getResource(paramsVm, vmCtx);
231                     if (vmCtx.getAttribute("getResource_result").equals("SUCCESS")) {
232                         if (logger.isDebugEnabled()) {
233                             logger.debug("Parsing VNFC details from VM relations");
234                         }
235                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "].URL",
236                                         vmCtx.getAttribute("vmRetrived.vserver-selflink"));
237                         for (String ctxVnfcKeySet : vmCtx
238                                         .getAttributeKeySet()) {    //loop through relationship-list data, to get vnfc relations
239                             if (ctxVnfcKeySet.startsWith("vmRetrived.") && vmCtx.getAttribute(ctxVnfcKeySet)
240                                             .equalsIgnoreCase("vnfc")) {
241                                 String vnfcKey = ctxVnfcKeySet.substring(0,
242                                                 ctxVnfcKeySet.length() - "related-to".length());
243                                 relationshipLength = 0;
244                                 if (vmCtx.getAttributeKeySet().contains(vnfcKey + "relationship-data_length")) {
245                                     relationshipLength = Integer.parseInt(
246                                                     vmCtx.getAttribute(vnfcKey + "relationship-data_length"));
247                                 }
248                                 for (int j = 0; j
249                                                 < relationshipLength; j++) {          //loop through relationship data, to get vnfc name
250                                     String key = vmCtx.getAttribute(
251                                                     vnfcKey + "relationship-data[" + j + "].relationship-key");
252                                     String value = vmCtx.getAttribute(
253                                                     vnfcKey + "relationship-data[" + j + "].relationship-value");
254                                     if (key.equalsIgnoreCase("vnfc.vnfc-name")) {
255                                         vnfHierarchyMap.put("VNF.VM[" + vmCount + "].VNFC", value);
256                                         break; //VM to VNFC is 1 to 1 relation, once we got the VNFC name we can break the loop
257                                     }
258                                 }
259                             }
260                         }
261                     } else {
262                         ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, "Error Retrieving VNFC hierarchy");
263                         vnfHierarchyMap.put("getVnfHierarchy_result", "FAILURE");
264                         logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
265                                         + vmCtx.getAttribute("getResource_result"));
266                         logger.warn("Incorrect or Incomplete VNF Hierarchy");
267                         throw new APPCException("Error Retrieving VNFC hierarchy");
268                     }
269                     vmCount++;
270                 }
271             }
272             vnfHierarchyMap.put("VNF.VMCount", vmCount + "");
273             if (vmCount == 0) {
274                 ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
275             }
276             ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
277             //Finally set all attributes to ctx
278             for (String attribute : vnfHierarchyMap.keySet()) {
279                 ctx.setAttribute(attribute, vnfHierarchyMap.get(attribute));
280             }
281         } else {
282             ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
283             ctx.setAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE, "Error Retrieving VNFC hierarchy");
284             logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
285                             .getAttribute("getResource_result"));
286             logger.warn("Incorrect or Incomplete VNF Hierarchy");
287             throw new APPCException("Error Retrieving VNFC hierarchy");
288         }
289         if (logger.isDebugEnabled()) {
290             logger.debug("exiting getVnfHierarchy======");
291         }
292     }
293 }