Updated SDC listener and dependent bundles
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / main / java / org / onap / appc / seqgen / impl / StopSequenceGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.seqgen.impl;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.appc.dg.flowbuilder.FlowBuilder;
25 import org.onap.appc.dg.flowbuilder.impl.FlowBuilderFactory;
26 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
27 import org.onap.appc.dg.objects.FlowStrategies;
28 import org.onap.appc.dg.objects.InventoryModel;
29 import org.onap.appc.dg.objects.VnfcDependencyModel;
30 import org.onap.appc.dg.objects.VnfcFlowModel;
31 import org.onap.appc.domainmodel.Vnfc;
32 import org.onap.appc.domainmodel.Vserver;
33 import org.onap.appc.exceptions.APPCException;
34 import org.onap.appc.seqgen.SequenceGenerator;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.onap.appc.seqgen.objects.ActionIdentifier;
38 import org.onap.appc.seqgen.objects.Constants;
39 import org.onap.appc.seqgen.objects.Response;
40 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
41 import org.onap.appc.seqgen.objects.Transaction;
42
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Map;
48
49 import static org.onap.appc.seqgen.objects.Constants.Action;
50 import static org.onap.appc.seqgen.objects.Constants.ActionLevel;
51 import static org.onap.appc.seqgen.objects.Constants.ResponseAction;
52 import static org.onap.appc.seqgen.objects.Constants.ResponseMessage;
53 import static org.onap.appc.seqgen.objects.Constants.Capabilties;
54
55 public class StopSequenceGenerator implements SequenceGenerator {
56
57     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopSequenceGenerator.class);
58
59     @Override
60     public List<Transaction> generateSequence(SequenceGeneratorInput input) throws APPCException {
61         if (input.getRequestInfo().getActionLevel().equals(ActionLevel.VNF.getAction()) && input.getDependencyModel() != null ) {
62             if(isVnfcPresent(input)) {
63                 FlowStrategies flowStrategy = readFlowStrategy(input);
64                 VnfcFlowModel flowModel = null;
65                 try {
66                         flowModel = buildFlowModel(input.getInventoryModel(), input.getDependencyModel(), flowStrategy);
67                     } catch (InvalidDependencyModelException invalidDependencyModelException) {
68                         logger.error("Error Generating Sequence", invalidDependencyModelException);
69                         throw  new APPCException(invalidDependencyModelException.getMessage(), invalidDependencyModelException);
70                 }
71                 logger.debug("Flow Model " + flowModel);
72                 return generateSequenceWithDependencyModel(flowModel, input);
73             }
74                 else throw  new APPCException("Vnfc details missing in the input");
75             } else {
76                 logger.info("Generating sequence without dependency model");
77                 return generateSequenceWithOutDependency(input);
78             }
79     }
80
81     private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input){
82         List<Transaction> transactionList = new LinkedList<>();
83         Integer transactionId = 1;
84         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
85         List<Integer> transactionIds = new LinkedList<>();
86         for (Vserver vm : vservers) {
87             Transaction transaction = new Transaction();
88             transaction.setTransactionId(transactionId);
89             transactionIds.add(transactionId++);
90             transaction.setAction(Action.STOP.getActionType());
91             transaction.setActionLevel(ActionLevel.VM.getAction());
92             ActionIdentifier actionIdentifier = new ActionIdentifier();
93             actionIdentifier.setvServerId(vm.getId());
94             transaction.setActionIdentifier(actionIdentifier);
95             transaction.setPayload(input.getRequestInfo().getPayload());
96             if(vservers.size()>1){
97                 Response failureResponse = new Response();
98                 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
99                 Map<String,String> failureAction = new HashMap<>();
100                 failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
101                 failureResponse.setResponseAction(failureAction);
102                 transaction.addResponse(failureResponse);
103             }
104             transactionList.add(transaction);
105        }
106        return transactionList;
107     }
108
109     private List<Transaction> generateSequenceWithDependencyModel(VnfcFlowModel flowModel,SequenceGeneratorInput input){
110         List<Transaction> transactionList = new LinkedList<>();
111         Integer transactionId = 1;
112         List<Integer> transactionIds = new LinkedList<>();
113         Iterator<List<Vnfc>> itr = flowModel.getModelIterator();
114         while (itr.hasNext()){
115             List<Vnfc> vnfcs = itr.next();
116             for(Vnfc vnfc:vnfcs){
117                 boolean stopApplicationSupported = readApplicationStopCapability(input);
118                 if(stopApplicationSupported && !vnfc.getVserverList().isEmpty()){
119                     Transaction stopAppTransaction = new Transaction();
120                     stopAppTransaction.setTransactionId(transactionId++);
121                     stopAppTransaction.setAction(Action.STOP_APPLICATION.getActionType());
122                     stopAppTransaction.setActionLevel(ActionLevel.VNFC.getAction());
123                     ActionIdentifier stopActionIdentifier = new ActionIdentifier();
124                     stopActionIdentifier .setVnfcName(vnfc.getVnfcName());
125                     stopAppTransaction.setActionIdentifier(stopActionIdentifier );
126                     stopAppTransaction.setPayload(input.getRequestInfo().getPayload());
127                     Response failureResponse = new Response();
128                     failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
129                     Map<String,String> failureAction = new HashMap<>();
130                     failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
131                     failureResponse.setResponseAction(failureAction);
132                     stopAppTransaction.addResponse(failureResponse);
133                     transactionList.add(stopAppTransaction);
134                 }
135                 List<Vserver> vms = vnfc.getVserverList();
136                 for(Vserver vm:vms){
137                     Transaction transaction = new Transaction();
138                     transaction.setTransactionId(transactionId);
139                     transactionIds.add(transactionId++);
140                     transaction.setAction(Action.STOP.getActionType());
141                     transaction.setActionLevel(ActionLevel.VM.getAction());
142                     ActionIdentifier actionIdentifier = new ActionIdentifier();
143                     actionIdentifier.setvServerId(vm.getId());
144                     transaction.setActionIdentifier(actionIdentifier);
145                     transaction.setPayload(input.getRequestInfo().getPayload());
146                     Response failureResponse = new Response();
147                     failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
148                     Map<String,String> failureAction = new HashMap<>();
149                     failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
150                     failureResponse.setResponseAction(failureAction);
151                     transaction.addResponse(failureResponse);
152                     transactionList.add(transaction);
153                 }
154             }
155         }
156         return transactionList;
157     }
158
159     private VnfcFlowModel buildFlowModel(InventoryModel inventoryModel, VnfcDependencyModel dependencyModel, FlowStrategies flowStrategy) throws APPCException, InvalidDependencyModelException {
160         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(flowStrategy);
161         if (flowBuilder == null) {
162             throw new APPCException("Flow Strategy not supported " + flowStrategy);
163         }
164         return flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
165     }
166
167     private FlowStrategies readFlowStrategy(SequenceGeneratorInput sequenceGeneratorInput)  {
168         Map<String, String> tunableParams = sequenceGeneratorInput.getTunableParams();
169         FlowStrategies strategy = null;
170         String strategyStr = null;
171         if (tunableParams != null) {
172             strategyStr = tunableParams.get(Constants.STRATEGY);
173             strategy = FlowStrategies.findByString(strategyStr);
174         }
175         if (strategy == null)
176              strategy= FlowStrategies.REVERSE;
177         return strategy;
178     }
179
180     private boolean isVnfcPresent(SequenceGeneratorInput input){
181         boolean vnfcPresent=true;
182         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
183         for (Vserver vm : vservers) {
184             if(!(vm.getVnfc()!=null&& vm.getVnfc().getVnfcType()!=null&& vm.getVnfc().getVnfcName()!=null)){
185                 vnfcPresent=false;break;
186             }
187         }
188         return vnfcPresent;
189     }
190
191     private boolean readApplicationStopCapability(SequenceGeneratorInput input) {
192         Map<String,List<String>> capability = input.getCapability();
193         if(capability!= null){
194             List<String> vnfcCapabilities = capability.get(Constants.CapabilityLevel.VNFC.getLevel());
195             if(vnfcCapabilities!=null)
196                 return vnfcCapabilities.stream().anyMatch(p -> Capabilties.STOP_APPLICATION.getCapability().equalsIgnoreCase(p));
197         }
198         return false;
199     }
200
201
202 }