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