1dd5fefdd15441f43cdb031df0806f26a87e2191
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / main / java / org / onap / appc / seqgen / provider / SequenceGeneratorProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.seqgen.provider;
27
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.Future;
37 import org.apache.commons.lang.StringUtils;
38 import org.onap.appc.dg.objects.InventoryModel;
39 import org.onap.appc.dg.objects.Node;
40 import org.onap.appc.dg.objects.VnfcDependencyModel;
41 import org.onap.appc.domainmodel.Vnf;
42 import org.onap.appc.domainmodel.Vnfc;
43 import org.onap.appc.domainmodel.Vserver;
44 import org.onap.appc.domainmodel.lcm.VNFOperation;
45 import org.onap.appc.exceptions.APPCException;
46 import org.onap.appc.seqgen.SequenceGenerator;
47 import org.onap.appc.seqgen.impl.SequenceGeneratorFactory;
48 import org.onap.appc.seqgen.objects.CapabilityModel;
49 import org.onap.appc.seqgen.objects.Constants;
50 import org.onap.appc.seqgen.objects.Constants.Action;
51 import org.onap.appc.seqgen.objects.PreCheckOption;
52 import org.onap.appc.seqgen.objects.RequestInfo;
53 import org.onap.appc.seqgen.objects.RequestInfoBuilder;
54 import org.onap.appc.seqgen.objects.Response;
55 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
56 import org.onap.appc.seqgen.objects.SequenceGeneratorInputBuilder;
57 import org.onap.appc.seqgen.objects.Transaction;
58 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
59 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
60 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
61 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
62 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.GenerateSequenceInput;
63 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.GenerateSequenceOutput;
64 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.GenerateSequenceOutputBuilder;
65 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.SequenceGeneratorService;
66 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.dependency.info.dependency.info.Vnfcs;
67 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.inventory.info.inventory.info.vnf.info.Vm;
68 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.StatusBuilder;
69 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.Transactions;
70 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.TransactionsBuilder;
71 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.ActionIdentifier;
72 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.ActionIdentifierBuilder;
73 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.PrecheckOptions;
74 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.PrecheckOptionsBuilder;
75 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.Responses;
76 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.ResponsesBuilder;
77 import org.opendaylight.yang.gen.v1.org.onap.appc.sequencegenerator.rev170706.response.transactions.responses.ResponseActionBuilder;
78 import org.opendaylight.yangtools.yang.common.RpcResult;
79 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
80 import com.att.eelf.configuration.EELFLogger;
81 import com.att.eelf.configuration.EELFManager;
82 import com.google.common.util.concurrent.Futures;
83 import com.google.common.util.concurrent.ListenableFuture;
84
85
86 public class SequenceGeneratorProvider implements AutoCloseable,SequenceGeneratorService {
87     protected DataBroker dataBroker;
88     protected RpcProviderRegistry rpcRegistry;
89     protected NotificationPublishService notificationService;
90     protected BindingAwareBroker.RpcRegistration<SequenceGeneratorService> rpcRegistration;
91     private final EELFLogger log = EELFManager.getInstance().getLogger(SequenceGeneratorProvider.class);
92     private final ExecutorService executor;
93     private final static String APP_NAME = "SequenceGeneratorProvider";
94
95     public SequenceGeneratorProvider(DataBroker dataBroker2, NotificationPublishService notificationProviderService,
96             RpcProviderRegistry rpcRegistry2) {
97         log.info("Creating provider for " + APP_NAME);
98         executor = Executors.newFixedThreadPool(1);
99         this.dataBroker = dataBroker2;
100         this.notificationService = notificationProviderService;
101
102         this.rpcRegistry = rpcRegistry2;
103
104         if (this.rpcRegistry != null) {
105             rpcRegistration = rpcRegistry.addRpcImplementation(SequenceGeneratorService.class, this);
106         }
107         log.info("Initialization complete for " + APP_NAME);
108     }
109
110     @Override
111     public void close() throws Exception {
112         log.info("Closing provider for " + APP_NAME);
113         if(this.executor != null){
114             executor.shutdown();
115         }
116         if(this.rpcRegistration != null) {
117             rpcRegistration.close();
118         }
119         log.info("Successfully closed provider for " + APP_NAME);
120     }
121
122     @Override
123     public ListenableFuture<RpcResult<GenerateSequenceOutput>> generateSequence(GenerateSequenceInput input) {
124         RpcResult<GenerateSequenceOutput> rpcResult=null;
125         log.debug("Received input = " + input );
126         try {
127             if(input.getRequestInfo()==null){
128                 throw new APPCException("Request info is missing in the input");
129             }
130             SequenceGenerator seqGenerator = SequenceGeneratorFactory.getInstance()
131                     .createSequenceGenerator(VNFOperation.findByString(input.getRequestInfo().getAction().name()));
132             SequenceGeneratorInput seqGenInput = buildSeqGenInput(input);
133             List<Transaction> transactions = seqGenerator.generateSequence(seqGenInput);
134             if (transactions.isEmpty()) {
135                 rpcResult = buildFailureResponse("Request is not supported", 450);
136             } else {
137                 rpcResult = buildSuccessResponse(transactions);
138             }
139         } catch (Exception e) {
140             log.error("Error Generating Sequence",e);
141             rpcResult = buildFailureResponse(e.getMessage(), 0);
142         }
143         return Futures.immediateFuture(rpcResult);
144     }
145
146     private RpcResult<GenerateSequenceOutput> buildSuccessResponse(List<Transaction> transactions) {
147         log.info("Building response from the list of transactions");
148         List<Transactions> transactionList = new LinkedList<>();
149         for(Transaction transaction: transactions) {
150             ActionIdentifier actionIdentifier = buildActionIdentifierForResponse(transaction);
151             List<PrecheckOptions> precheckOptions = buildPrecheckOptionsForResponse(transaction);
152             List<Responses> responseList = getResponses(transaction);
153             Transactions transactionObj
154                     = new TransactionsBuilder()
155                     .setActionIdentifier(actionIdentifier)
156                     .setAction(transaction.getAction())
157                     .setActionLevel(transaction.getActionLevel())
158                     .setPrecheckOperator(transaction.getPreCheckOperator())
159                     .setPayload(transaction.getPayload())
160                     .setTransactionId(transaction.getTransactionId())
161                     .setPrecheckOptions(precheckOptions)
162                     .setResponses(responseList)
163                     .build();
164             transactionList.add(transactionObj);
165         }
166
167         GenerateSequenceOutputBuilder builder = new GenerateSequenceOutputBuilder()
168                 .setTransactions(transactionList);
169
170         return RpcResultBuilder
171                 .<GenerateSequenceOutput> status(true)
172                 .withResult(builder.build()).build();
173     }
174
175     private ActionIdentifier buildActionIdentifierForResponse(Transaction transaction) {
176         log.info("Adding action identifiers to response.");
177         ActionIdentifier actionIdentifier = null;
178         if(transaction.getActionIdentifier() != null){
179             actionIdentifier = new ActionIdentifierBuilder()
180                     .setVnfId(transaction.getActionIdentifier().getVnfId())
181                     .setVnfcName(transaction.getActionIdentifier().getVnfcName())
182                     .setVserverId(transaction.getActionIdentifier().getvServerId())
183                     .build();
184         }
185         return actionIdentifier;
186     }
187
188     private List<PrecheckOptions> buildPrecheckOptionsForResponse(Transaction transaction) {
189         log.info("Adding Precheck options to response");
190         List<PrecheckOptions> precheckOptions = new LinkedList<>();
191         if(transaction.getPrecheckOptions() != null){
192             for(PreCheckOption option:transaction.getPrecheckOptions()){
193                 PrecheckOptions precheckOption = new PrecheckOptionsBuilder()
194                         .setParamName(option.getParamName())
195                         .setParamValue(option.getParamValue())
196                         .setPreTransactionId(option.getPreTransactionId())
197                         .setRule(option.getRule())
198                         .build();
199                 precheckOptions.add(precheckOption);
200             }
201         }
202         return precheckOptions;
203     }
204     private List<Responses> getResponses(Transaction transaction) {
205         log.info("Building response from the Transaction");
206         List<Responses> responseList = new LinkedList<>();
207         for(Response resp : transaction.getResponses()){
208             Map<String,String> responseActions = resp.getResponseAction();
209             ResponseActionBuilder responseActionBuilder = new ResponseActionBuilder();
210             if(responseActions.get(Constants.ResponseAction.WAIT.getAction()) != null){
211                 responseActionBuilder = responseActionBuilder.setWait(Integer.parseInt(responseActions.get(Constants.ResponseAction.WAIT.getAction())));
212             }
213             if(responseActions.get(Constants.ResponseAction.RETRY.getAction()) != null){
214                 responseActionBuilder = responseActionBuilder.setRetry(Integer.parseInt(responseActions.get(Constants.ResponseAction.RETRY.getAction())));
215             }
216             if(responseActions.get(Constants.ResponseAction.CONTINUE.getAction().toLowerCase())!=null){
217                 responseActionBuilder = responseActionBuilder
218                         .setContinue(Boolean.parseBoolean(responseActions.get(Constants.ResponseAction.CONTINUE.getAction().toLowerCase())));
219             }
220             if(responseActions.get(Constants.ResponseAction.IGNORE.getAction()) != null){
221                 responseActionBuilder = responseActionBuilder.setIgnore(Boolean.parseBoolean(responseActions.get(Constants.ResponseAction.IGNORE.getAction())));
222             }
223             if(responseActions.get(Constants.ResponseAction.STOP.getAction()) != null){
224                 responseActionBuilder = responseActionBuilder.setStop(Boolean.parseBoolean(responseActions.get(Constants.ResponseAction.STOP.getAction())));
225             }
226             if(responseActions.get(Constants.ResponseAction.JUMP.getAction()) != null){
227                 responseActionBuilder = responseActionBuilder.setJump(Integer.parseInt(responseActions.get(Constants.ResponseAction.JUMP.getAction())));
228             }
229             Responses response = new ResponsesBuilder()
230                     .setResponseMessage(resp.getResponseMessage())
231                     .setResponseAction(responseActionBuilder.build())
232                     .build();
233             responseList.add(response);
234         }
235         return responseList;
236     }
237
238     private SequenceGeneratorInput buildSeqGenInput(GenerateSequenceInput input) throws APPCException {
239
240         log.info("Building SequenceGeneratorInput from Yang object GenerateSequenceInput.");
241         validateMandatory(input);
242
243         RequestInfo requestInfo = buildRequestInfoForSeqGenInput(input);
244         InventoryModel inventoryModel = readInventoryModel(input);
245
246         VnfcDependencyModel dependencyModel = readDependencyModel(input);
247         if(dependencyModel != null){
248             validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
249         }
250
251         CapabilityModel capModel = buildCapabilitiesForSeqGenInput(input);
252
253         SequenceGeneratorInputBuilder builder = new SequenceGeneratorInputBuilder()
254                 .requestInfo(requestInfo)
255                 .inventoryModel(inventoryModel)
256                 .dependendcyModel(dependencyModel)
257                 .capabilityModel(capModel);
258
259         builder = buildTunableParamsForSeqGenInput(input, builder);
260
261         return builder.build();
262     }
263
264     private SequenceGeneratorInputBuilder buildTunableParamsForSeqGenInput(GenerateSequenceInput input, SequenceGeneratorInputBuilder builder) {
265         log.info("Initializing Tunable Parameters based on YANG object.");
266         if(input.getTunableParameters() != null){
267             builder = builder.tunableParameter(Constants.RETRY_COUNT,String.valueOf(input.getTunableParameters().getRetryCount()))
268                     .tunableParameter(Constants.WAIT_TIME,String.valueOf(input.getTunableParameters().getWaitTime()));
269             if(input.getTunableParameters().getStrategy() != null){
270                 builder = builder.tunableParameter(Constants.STRATEGY,input.getTunableParameters().getStrategy().name());
271             }
272         }
273         return builder;
274     }
275
276     public CapabilityModel buildCapabilitiesForSeqGenInput(GenerateSequenceInput input) {
277         log.info("Initializing capabilities based on YANG object.");
278         
279         List<String> vnfCapabilities = null;
280         List<String> vfModuleCapabilities = null;
281         Map<String, List<String>> vmCapabilities = null;
282         List<String> vnfcCapabilities = null;
283         
284         if(input.getCapabilities()!=null){
285             if(input.getCapabilities().getVnf()!=null){
286                 vnfCapabilities = input.getCapabilities().getVnf();
287             }
288             if(input.getCapabilities().getVnfc()!=null){
289                 vfModuleCapabilities = input.getCapabilities().getVnfc();
290             }
291             if(input.getCapabilities().getVm()!=null){
292                 vmCapabilities = new HashMap<String, List<String>>();
293                 vmCapabilities.put(Action.ATTACH_VOLUME.getActionType(), input.getCapabilities().getVm().getAttachVolume());
294                 vmCapabilities.put(Action.DETACH_VOLUME.getActionType(), input.getCapabilities().getVm().getDetachVolume());
295                 vmCapabilities.put(Action.EVACUATE.getActionType(), input.getCapabilities().getVm().getEvacuate());
296                 vmCapabilities.put(Action.MIGRATE.getActionType(), input.getCapabilities().getVm().getMigrate());
297                 vmCapabilities.put(Action.REBOOT.getActionType(), input.getCapabilities().getVm().getReboot());
298                 vmCapabilities.put(Action.REBUILD.getActionType(), input.getCapabilities().getVm().getRebuild());
299                 vmCapabilities.put(Action.RESTART.getActionType(), input.getCapabilities().getVm().getRestart());
300                 vmCapabilities.put(Action.SNAPSHOT.getActionType(), input.getCapabilities().getVm().getSnapshot());
301                 vmCapabilities.put(Action.START.getActionType(), input.getCapabilities().getVm().getStart());
302                 vmCapabilities.put(Action.STOP.getActionType(), input.getCapabilities().getVm().getStop());
303             }
304             if(input.getCapabilities().getVfModule()!=null){
305                 vnfcCapabilities = input.getCapabilities().getVfModule();
306             }
307         }
308         return new CapabilityModel(vnfCapabilities, vfModuleCapabilities, vmCapabilities, vnfcCapabilities);
309     }
310
311     private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
312         Set<String> dependencyModelVnfcSet = new HashSet<>();
313         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
314         Set<String> inventoryModelVnfcsSet = new HashSet<>();
315
316         for (Node<Vnfc> node : dependencyModel.getDependencies()) {
317             dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
318             if (node.getChild().isMandatory()) {
319                 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
320             }
321         }
322
323         for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
324             inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
325         }
326
327         // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
328         if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
329             return;
330         }
331
332         if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
333             Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
334             difference.removeAll(dependencyModelVnfcSet);
335             log.error("Dependency model is missing following vnfc type(s): " + difference);
336             throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
337         } else {
338             Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
339             difference.removeAll(inventoryModelVnfcsSet);
340             if (difference.size() > 0) {
341                 log.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
342                 throw new APPCException("VMs missing for the mandatory VNFC : " + difference);
343             }
344         }
345     }
346
347     private RequestInfo buildRequestInfoForSeqGenInput(GenerateSequenceInput input) {
348         log.info("Building RequestInfo from Yang object");
349         RequestInfoBuilder requestInfobuilder = buildRequestInformation(input);
350
351         if(input.getRequestInfo().getActionIdentifier() != null){
352             requestInfobuilder = buildActionIdentifiers(input, requestInfobuilder);
353         }
354
355         return requestInfobuilder.build();
356     }
357
358     private RequestInfoBuilder buildActionIdentifiers(GenerateSequenceInput input, RequestInfoBuilder requestInfobuilder) {
359         log.info("Initializing actionIdentifier for RequestInfo");
360         requestInfobuilder = requestInfobuilder
361                 .actionIdentifier()
362                 .vnfId(input.getRequestInfo().getActionIdentifier().getVnfId())
363                 .vnfcName(input.getRequestInfo().getActionIdentifier().getVnfcName())
364                 .vServerId(input.getRequestInfo().getActionIdentifier().getVserverId());
365         return requestInfobuilder;
366     }
367
368     private RequestInfoBuilder buildRequestInformation(GenerateSequenceInput input) {
369         log.info("Initializing action, actionLevel and payload for RequestInfo");
370         return new RequestInfoBuilder()
371                 .action(input.getRequestInfo().getAction().name())
372                 .actionLevel(input.getRequestInfo().getActionLevel().getName().toLowerCase())
373                 .payload(input.getRequestInfo().getPayload());
374     }
375
376     private void validateMandatory(GenerateSequenceInput input) throws APPCException {
377         if(input.getRequestInfo() == null){
378             throw new APPCException("Request Info is not present in the request");
379         }
380         if(input.getRequestInfo().getAction() == null){
381             throw new APPCException("Action is not present in the request");
382         }
383         if(input.getInventoryInfo() == null){
384             throw new APPCException("inventoryInfo is not provided in the input");
385         }
386         if (input.getInventoryInfo().getVnfInfo() == null) {
387             log.error("vnfInfo is null in the input");
388             throw new APPCException("vnfInfo is missing in the input");
389         }
390         if(input.getInventoryInfo().getVnfInfo().getVm().isEmpty()){
391             log.error("Null vm information in input.");
392             throw new APPCException("VnfInfo is missing in the input");
393         }
394         log.info("Mandatory information present in the request.");
395     }
396
397     private VnfcDependencyModel readDependencyModel(GenerateSequenceInput input) throws APPCException{
398         log.info("Initializing DependencyModel from YANG model.");
399         if(input.getDependencyInfo() == null || input.getDependencyInfo().getVnfcs() == null || input.getDependencyInfo().getVnfcs().isEmpty()){
400             log.info("No dependency model information is present for the request.");
401             return null;
402         }
403         List<Vnfcs> vnfcs = input.getDependencyInfo().getVnfcs();
404         Set<Node<org.onap.appc.domainmodel.Vnfc>> dependencies = new HashSet<>();
405         Set<String> parentVnfcs = new HashSet<>();
406         Set<String> allVnfcTypes = new HashSet<>();
407         for(Vnfcs vnfcObj:vnfcs) {
408             org.onap.appc.domainmodel.Vnfc vnfc = new org.onap.appc.domainmodel.Vnfc();
409             vnfc.setVnfcType(vnfcObj.getVnfcType());
410             allVnfcTypes.add(vnfcObj.getVnfcType());
411             vnfc.setResilienceType(vnfcObj.getResilience());
412             Node<Vnfc> currentNode = buildVnfcNodeForDependenyInfo(dependencies, vnfcObj, vnfc);
413             for(String parentVnfcType:vnfcObj.getParents()) {
414                 parentVnfcs.add(parentVnfcType);
415                 Node<Vnfc> parentNode = readNode(parentVnfcType, dependencies);
416                 if(parentNode == null){
417                     Vnfc parentVnfc = new Vnfc();
418                     parentVnfc.setVnfcType(parentVnfcType);
419                     parentNode = new Node<>(parentVnfc);
420                     currentNode.addParent(parentVnfc);
421                     dependencies.add(parentNode);
422                 }
423                 else{
424                     currentNode.addParent(parentNode.getChild());
425                 }
426             }
427         }
428         for(String parent:parentVnfcs){
429             if(!allVnfcTypes.contains(parent)){
430                 throw new APPCException("Dependency model missing vnfc type " + parent);
431             }
432         }
433         return new VnfcDependencyModel(dependencies);
434     }
435
436     private Node<Vnfc> buildVnfcNodeForDependenyInfo(Set<Node<Vnfc>> dependencies, Vnfcs vnfcObj, Vnfc vnfc) {
437         Node<Vnfc> currentNode = readNode(vnfcObj.getVnfcType(), dependencies);
438         if(currentNode == null){
439             currentNode = new Node<>(vnfc);
440             dependencies.add(currentNode);
441         }
442         else{
443             currentNode.getChild().setResilienceType(vnfcObj.getResilience());
444             currentNode.getChild().setMandatory(vnfcObj.isMandatory());
445         }
446         return currentNode;
447     }
448
449     private Node<org.onap.appc.domainmodel.Vnfc> readNode(String vnfcType, Set<Node<org.onap.appc.domainmodel.Vnfc>> dependencies) {
450         for(Node<org.onap.appc.domainmodel.Vnfc> node : dependencies){
451             if(node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)){
452                 return node;
453             }
454         }
455         return null;
456     }
457
458     private InventoryModel readInventoryModel(GenerateSequenceInput input) throws APPCException {
459
460         log.info("Initializing InventoryModel from Yang input model");
461         Vnf vnf = createVnfForInventoryModel(input);
462         Map<org.onap.appc.domainmodel.Vnfc, List<Vserver>> map = new HashMap<>();
463         buildVserverDetailsForInventoryModel(input, vnf, map);
464         for(Map.Entry<org.onap.appc.domainmodel.Vnfc, List<Vserver>> entry:map.entrySet()){
465             org.onap.appc.domainmodel.Vnfc vnfc = entry.getKey();
466             List<Vserver> vmList = entry.getValue();
467             vnfc.addVservers(vmList);
468         }
469         return new InventoryModel(vnf);
470     }
471
472     private void buildVserverDetailsForInventoryModel(GenerateSequenceInput input, Vnf vnf, Map<Vnfc, List<Vserver>> map) throws APPCException {
473         if(input.getInventoryInfo().getVnfInfo().getVm().size() < 1) {
474             throw  new APPCException("vnfInfo is missing  in the input");
475         }
476         for(Vm vm:input.getInventoryInfo().getVnfInfo().getVm()) {
477             if(StringUtils.isBlank(vm.getVserverId())){
478                 throw new APPCException("vserver-id not found ");
479             }
480             Vserver vserver=new Vserver();
481             vserver.setId(vm.getVserverId());
482             if(!StringUtils.isBlank(vm.getVnfc().getVnfcName()) &&
483                 !StringUtils.isBlank(vm.getVnfc().getVnfcType())) {
484                 Vnfc vfc = new Vnfc();
485                 vfc.setVnfcName(vm.getVnfc().getVnfcName());
486                 vfc.setVnfcType(vm.getVnfc().getVnfcType());
487                 vfc.setVnfcFunctionCode(vm.getVnfc().getVnfcFunctionCode());
488                 vserver.setVnfc(vfc);
489                 List<Vserver> vms = map.get(vfc);
490                 if(vms ==null) {
491                     vms = new LinkedList<>();
492                     map.put(vfc,vms);
493                 }
494                 vms.add(vserver);
495             }
496             vnf.addVserver(vserver);
497          }
498     }
499
500     private Vnf createVnfForInventoryModel(GenerateSequenceInput input) {
501         log.info("Setting VnfId and VnfType values for Vnf Inventory Model ");
502         Vnf vnf=new Vnf();
503         vnf.setVnfId(input.getInventoryInfo().getVnfInfo().getVnfId());
504         vnf.setVnfType(input.getInventoryInfo().getVnfInfo().getVnfType());
505         vnf.setIdentityUrl(input.getInventoryInfo().getVnfInfo().getIdentityUrl());
506         return vnf;
507     }
508
509     private RpcResult<GenerateSequenceOutput> buildFailureResponse(String errorMessage, int errorCode){
510         GenerateSequenceOutputBuilder sequenceGeneratorOutputBuilder=new GenerateSequenceOutputBuilder();
511         StatusBuilder statusBuilder = new StatusBuilder();
512         statusBuilder.setCode((errorCode != 0)? errorCode : 401);
513         statusBuilder.setMessage(errorMessage);
514         sequenceGeneratorOutputBuilder.setStatus(statusBuilder.build());
515         return RpcResultBuilder
516                 .<GenerateSequenceOutput> status(true)
517                 .withResult(sequenceGeneratorOutputBuilder.build())
518                 .build();
519     }
520 }