Sonar fixes in RestServiceNode
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / FlowControlNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.appc.flow.controller.node;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.fasterxml.jackson.annotation.JsonInclude.Include;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.SerializationFeature;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Properties;
40 import org.apache.commons.lang3.StringUtils;
41 import org.json.JSONObject;
42 import org.onap.appc.flow.controller.ResponseHandlerImpl.DefaultResponseHandler;
43 import org.onap.appc.flow.controller.data.PrecheckOption;
44 import org.onap.appc.flow.controller.data.ResponseAction;
45 import org.onap.appc.flow.controller.data.Transaction;
46 import org.onap.appc.flow.controller.data.Transactions;
47 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
48 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
49 import org.onap.appc.flow.controller.executorImpl.NodeExecutor;
50 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
51 import org.onap.appc.flow.controller.interfaceData.*;
52 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
53 import org.onap.appc.flow.controller.utils.EncryptionTool;
54 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
55 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
56 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
58
59 public class FlowControlNode implements SvcLogicJavaPlugin{
60
61     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlNode.class);
62     private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
63
64     public void processFlow(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
65         log.debug("Received processParamKeys call with params : " + inParams);
66         String responsePrefix = inParams.get(FlowControllerConstants.INPUT_PARAM_RESPONSE_PRIFIX);
67         try {
68             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
69             FlowControlDBService dbservice = FlowControlDBService.initialise();
70             SvcLogicContext localContext = new SvcLogicContext();
71             localContext.setAttribute(FlowControllerConstants.REQUEST_ID, ctx.getAttribute(FlowControllerConstants.REQUEST_ID));
72             localContext.setAttribute(FlowControllerConstants.VNF_TYPE, ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
73             localContext.setAttribute(FlowControllerConstants.REQUEST_ACTION, ctx.getAttribute(FlowControllerConstants.REQUEST_ACTION));
74             localContext.setAttribute(FlowControllerConstants.ACTION_LEVEL, ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
75             localContext.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
76             ctx.setAttribute(FlowControllerConstants.RESPONSE_PREFIX, responsePrefix);
77             dbservice.getFlowReferenceData(ctx, inParams, localContext);
78
79             for (Object key : localContext.getAttributeKeySet()) {
80                 String parmName = (String) key;
81                 String parmValue = ctx.getAttribute(parmName);
82                 log.debug("processFlow " + parmName +  "="  + parmValue);
83
84             }
85             processFlowSequence(inParams, ctx, localContext);
86             if(!ctx.getAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS).equals(FlowControllerConstants.OUTPUT_STATUS_SUCCESS))
87                 throw new SvcLogicException(ctx.getAttribute(responsePrefix +  FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
88
89         } catch (Exception e) {
90             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_STATUS, FlowControllerConstants.OUTPUT_STATUS_FAILURE);
91             ctx.setAttribute(responsePrefix + FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
92             log.error("Error occured in processFlow ", e);
93             throw new SvcLogicException(e.getMessage());
94         }
95     }
96
97     private void processFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext) throws Exception
98     {
99         String fn = "FlowExecutorNode.processflowSequence";
100         log.debug(fn + "Received model for flow : " + localContext.toString());
101         FlowControlDBService dbservice = FlowControlDBService.initialise();
102         String flowSequnce = null;
103         for (Object key : localContext.getAttributeKeySet()) {
104             String parmName = (String) key;
105             String parmValue = ctx.getAttribute(parmName);
106             log.debug(parmName +  "="  + parmValue);
107
108         }
109         if(localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) !=null){
110
111             if(localContext.getAttribute(FlowControllerConstants.GENERATION_NODE) != null){
112                 GraphExecutor transactionExecutor = new GraphExecutor();
113                 Boolean generatorExists = transactionExecutor.hasGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE), null, "sync");
114                 if(generatorExists){
115                     flowSequnce = transactionExecutor.executeGraph("APPC_COMMOM", localContext.getAttribute(FlowControllerConstants.GENERATION_NODE),
116                         null, "sync", null).getProperty(FlowControllerConstants.FLOW_SEQUENCE);
117                 }
118                 else
119                     throw new Exception("Can not find Custom defined Flow Generator for " + localContext.getAttribute(FlowControllerConstants.GENERATION_NODE));
120             }
121             else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.DESINGTIME)){
122                 localContext.setAttribute(FlowControllerConstants.VNFC_TYPE, ctx.getAttribute(FlowControllerConstants.VNFC_TYPE));
123                 flowSequnce = dbservice.getDesignTimeFlowModel(localContext);
124                 if(flowSequnce == null)
125                     throw new Exception("Flow Sequence is not found User Desinged VNF " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
126             }
127             else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.RUNTIME)){
128
129                 Transaction transaction = new Transaction();
130                 String input = collectInputParams(ctx,transaction);
131                 log.info("CollectInputParamsData-Input: " + input );
132
133                 RestExecutor restExe = new RestExecutor();
134                 Map<String,String>flowSeq= restExe.execute(transaction, localContext);
135
136                 JSONObject sequence = new JSONObject(flowSeq.get("restResponse"));
137                 if(sequence.has("output"))
138                     flowSequnce = sequence.getJSONObject("output").toString();
139                 log.info("MultistepSequenceGenerator-Output: "+flowSequnce);
140
141                 if(flowSequnce == null)
142                     throw new Exception("Failed to get the Flow Sequece runtime for VNF type" + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
143
144             }
145             else if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE)).equalsIgnoreCase(FlowControllerConstants.EXTERNAL)){
146                 //String input = collectInputParams(localContext);
147                 //    flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
148                 //if(flowSequnce == null)
149                 throw new Exception("Flow Sequence not found for " + ctx.getAttribute(FlowControllerConstants.VNF_TYPE));
150             }
151             else
152             {
153                 //No other type of model supported...in Future can get flowModel from other generators which will be included here
154                 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time" );
155             }
156         }
157         else{
158             FlowGenerator flowGenerator = new FlowGenerator();
159             Transactions trans = flowGenerator.createSingleStepModel(inParams,ctx);
160             ObjectMapper mapper = new ObjectMapper();
161             flowSequnce = mapper.writeValueAsString(trans);
162             log.debug("Single step Flow Sequence : " +  flowSequnce);
163         }
164         log.debug("Received Flow Sequence : " +  flowSequnce);
165
166         HashMap<Integer, Transaction> transactionMap = createTransactionMap(flowSequnce, localContext);
167         exeuteAllTransaction(transactionMap, ctx);
168         log.info("Executed all the transacstion successfully");
169     }
170
171     private void exeuteAllTransaction(HashMap<Integer, Transaction> transactionMap, SvcLogicContext ctx) throws Exception {
172
173         String fn = "FlowExecutorNode.exeuteAllTransaction ";
174         int retry = 0;
175         FlowExecutorInterface flowExecutor;
176         for (int key = 1; key <= transactionMap.size() ; key++)
177         {
178             log.debug(fn + "Starting transactions ID " + key + " :)=" + retry);
179             Transaction transaction = transactionMap.get(key);
180             if(!preProcessor(transactionMap, transaction)){
181                 log.info("Skipping Transaction ID " +  transaction.getTransactionId());
182                 continue;
183             }
184             if(transaction.getExecutionType() != null){
185                 switch (transaction.getExecutionType()){
186                     case FlowControllerConstants.GRAPH :
187                         flowExecutor = new GraphExecutor();
188                         break;
189                     case FlowControllerConstants.NODE :
190                         flowExecutor =  new NodeExecutor();
191                         break;
192                     case FlowControllerConstants.REST :
193                         flowExecutor = new RestExecutor();
194                         break;
195                     default :
196                         throw new Exception("No Executor found for transaction ID" + transaction.getTransactionId());
197                 }
198                 flowExecutor.execute(transaction, ctx);
199                 ResponseAction responseAction= handleResponse(transaction);
200
201                 if(responseAction.getWait() != null && Integer.parseInt(responseAction.getWait()) > 0){
202                     log.debug(fn + "Going to Sleep .... " + responseAction.getWait());
203                     Thread.sleep(Integer.parseInt(responseAction.getWait())*1000L);
204                 }
205
206                 if(responseAction.isIntermediateMessage()){
207                     log.debug(fn + "Sending Intermediate Message back  .... ");
208                     sendIntermediateMessage();
209                 }
210                 if(responseAction.getRetry() != null && Integer.parseInt(responseAction.getRetry()) > retry ){
211                     log.debug(fn + "Ooppss!!! We will retry again ....... ");
212                     key--;
213                     retry++;
214                     log.debug(fn + "key =" +  key +  "retry =" + retry);
215
216                 }
217                 if(responseAction.isIgnore()){
218                     log.debug(fn + "Ignoring this Error and moving ahead  ....... ");
219                     continue;
220                 }
221                 if(responseAction.isStop()){
222                     log.debug(fn + "Need to Stop  ....... ");
223                     break;
224                 }
225                 if(responseAction.getJump() != null && Integer.parseInt(responseAction.getJump()) > 0 ){
226                     key = Integer.parseInt(responseAction.getJump());
227                     key --;
228                 }
229                 log.debug(fn + "key =" +  key +  "retry =" + retry);
230
231             } else {
232                 throw new Exception("Don't know how to execute transaction ID " + transaction.getTransactionId());
233             }
234         }
235
236     }
237     private void sendIntermediateMessage() {
238         // TODO Auto-generated method stub
239     }
240
241     private ResponseAction handleResponse(Transaction transaction) {
242         log.info("Handling Response for transaction Id " + transaction.getTransactionId());
243         DefaultResponseHandler defaultHandler = new DefaultResponseHandler();
244         return defaultHandler.handlerResponse(transaction);
245     }
246
247     private boolean preProcessor(HashMap<Integer, Transaction> transactionMap, Transaction transaction) throws IOException {
248         log.debug("Starting Preprocessing Logic ");
249         boolean runthisStep = false;
250         try {
251             if(transaction.getPrecheck() != null && transaction.getPrecheck().getPrecheckOptions() != null
252                 && !transaction.getPrecheck().getPrecheckOptions().isEmpty()){
253                 List<PrecheckOption> precheckOptions  = transaction.getPrecheck().getPrecheckOptions();
254                 for(PrecheckOption precheck : precheckOptions){
255                     Transaction trans = transactionMap.get(precheck.getpTransactionID());
256                     ObjectMapper mapper = new ObjectMapper();
257                     log.info("Mapper= " + mapper.writeValueAsString(trans));
258                     HashMap trmap = mapper.readValue(mapper.writeValueAsString(trans), HashMap.class);
259                     if(trmap.get(precheck.getParamName()) != null &&
260                         ((String) trmap.get(precheck.getParamName())).equalsIgnoreCase(precheck.getParamValue()))
261                         runthisStep = true;
262                     else
263                         runthisStep = false;
264
265                     if(("any").equalsIgnoreCase(transaction.getPrecheck().getPrecheckOperator()) && runthisStep)
266                         break;
267                 }
268             } else {
269                 log.debug("No Pre check defined for transaction ID " + transaction.getTransactionId());
270                 runthisStep = true;
271             }
272         }
273         catch(Exception e)
274         {
275             log.error("Error occured when Preprocessing Logic ", e);
276             throw e;
277         }
278         log.debug("Returing process current Transaction = " + runthisStep);
279
280         return runthisStep ;
281     }
282
283     private HashMap<Integer, Transaction> createTransactionMap(String flowSequnce, SvcLogicContext localContext) throws Exception {
284         ObjectMapper mapper = new ObjectMapper();
285         Transactions transactions = mapper.readValue(flowSequnce,Transactions.class);
286         HashMap<Integer, Transaction> transMap = new HashMap<>();
287         for(Transaction transaction : transactions.getTransactions()){
288             compileFlowDependencies(transaction, localContext);
289             //loadTransactionIntoStatus(transactions, ctx); //parse the Transactions Object and create records in process_flow_status table
290             transMap.put(transaction.getTransactionId(), transaction);
291         }
292         return transMap;
293     }
294
295     private void compileFlowDependencies(Transaction transaction, SvcLogicContext localContext) throws Exception {
296         FlowControlDBService dbservice = FlowControlDBService.initialise();
297         dbservice.populateModuleAndRPC(transaction, localContext.getAttribute(FlowControllerConstants.VNF_TYPE));
298         ObjectMapper mapper = new ObjectMapper();
299         log.debug("Indivisual Transaction Details :" + transaction.toString());
300         if((localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) == null) ||
301             (localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE) != null &&
302                 ! localContext.getAttribute(FlowControllerConstants.SEQUENCE_TYPE).equalsIgnoreCase(FlowControllerConstants.DESINGTIME))){
303             localContext.setAttribute("artifact-content", mapper.writeValueAsString(transaction));
304             dbservice.loadSequenceIntoDB(localContext);
305         }
306         //get a field in transction class as transactionhandle interface and register the Handler here for each trnactions
307     }
308
309     private String collectInputParams(SvcLogicContext ctx,Transaction transaction) throws Exception {
310
311         String fn = "FlowExecuteNode.collectInputParams";
312         Properties prop = loadProperties();
313         log.info("Loaded Properties " + prop.toString());
314
315         String vnfId = ctx.getAttribute(FlowControllerConstants.VNF_ID);
316         String inputData = null;
317         log.debug(fn + "vnfId :" + vnfId);
318
319         if (StringUtils.isBlank(vnfId)) {
320             throw new Exception("VnfId is missing");
321         }
322
323         try {
324
325             ActionIdentifier actionIdentifier = new ActionIdentifier();
326             log.debug("Enter ActionIdentifier");
327             if (StringUtils.isNotBlank(vnfId)) {
328                 actionIdentifier.setVnfId(vnfId);
329             }
330             if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VSERVER_ID))) {
331                 actionIdentifier.setVserverId(ctx.getAttribute(FlowControllerConstants.VSERVER_ID));
332             }
333             if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNFC_NAME))) {
334                 actionIdentifier.setVnfcName(ctx.getAttribute(FlowControllerConstants.VNFC_NAME));
335             }
336             log.info("ActionIdentifierData" + actionIdentifier.toString());
337
338             RequestInfo requestInfo = new RequestInfo();
339             log.info("Enter RequestInfo");
340             requestInfo.setAction(ctx.getAttribute(FlowControllerConstants.REQUEST_ACTION));
341             requestInfo.setActionLevel(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL));
342             requestInfo.setPayload(ctx.getAttribute(FlowControllerConstants.PAYLOAD));
343             requestInfo.setActionIdentifier(actionIdentifier);
344             log.debug("RequestInfo: " + requestInfo.toString());
345
346             InventoryInfo inventoryInfo = getInventoryInfo(ctx, vnfId);
347             DependencyInfo dependencyInfo = getDependencyInfo(ctx);
348             Capabilities capabilites = getCapabilitesData(ctx);
349
350             Input input = new Input();
351             log.info("Enter InputData");
352             input.setRequestInfo(requestInfo);
353             input.setInventoryInfo(inventoryInfo);
354             input.setDependencyInfo(dependencyInfo);
355             input.setCapabilities(capabilites);
356             log.info(fn + "Input parameters:" + input.toString());
357
358             ObjectMapper mapper = new ObjectMapper();
359             mapper.setSerializationInclusion(Include.NON_NULL);
360             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
361             inputData = mapper.writeValueAsString(input);
362             log.info("InputDataJson:" + inputData);
363
364         } catch (Exception e) {
365             log.error("Error occured in " + fn, e);
366         }
367
368         String resourceUri = prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_URL);
369         log.info(fn + "resourceUri= " + resourceUri);
370
371         EncryptionTool et = EncryptionTool.getInstance();
372         String pass = et.decrypt(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_PWD));
373
374         transaction.setPayload(inputData);
375         transaction.setExecutionRPC("POST");
376         transaction.setuId(prop.getProperty(FlowControllerConstants.SEQ_GENERATOR_UID));
377         transaction.setPswd(pass);
378         transaction.setExecutionEndPoint(resourceUri);
379
380         return inputData;
381     }
382
383     private DependencyInfo getDependencyInfo(SvcLogicContext ctx) throws Exception {
384
385         String fn = "FlowExecutorNode.getDependencyInfo";
386         DependencyInfo dependencyInfo = new DependencyInfo();
387         FlowControlDBService dbservice = FlowControlDBService.initialise();
388         String dependencyData = dbservice.getDependencyInfo(ctx);
389         log.info(fn + "dependencyDataInput:" + dependencyData);
390
391         if (dependencyData != null) {
392             ObjectMapper mapper = new ObjectMapper();
393             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
394             mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
395             //JsonNode dependencyInfoData = mapper.readTree(dependencyData).get("dependencyInfo");
396             JsonNode vnfcData = mapper.readTree(dependencyData).get("vnfcs");
397             List<Vnfcs> vnfclist = Arrays.asList(mapper.readValue(vnfcData.toString(), Vnfcs[].class));
398             dependencyInfo.getVnfcs().addAll(vnfclist);
399
400             log.info("Dependency Output:"+ dependencyInfo.toString());
401         }
402         return dependencyInfo;
403     }
404
405     private Capabilities getCapabilitesData(SvcLogicContext ctx) throws Exception {
406
407         String fn = "FlowExecutorNode.getCapabilitesData";
408         Capabilities capabilities = new Capabilities();
409         FlowControlDBService dbservice = FlowControlDBService.initialise();
410         String capabilitiesData = dbservice.getCapabilitiesData(ctx);
411         log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
412
413         if (capabilitiesData != null) {
414             ObjectMapper mapper = new ObjectMapper();
415             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
416             mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
417             JsonNode capabilitiesNode = mapper.readValue(capabilitiesData,JsonNode.class);
418             log.info("capabilitiesNode:" + capabilitiesNode.toString());
419
420             JsonNode vnfs = capabilitiesNode.findValue(FlowControllerConstants.VNF);
421             List<String> vnfsList = new ArrayList<>();
422             if (vnfs != null) {
423                 for (int i = 0; i < vnfs.size(); i++) {
424
425                     String vnf = vnfs.get(i).asText();
426                     vnfsList.add(vnf);
427                 }
428             }
429
430             JsonNode vfModules = capabilitiesNode.findValue(FlowControllerConstants.VF_MODULE);
431             List<String> vfModulesList = new ArrayList<>();
432             if (vfModules != null) {
433                 for (int i = 0; i < vfModules.size(); i++) {
434
435                     String vfModule = vfModules.get(i).asText();
436                     vfModulesList.add(vfModule);
437                 }
438             }
439
440             JsonNode vnfcs = capabilitiesNode.findValue(FlowControllerConstants.VNFC);
441             List<String> vnfcsList = new ArrayList<>();
442             if (vnfcs != null) {
443                 for (int i = 0; i < vnfcs.size(); i++) {
444
445                     String vnfc1 = vnfcs.get(i).asText();
446                     vnfcsList.add(vnfc1);
447                 }
448             }
449
450             JsonNode vms = capabilitiesNode.findValue(FlowControllerConstants.VM);
451
452             List<String> vmList = new ArrayList<>();
453             if (vms != null) {
454                 for (int i = 0; i < vms.size(); i++) {
455
456                     String vm1 = vms.get(i).asText();
457                     vmList.add(vm1);
458                 }
459             }
460
461             capabilities.getVnfc().addAll(vnfcsList);
462             capabilities.getVnf().addAll(vnfsList);
463             capabilities.getVfModule().addAll(vfModulesList);
464             capabilities.getVm().addAll(vmList);
465
466             log.info("Capabilities Output:"+ capabilities.toString());
467         }
468         return capabilities;
469     }
470
471     private InventoryInfo getInventoryInfo(SvcLogicContext ctx, String vnfId) throws Exception{
472         String fn = "FlowExecutorNode.getInventoryInfo";
473
474         VnfInfo vnfInfo = new VnfInfo();
475         vnfInfo.setVnfId(vnfId);
476         vnfInfo.setVnfName(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-name"));
477         vnfInfo.setVnfType(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"));
478
479         String vmcount = ctx.getAttribute("tmp.vnfInfo.vm-count");
480         if(StringUtils.isNotBlank(vmcount)){
481             int vmCount = Integer.parseInt(vmcount);
482             log.info(fn +"vmcount:"+ vmCount);
483
484             Vm vm = new Vm();
485             Vnfcslist vnfc = new Vnfcslist();
486             for (int i = 0; i < vmCount; i++) {
487
488                 vm.setVserverId(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-id"));
489                 String vnfccount = ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
490                 int vnfcCount = Integer.parseInt(vnfccount);
491
492                 if (vnfcCount > 0) {
493                     vnfc.setVnfcName(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
494                     vnfc.setVnfcType(ctx.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-type"));
495                     vm.setVnfc(vnfc);
496                 }
497                 vnfInfo.getVm().add(vm);
498             }
499         }
500         InventoryInfo inventoryInfo = new InventoryInfo();
501         inventoryInfo.setVnfInfo(vnfInfo);
502         log.info(fn + "Inventory Output:" +inventoryInfo.toString());
503
504         return inventoryInfo;
505     }
506
507     private static Properties loadProperties() throws Exception {
508         Properties props = new Properties();
509         String propDir = System.getenv(SDNC_CONFIG_DIR_VAR);
510         if (propDir == null)
511             throw new Exception("Cannot find Property file -" + SDNC_CONFIG_DIR_VAR);
512         String propFile = propDir + FlowControllerConstants.APPC_FLOW_CONTROLLER;
513         try (InputStream propStream = new FileInputStream(propFile))
514         {
515             props.load(propStream);
516         }
517         catch (Exception e)
518         {
519             throw new Exception("Could not load properties file " + propFile, e);
520         }
521         return props;
522     }
523 }