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