2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.aai.client.node;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.HashMap;
33 import java.util.List;
36 import org.apache.commons.lang3.StringUtils;
38 import org.onap.appc.aai.client.AppcAaiClientConstant;
39 import org.onap.appc.aai.client.aai.AaiService;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
49 //import com.fasterxml.jackson.databind.ObjectMapper;
52 public class AAIResourceNode implements SvcLogicJavaPlugin {
54 private static final EELFLogger log = EELFManager.getInstance().getLogger(AAIResourceNode.class);
57 public AaiService getAaiService() {
58 return new AaiService();
60 /* Gets VNF Info and All VServers associated with Vnf */
61 public void getVnfInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
63 log.info("Received getVnfInfo call with params : " + inParams);
65 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
70 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
71 AaiService aai = getAaiService();
75 aai.getGenericVnfInfo(inParams,ctx);
80 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
81 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
82 log.info("getVnfInfo Successful ");
83 } catch (Exception e) {
84 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
85 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
86 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
87 log.error("Failed in getVnfInfo " + e.getMessage());
89 throw new SvcLogicException(e.getMessage());
95 public void getAllVServersVnfcsInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
97 log.info("Received getAllVServersVnfcsInfo call with params : " + inParams);
99 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
102 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
103 AaiService aai = getAaiService();
107 ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
109 int vmWithNoVnfcsCount = 0;
110 String vmCountStr = ctx.getAttribute(responsePrefix + "vm-count");
112 if ( vmCountStr == null )
113 throw new Exception("Unable to get VServers for the VNF");
115 int vmCount = Integer.parseInt(vmCountStr);
116 for ( int i = 0; i < vmCount; i++ ) {
118 SvcLogicContext vmServerCtx = new SvcLogicContext();
120 Map<String, String> paramsVm = new HashMap<String, String>();
121 paramsVm.put("vserverId", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
122 paramsVm.put("tenantId", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
123 paramsVm.put("cloudOwner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
124 paramsVm.put("cloudRegionId", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
125 paramsVm.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
129 aai.getVMInfo(paramsVm, vmServerCtx);
131 HashMap<String, String> vserverMap = new HashMap<String, String>();
132 vserverMap.put("vserver-id", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
133 vserverMap.put("tenant-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
134 vserverMap.put("cloud-owner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
135 vserverMap.put("cloud-region-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
137 // Parameters returned by getVMInfo
138 vserverMap.put("vserver-name", vmServerCtx.getAttribute(responsePrefix + "vm.vserver-name"));
139 vserverMap.put("vf-module-id", vmServerCtx.getAttribute(responsePrefix + "vm.vf-module-id"));
142 // as Per 17.07 requirements we are supporting only one VNFC per VM.
144 String vnfcName = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc[0].vnfc-name");
145 vserverMap.put("vnfc-name", vnfcName);
148 String vnfcCount = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc-count");
149 if ( vnfcCount == null )
152 vserverMap.put("vnfc-count", vnfcCount);
154 if ( vnfcName != null ) {
155 Map<String, String> paramsVnfc = new HashMap<String, String>();
156 paramsVnfc.put("vnfcName", vnfcName);
158 paramsVnfc.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
160 SvcLogicContext vnfcCtx = new SvcLogicContext();
162 aai.getVnfcInfo(paramsVnfc, vnfcCtx);
164 vserverMap.put("vnfc-type", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-type"));
165 vserverMap.put("vnfc-function-code", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-function-code"));
166 vserverMap.put("group-notation", vnfcCtx.getAttribute(responsePrefix + "vnfc.group-notation"));
171 vmWithNoVnfcsCount++;
175 vservers.add(vserverMap);
182 Collections.sort(vservers, new Comparator<Map<String, String>>() {
184 public int compare(Map<String, String> o1, Map<String, String> o2) {
185 return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
189 log.info("SORTED VSERVERS " + vservers.toString());
191 populateContext(vservers, ctx, responsePrefix);
193 log.info("VMCOUNT IN GETALLVSERVERS " + vmCount);
194 log.info("VMSWITHNOVNFCSCOUNT IN GETALLVSERVERS " + vmWithNoVnfcsCount);
195 ctx.setAttribute(responsePrefix+"vnf.vm-count", String.valueOf(vmCount));
196 ctx.setAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count", String.valueOf(vmWithNoVnfcsCount));
199 } catch (Exception e) {
200 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
201 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
202 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
203 log.error("Failed in getAllVServersVnfcsInfo " + e.getMessage());
205 throw new SvcLogicException(e.getMessage());
212 public void populateContext(ArrayList<Map<String, String>> vservers, SvcLogicContext ctx, String prefix) {
215 log.info("Populating Final Context");
218 for (Map<String, String> entry : vservers) {
219 for (String key : entry.keySet()) {
220 String value = entry.get(key);
222 ctx.setAttribute(prefix+ "vm[" + ctr + "]."+ key, value);
223 log.info("Populating Context Key = " + prefix+ "vm[" + ctr + "]."+ key + " Value = " + value);
231 String firstVServerName = null;
232 for ( int i =0; i < ctr; i++ ) {
233 String vnfcName = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
234 log.info("VNFCNAME " + i + vnfcName);
235 if ( vnfcName == null && firstVServerName == null ) {
236 firstVServerName = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
237 ctx.setAttribute("vm-name" , firstVServerName);
238 log.info("Populating Context Key = " + "vm-name" + " Value = " + firstVServerName);
245 public void addVnfcs(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
247 log.info("Received addVnfcs call with params : " + inParams);
249 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
253 int vmWithNoVnfcCount = 0;
257 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
258 AaiService aai = getAaiService();
262 String vnfcRefLenStr = ctx.getAttribute("vnfcReference_length");
264 if ( vnfcRefLenStr == null) {
265 log.info("Vnfc Reference data is missing");
266 throw new Exception("Vnfc Reference data is missing");
270 vnfcRefLen = Integer.parseInt(vnfcRefLenStr);
272 String vmWithNoVnfcCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count");
274 //Commented for backward compatibility
276 /*if ( vmWithNoVnfcCountStr == null) {
277 throw new Exception("VNFCs to be added data from A&AI is missing");
278 //log.info("VNFCs to be added data from A&AI is missing");
281 vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
283 if ( vmWithNoVnfcCount!= vnfcRefLen )
284 throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");
286 String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
288 if ( vmCountStr == null)
289 throw new Exception("VM data from A&AI is missing");
291 vmCount = Integer.parseInt(vmCountStr);
294 log.info("VMCOUNT " + vmCount);
295 log.info("VNFCREFLEN " + vnfcRefLen);
296 aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
303 if ( vmWithNoVnfcCountStr == null) {
304 log.info("Parameter VM without VNFCs(vmWithNoVnfcCountStr) from A&AI is Null");
307 vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
309 log.info("No of VM without VNFCs(vmWithNoVnfcCount) from A&AI is " +vmWithNoVnfcCount);
311 String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
313 if ( vmCountStr == null)
314 throw new Exception("VM data from A&AI is missing");
316 vmCount = Integer.parseInt(vmCountStr);
318 log.info("VMCOUNT " + vmCount);
319 log.info("VNFCREFLEN " + vnfcRefLen);
321 if ( vmWithNoVnfcCount!= vnfcRefLen ) {
322 //throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");
323 log.info("vmWithNoVnfcCount and vnfcRefLen data from table are not same ");
324 aai.checkAndUpdateVnfc(inParams,ctx, vnfcRefLen, vmCount);
329 aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
334 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
335 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
337 log.info("addVnfcs Successful ");
338 } catch (Exception e) {
339 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
340 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
341 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
342 log.error("Failed in addVnfcs " + e.getMessage());
344 throw new SvcLogicException(e.getMessage());
349 public void updateVnfAndVServerStatus(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
351 log.info("Received updateVnfAndVServerStatus call with params : " + inParams);
353 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
361 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
362 AaiService aai = getAaiService();
366 String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
368 if ( vmCountStr == null)
369 throw new Exception("VM data from A&AI is missing");
371 vmCount = Integer.parseInt(vmCountStr);
374 log.info("VMCOUNT " + vmCount);
377 aai.updateVnfStatus(inParams, ctx);
378 aai.updateVServerStatus(inParams,ctx, vmCount);
380 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
381 AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
383 log.info("updateVnfAndVServerStatus Successful ");
384 } catch (Exception e) {
385 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
386 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
387 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
388 log.error("Failed in updateVnfAndVServerStatus " + e.getMessage());
390 throw new SvcLogicException(e.getMessage());
394 /*public void getDummyValues(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
396 log.info("Received getDummyValues call with params : " + inParams);
398 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
402 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
403 String instarKeys = inParams.get("instarKeys");
404 ObjectMapper mapper = new ObjectMapper();
405 if ( instarKeys != null ) {
407 List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
409 Map<String, String> instarParams =new HashMap<String, String>();
411 //System.out.println(keyList.toString());
414 for(int i=0;i<keyList.size();i++)
416 log.info(" -->"+keyList.get(i));
418 //ctx.setAttribute(keyList.get(i), "test" + i);
420 instarParams.put( keyList.get(i), "test" + i);
424 log.info("INSTARPARAMMAP " + instarParams);
425 String jsonString = mapper.writeValueAsString(instarParams);
426 log.info(jsonString);
427 ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
431 log.info("getDummyValues Successful ");
432 } catch (Exception e) {
433 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
434 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
435 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
436 log.error("Failed in getDummyValues " + e.getMessage());
438 throw new SvcLogicException(e.getMessage());
444 /*public void getRequestKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
446 log.info("Received getRequestKeys call with params : " + inParams);
448 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
452 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
453 String instarKeys = inParams.get("instarKeys");
455 ObjectMapper mapper = new ObjectMapper();
456 if ( instarKeys != null ) {
458 List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
460 //Map<String, String> instarParams =new HashMap<String, String>();
464 for(int i=0;i<keyList.size();i++)
466 log.info("INSTARKEY -->"+keyList.get(i));
469 String instarParameter = ctx.getAttribute("INSTAR." + keyList.get(i));
470 log.info("INSTARPARAMETER " + instarParameter);
471 Parameter param = parseParameterContent(instarParameter);
473 log.info("PARAMETER KEY SIZE " + param.getRequestKeys().size());
474 log.info("RULE TYPE " + param.getClassType());
476 for ( int j =0 ; j < param.getRequestKeys().size() ; j++ ) {
478 log.info(" PARAM KEY NAME " + param.getRequestKeys().get(j).getKeyName());
479 log.info(" PARAM KEY VALUE " + param.getRequestKeys().get(j).getKeyValue());
482 // instarParams.put( keyList.get(i), "test" + i);
486 //log.info("INSTARPARAMMAP " + instarParams);
487 //String jsonString = mapper.writeValueAsString(instarParams);
488 //log.info(jsonString);
489 //ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
493 log.info("getRequestKeys Successful ");
494 } catch (Exception e) {
495 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
496 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
497 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
498 log.error("Failed in getRequestKeys " + e.getMessage());
500 throw new SvcLogicException(e.getMessage());
506 public Parameter parseParameterContent(String parameter) throws JsonParseException, JsonMappingException, IOException{
507 Parameter parameterDefinition = null;
508 if(StringUtils.isNotBlank(parameter)){
509 ObjectMapper mapper = new ObjectMapper();
510 parameterDefinition = mapper.readValue(parameter, Parameter.class);
512 return parameterDefinition;