2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.seqgen.impl;
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;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 import java.util.List;
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;
58 public class StopSequenceGenerator implements SequenceGenerator {
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopSequenceGenerator.class);
63 public List<Transaction> generateSequence(SequenceGeneratorInput input) throws Exception {
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;
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);
74 logger.debug("Flow Model " + flowModel);
75 return generateSequenceWithDependencyModel(flowModel, input);
77 else throw new APPCException("Vnfc details missing in the input");
79 logger.info("Generating sequence without dependency model");
80 return generateSequenceWithOutDependency(input);
84 private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input)throws Exception{
85 String payload = null;
86 PayloadGenerator payloadGenerator = new PayloadGenerator();
87 List<Transaction> transactionList = new LinkedList<>();
88 Integer transactionId = 1;
89 List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
90 List<Integer> transactionIds = new LinkedList<>();
91 for (Vserver vm : vservers) {
92 Transaction transaction = new Transaction();
93 transaction.setTransactionId(transactionId);
94 transactionIds.add(transactionId++);
95 transaction.setAction(Action.STOP.getActionType());
96 transaction.setActionLevel(ActionLevel.VM.getAction());
97 ActionIdentifier actionIdentifier = new ActionIdentifier();
98 actionIdentifier.setvServerId(vm.getId());
99 transaction.setActionIdentifier(actionIdentifier);
100 String vmId = vm.getId();
101 String url = vm.getUrl();
102 payload = payloadGenerator.getPayload(input, vmId, url);
103 transaction.setPayload(payload);
104 if(vservers.size()>1){
105 Response failureResponse = new Response();
106 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
107 Map<String,String> failureAction = new HashMap<>();
108 failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
109 failureResponse.setResponseAction(failureAction);
110 transaction.addResponse(failureResponse);
112 transactionList.add(transaction);
114 return transactionList;
117 private List<Transaction> generateSequenceWithDependencyModel(VnfcFlowModel flowModel,SequenceGeneratorInput input){
118 List<Transaction> transactionList = new LinkedList<>();
119 Integer transactionId = 1;
120 List<Integer> transactionIds = new LinkedList<>();
121 Iterator<List<Vnfc>> itr = flowModel.getModelIterator();
122 while (itr.hasNext()){
123 List<Vnfc> vnfcs = itr.next();
124 for(Vnfc vnfc:vnfcs){
125 boolean stopApplicationSupported = readApplicationStopCapability(input);
126 if(stopApplicationSupported && !vnfc.getVserverList().isEmpty()){
127 Transaction stopAppTransaction = new Transaction();
128 stopAppTransaction.setTransactionId(transactionId++);
129 stopAppTransaction.setAction(Action.STOP_APPLICATION.getActionType());
130 stopAppTransaction.setActionLevel(ActionLevel.VNFC.getAction());
131 ActionIdentifier stopActionIdentifier = new ActionIdentifier();
132 stopActionIdentifier .setVnfcName(vnfc.getVnfcName());
133 stopAppTransaction.setActionIdentifier(stopActionIdentifier );
134 stopAppTransaction.setPayload(input.getRequestInfo().getPayload());
135 Response failureResponse = new Response();
136 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
137 Map<String,String> failureAction = new HashMap<>();
138 failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
139 failureResponse.setResponseAction(failureAction);
140 stopAppTransaction.addResponse(failureResponse);
141 transactionList.add(stopAppTransaction);
143 List<Vserver> vms = vnfc.getVserverList();
145 Transaction transaction = new Transaction();
146 transaction.setTransactionId(transactionId);
147 transactionIds.add(transactionId++);
148 transaction.setAction(Action.STOP.getActionType());
149 transaction.setActionLevel(ActionLevel.VM.getAction());
150 ActionIdentifier actionIdentifier = new ActionIdentifier();
151 actionIdentifier.setvServerId(vm.getId());
152 transaction.setActionIdentifier(actionIdentifier);
153 transaction.setPayload(input.getRequestInfo().getPayload());
154 Response failureResponse = new Response();
155 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
156 Map<String,String> failureAction = new HashMap<>();
157 failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
158 failureResponse.setResponseAction(failureAction);
159 transaction.addResponse(failureResponse);
160 transactionList.add(transaction);
164 return transactionList;
167 private VnfcFlowModel buildFlowModel(InventoryModel inventoryModel, VnfcDependencyModel dependencyModel, FlowStrategies flowStrategy) throws APPCException, InvalidDependencyModelException {
168 FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(flowStrategy);
169 if (flowBuilder == null) {
170 throw new APPCException("Flow Strategy not supported " + flowStrategy);
172 return flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
175 private FlowStrategies readFlowStrategy(SequenceGeneratorInput sequenceGeneratorInput) {
176 Map<String, String> tunableParams = sequenceGeneratorInput.getTunableParams();
177 FlowStrategies strategy = null;
178 String strategyStr = null;
179 if (tunableParams != null) {
180 strategyStr = tunableParams.get(Constants.STRATEGY);
181 strategy = FlowStrategies.findByString(strategyStr);
183 if (strategy == null)
184 strategy= FlowStrategies.REVERSE;
188 private boolean isVnfcPresent(SequenceGeneratorInput input){
189 boolean vnfcPresent=true;
190 List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
191 for (Vserver vm : vservers) {
192 if(!(vm.getVnfc()!=null&& vm.getVnfc().getVnfcType()!=null&& vm.getVnfc().getVnfcName()!=null)){
193 vnfcPresent=false;break;
199 private boolean readApplicationStopCapability(SequenceGeneratorInput input) {
200 Map<String,List<String>> capability = input.getCapability();
201 if(capability!= null){
202 List<String> vnfcCapabilities = capability.get(Constants.CapabilityLevel.VNFC.getLevel());
203 if(vnfcCapabilities!=null)
204 return vnfcCapabilities.stream().anyMatch(p -> Capabilties.STOP_APPLICATION.getCapability().equalsIgnoreCase(p));