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