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.onap.appc.seqgen.impl;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.appc.dg.flowbuilder.FlowBuilder;
27 import org.onap.appc.dg.flowbuilder.impl.FlowBuilderFactory;
28 import org.onap.appc.dg.objects.FlowStrategies;
29 import org.onap.appc.dg.objects.InventoryModel;
30 import org.onap.appc.dg.objects.VnfcDependencyModel;
31 import org.onap.appc.dg.objects.VnfcFlowModel;
32 import org.onap.appc.domainmodel.Vnfc;
33 import org.onap.appc.domainmodel.Vserver;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.seqgen.SequenceGenerator;
36 import org.onap.appc.seqgen.objects.*;
40 import static org.onap.appc.seqgen.objects.Constants.*;
42 public class StartSequenceGenerator implements SequenceGenerator {
44 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartSequenceGenerator.class);
46 private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input) throws APPCException {
48 List<Transaction> transactionList = new LinkedList<>();
49 Integer transactionId = 1;
50 List<Vnfc> invVnfcList = input.getInventoryModel().getVnf().getVnfcs();
51 boolean singleTransaction=checkSingleTransaction(invVnfcList);
52 for (Vnfc vnfc : invVnfcList) {
53 List<Vserver> vms = vnfc.getVserverList();
54 List<Integer> transactionIds = new LinkedList<>();
55 for (Vserver vm : vms) {
56 Transaction transaction = new Transaction();
57 transaction.setTransactionId(transactionId);
58 transactionIds.add(transactionId++);
59 transaction.setAction(Action.START.getActionType());
60 transaction.setActionLevel(ActionLevel.VM.getAction());
61 ActionIdentifier actionIdentifier = new ActionIdentifier();
62 actionIdentifier.setvServerId(vm.getId());
63 transaction.setActionIdentifier(actionIdentifier);
64 transaction.setPayload(input.getRequestInfo().getPayload());
65 if(!singleTransaction){
66 updateResponse(transaction);
69 transactionList.add(transaction);
72 return transactionList;
75 private boolean checkSingleTransaction(List<Vnfc> invVnfcList) {
77 for(Vnfc vnfc : invVnfcList) {
78 List<Vserver> vms = vnfc.getVserverList();
79 vServerCount=vServerCount+vms.size();
81 return vServerCount <= 1;
84 private void updateResponse(Transaction transaction) {
85 Response ignoreResponse = new Response();
86 ignoreResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
87 Map<String, String> ignoreAction = new HashMap<>();
88 ignoreAction.put(ResponseAction.IGNORE.getAction(), Boolean.TRUE.toString());
89 ignoreResponse.setResponseAction(ignoreAction);
90 transaction.addResponse(ignoreResponse);
93 private List<Transaction> generateSequenceWithDependencyModel(VnfcFlowModel flowModel, SequenceGeneratorInput input) throws APPCException {
94 Integer waitTime = readWaitTime(input);
95 Integer retryCount = readRetryCount(input);
96 List<Transaction> transactionList = new LinkedList<>();
97 Integer transactionId = 1;
98 Iterator<List<Vnfc>> itr = flowModel.getModelIterator();
99 while (itr.hasNext()) {
100 List<Vnfc> vnfcs = itr.next();
101 for (Vnfc vnfc : vnfcs) {
102 List<Vserver> vms = vnfc.getVserverList();
103 List<Integer> transactionIds = new LinkedList<>();
105 for (Vserver vm : vms) {
106 Transaction transaction = new Transaction();
107 transaction.setTransactionId(transactionId);
108 transactionIds.add(transactionId++);
109 transaction.setAction(Action.START.getActionType());
110 transaction.setActionLevel(ActionLevel.VM.getAction());
111 ActionIdentifier actionIdentifier = new ActionIdentifier();
112 actionIdentifier.setvServerId(vm.getId());
113 transaction.setActionIdentifier(actionIdentifier);
114 transaction.setPayload(input.getRequestInfo().getPayload());
115 updateResponse(transaction);
116 transactionList.add(transaction);
118 boolean startApplicationSupported = readApplicationStartCapability(input);
119 if (startApplicationSupported) {
120 Transaction startAppTransaction = new Transaction();
121 startAppTransaction.setTransactionId(transactionId++);
122 startAppTransaction.setAction(Action.START_APPLICATION.getActionType());
123 startAppTransaction.setActionLevel(ActionLevel.VNFC.getAction());
124 ActionIdentifier startActionIdentifier = new ActionIdentifier();
125 startActionIdentifier.setVnfcName(vnfc.getVnfcName());
126 startAppTransaction.setActionIdentifier(startActionIdentifier);
127 startAppTransaction.setPayload(input.getRequestInfo().getPayload());
129 List<PreCheckOption> preCheckOptions = new LinkedList<>();
130 for (Integer vmTransactionId : transactionIds) {
131 setPreCheckOptions(preCheckOptions, vmTransactionId);
133 startAppTransaction.setPreCheckOperator(PreCheckOperator.ANY.getOperator());
134 startAppTransaction.setPrecheckOptions(preCheckOptions);
135 transactionList.add(startAppTransaction);
137 boolean healthCheckSupported = readHealthCheckCapabilites(input.getCapability());
138 if (healthCheckSupported) {
139 Transaction healthCheckTransaction = new Transaction();
140 healthCheckTransaction.setTransactionId(transactionId++);
141 healthCheckTransaction.setAction(Action.HEALTH_CHECK.getActionType());
142 healthCheckTransaction.setActionLevel(ActionLevel.VNFC.getAction());
143 ActionIdentifier healthCheckActionIdentifier = new ActionIdentifier();
144 healthCheckActionIdentifier.setVnfcName(vnfc.getVnfcName());
145 healthCheckTransaction.setActionIdentifier(healthCheckActionIdentifier);
146 healthCheckTransaction.setPayload(input.getRequestInfo().getPayload());
148 Response retryResponse = new Response();
149 retryResponse.setResponseMessage(ResponseMessage.UNHEALTHY.getResponse());
150 Map<String, String> retryAction = new HashMap<>();
151 retryAction.put(ResponseAction.RETRY.getAction(), retryCount.toString());
152 retryAction.put(ResponseAction.WAIT.getAction(), waitTime.toString());
153 retryResponse.setResponseAction(retryAction);
154 healthCheckTransaction.addResponse(retryResponse);
156 Response healthyResponse = new Response();
157 healthyResponse.setResponseMessage(ResponseMessage.HEALTHY.getResponse());
158 Map<String, String> healthyAction = new HashMap<>();
159 healthyAction.put(ResponseAction.CONTINUE.getAction().toLowerCase(), Boolean.TRUE.toString());
160 healthyResponse.setResponseAction(healthyAction);
161 healthCheckTransaction.addResponse(healthyResponse);
163 Response failureResponse = new Response();
164 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
165 Map<String, String> failureResonseAction = new HashMap<>();
166 failureResonseAction.put(ResponseAction.STOP.getAction(), Boolean.TRUE.toString());
167 failureResponse.setResponseAction(failureResonseAction);
168 healthCheckTransaction.addResponse(failureResponse);
169 transactionList.add(healthCheckTransaction);
174 return transactionList;
177 private void setPreCheckOptions(List<PreCheckOption> preCheckOptions, Integer vmTransactionId) {
178 PreCheckOption option = new PreCheckOption();
179 option.setPreTransactionId(vmTransactionId);
180 option.setParamName("status");
181 option.setParamValue("success");
182 preCheckOptions.add(option);
186 public List<Transaction> generateSequence(SequenceGeneratorInput input) throws APPCException {
187 if(input.getRequestInfo().getActionLevel().equals(ActionLevel.VM.getAction())||input.getRequestInfo().getActionLevel().equals(ActionLevel.VNFC.getAction())||
188 input.getRequestInfo().getActionLevel().equals(ActionLevel.VNF.getAction())||input.getRequestInfo().getActionLevel().equals(ActionLevel.VF_MODULE.getAction())) {
189 if (input.getRequestInfo().getActionLevel().equals(ActionLevel.VNF.getAction()) && input.getDependencyModel() != null) {
190 FlowStrategies flowStrategy = readStartFlowStrategy(input);
191 VnfcFlowModel flowModel = buildFlowModel(input.getInventoryModel()
192 , input.getDependencyModel(), flowStrategy);
193 logger.debug("Flow Model " + flowModel);
194 return generateSequenceWithDependencyModel(flowModel, input);
196 logger.info("Generating sequence without dependency model");
197 return generateSequenceWithOutDependency(input);
199 }throw new APPCException("Invalid action level "+input.getRequestInfo().getActionLevel());
202 private VnfcFlowModel buildFlowModel(InventoryModel inventoryModel, VnfcDependencyModel dependencyModel, FlowStrategies flowStrategy) throws APPCException {
203 FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(flowStrategy);
204 if (flowBuilder == null) {
205 throw new APPCException("Flow Strategy not supported " + flowStrategy);
207 return flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
210 private FlowStrategies readStartFlowStrategy(SequenceGeneratorInput sequenceGeneratorInput) throws APPCException {
211 Map<String, String> tunableParams = sequenceGeneratorInput.getTunableParams();
212 FlowStrategies strategy;
213 String strategyStr = null;
214 if (tunableParams != null) {
215 strategyStr = tunableParams.get(Constants.STRATEGY);
216 if (StringUtils.isBlank(strategyStr)) {
217 return FlowStrategies.FORWARD;
220 strategy = FlowStrategies.findByString(strategyStr);
221 if (strategy != null) {
225 throw new APPCException("Invalid Strategy " + strategyStr);
228 private boolean readHealthCheckCapabilites(Map<String, List<String>> capabilities) {
229 if (capabilities != null) {
230 List<String> vnfcCapabilities = capabilities.get(CapabilityLevel.VNFC.getLevel());
231 if (vnfcCapabilities != null)
232 return vnfcCapabilities.stream()
233 .anyMatch(p -> Capabilties.HEALTH_CHECK.getCapability().equalsIgnoreCase(p));
238 private boolean readApplicationStartCapability(SequenceGeneratorInput input) {
239 Map<String, List<String>> capability = input.getCapability();
240 if (capability != null) {
241 List<String> vnfcCapabilities = capability.get(CapabilityLevel.VNFC.getLevel());
242 if (vnfcCapabilities != null)
243 return vnfcCapabilities.stream().anyMatch(p -> Capabilties.START_APPLICATION.getCapability().equalsIgnoreCase(p));
248 private Integer readRetryCount(SequenceGeneratorInput input) throws APPCException {
249 String paramValStr = input.getTunableParams().get(RETRY_COUNT);
250 if (StringUtils.isEmpty(paramValStr)) {
251 return RETRY_COUNT_VALUE;
254 return Integer.parseInt(paramValStr);
255 } catch (NumberFormatException e) {
256 String message = "Invalid Number for Retry Count " + paramValStr;
257 logger.error(message, e);
258 throw new APPCException(message);
262 private Integer readWaitTime(SequenceGeneratorInput input) throws APPCException {
263 String paramValStr = input.getTunableParams().get(WAIT_TIME);
264 if (StringUtils.isEmpty(paramValStr)) {
265 return WAIT_TIME_VALUE;
268 return Integer.parseInt(paramValStr);
269 } catch (NumberFormatException e) {
270 String message = "Invalid Number for Wait Time " + paramValStr;
271 logger.error(message, e);
272 throw new APPCException(message);