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