2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.appc.seqgen.impl;
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;
40 import static org.openecomp.appc.seqgen.objects.Constants.*;
42 public class StopSequenceGenerator implements SequenceGenerator {
44 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartSequenceGenerator.class);
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);
57 logger.info("Generating sequence without dependency model");
58 return generateSequenceWithOutDependency(input);
60 }throw new APPCException("Invalid action level "+input.getRequestInfo().getActionLevel());
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();
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);
84 transactionList.add(transaction);
87 return transactionList;
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);
98 private boolean checkSingleTransaction(List<Vnfc> invVnfcList) {
100 for(Vnfc vnfc : invVnfcList) {
101 List<Vserver> vms = vnfc.getVserverList();
102 vServerCount=vServerCount+vms.size();
104 return vServerCount <= 1;
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);
128 List<Vserver> vms = vnfc.getVserverList();
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());
140 updateStopResponse(transaction);
141 transactionList.add(transaction);
145 return transactionList;
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);
153 return flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
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;
165 strategy = FlowStrategies.findByString(strategyStr);
166 if (strategy != null) {
170 throw new APPCException("Invalid Strategy " + strategyStr);
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));