2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.seqgen.impl;
 
  27 import org.apache.commons.lang3.StringUtils;
 
  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;
 
  47 import java.util.HashMap;
 
  48 import java.util.Iterator;
 
  49 import java.util.LinkedList;
 
  50 import java.util.List;
 
  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;
 
  59 public class StopSequenceGenerator implements SequenceGenerator {
 
  61     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopSequenceGenerator.class);
 
  64     public List<Transaction> generateSequence(SequenceGeneratorInput input) throws APPCException {
 
  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;
 
  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);
 
  75                 logger.debug("Flow Model " + flowModel);
 
  76                 return generateSequenceWithDependencyModel(flowModel, input);
 
  78                 else throw  new APPCException("Vnfc details missing in the input");
 
  80                 logger.info("Generating sequence without dependency model");
 
  81                 return generateSequenceWithOutDependency(input);
 
  85     private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input){
 
  86         List<Transaction> transactionList = new LinkedList<>();
 
  87         Integer transactionId = 1;
 
  88         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
 
  89         List<Integer> transactionIds = new LinkedList<>();
 
  90         for (Vserver vm : vservers) {
 
  91             Transaction transaction = new Transaction();
 
  92             transaction.setTransactionId(transactionId);
 
  93             transactionIds.add(transactionId++);
 
  94             transaction.setAction(Action.STOP.getActionType());
 
  95             transaction.setActionLevel(ActionLevel.VM.getAction());
 
  96             ActionIdentifier actionIdentifier = new ActionIdentifier();
 
  97             actionIdentifier.setvServerId(vm.getId());
 
  98             transaction.setActionIdentifier(actionIdentifier);
 
  99             transaction.setPayload(input.getRequestInfo().getPayload());
 
 100             if(vservers.size()>1){
 
 101                 Response failureResponse = new Response();
 
 102                 failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
 
 103                 Map<String,String> failureAction = new HashMap<>();
 
 104                 failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
 
 105                 failureResponse.setResponseAction(failureAction);
 
 106                 transaction.addResponse(failureResponse);
 
 108             transactionList.add(transaction);
 
 110        return transactionList;
 
 113     private List<Transaction> generateSequenceWithDependencyModel(VnfcFlowModel flowModel,SequenceGeneratorInput input){
 
 114         List<Transaction> transactionList = new LinkedList<>();
 
 115         Integer transactionId = 1;
 
 116         List<Integer> transactionIds = new LinkedList<>();
 
 117         Iterator<List<Vnfc>> itr = flowModel.getModelIterator();
 
 118         while (itr.hasNext()){
 
 119             List<Vnfc> vnfcs = itr.next();
 
 120             for(Vnfc vnfc:vnfcs){
 
 121                 boolean stopApplicationSupported = readApplicationStopCapability(input);
 
 122                 if(stopApplicationSupported && !vnfc.getVserverList().isEmpty()){
 
 123                     Transaction stopAppTransaction = new Transaction();
 
 124                     stopAppTransaction.setTransactionId(transactionId++);
 
 125                     stopAppTransaction.setAction(Action.STOP_APPLICATION.getActionType());
 
 126                     stopAppTransaction.setActionLevel(ActionLevel.VNFC.getAction());
 
 127                     ActionIdentifier stopActionIdentifier = new ActionIdentifier();
 
 128                     stopActionIdentifier .setVnfcName(vnfc.getVnfcName());
 
 129                     stopAppTransaction.setActionIdentifier(stopActionIdentifier );
 
 130                     stopAppTransaction.setPayload(input.getRequestInfo().getPayload());
 
 131                     Response failureResponse = new Response();
 
 132                     failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
 
 133                     Map<String,String> failureAction = new HashMap<>();
 
 134                     failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
 
 135                     failureResponse.setResponseAction(failureAction);
 
 136                     stopAppTransaction.addResponse(failureResponse);
 
 137                     transactionList.add(stopAppTransaction);
 
 139                 List<Vserver> vms = vnfc.getVserverList();
 
 141                     Transaction transaction = new Transaction();
 
 142                     transaction.setTransactionId(transactionId);
 
 143                     transactionIds.add(transactionId++);
 
 144                     transaction.setAction(Action.STOP.getActionType());
 
 145                     transaction.setActionLevel(ActionLevel.VM.getAction());
 
 146                     ActionIdentifier actionIdentifier = new ActionIdentifier();
 
 147                     actionIdentifier.setvServerId(vm.getId());
 
 148                     transaction.setActionIdentifier(actionIdentifier);
 
 149                     transaction.setPayload(input.getRequestInfo().getPayload());
 
 150                     Response failureResponse = new Response();
 
 151                     failureResponse.setResponseMessage(ResponseMessage.FAILURE.getResponse());
 
 152                     Map<String,String> failureAction = new HashMap<>();
 
 153                     failureAction.put(ResponseAction.IGNORE.getAction(),Boolean.TRUE.toString());
 
 154                     failureResponse.setResponseAction(failureAction);
 
 155                     transaction.addResponse(failureResponse);
 
 156                     transactionList.add(transaction);
 
 160         return transactionList;
 
 163     private VnfcFlowModel buildFlowModel(InventoryModel inventoryModel, VnfcDependencyModel dependencyModel, FlowStrategies flowStrategy) throws APPCException, InvalidDependencyModelException {
 
 164         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(flowStrategy);
 
 165         if (flowBuilder == null) {
 
 166             throw new APPCException("Flow Strategy not supported " + flowStrategy);
 
 168         return flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
 
 171     private FlowStrategies readFlowStrategy(SequenceGeneratorInput sequenceGeneratorInput)  {
 
 172         Map<String, String> tunableParams = sequenceGeneratorInput.getTunableParams();
 
 173         FlowStrategies strategy = null;
 
 174         String strategyStr = null;
 
 175         if (tunableParams != null) {
 
 176             strategyStr = tunableParams.get(Constants.STRATEGY);
 
 177             strategy = FlowStrategies.findByString(strategyStr);
 
 179         if (strategy == null)
 
 180              strategy= FlowStrategies.REVERSE;
 
 184     private boolean isVnfcPresent(SequenceGeneratorInput input){
 
 185         boolean vnfcPresent=true;
 
 186         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
 
 187         for (Vserver vm : vservers) {
 
 188             if(!(vm.getVnfc()!=null&& vm.getVnfc().getVnfcType()!=null&& vm.getVnfc().getVnfcName()!=null)){
 
 189                 vnfcPresent=false;break;
 
 195     private boolean readApplicationStopCapability(SequenceGeneratorInput input) {
 
 196         Map<String,List<String>> capability = input.getCapability();
 
 197         if(capability!= null){
 
 198             List<String> vnfcCapabilities = capability.get(Constants.CapabilityLevel.VNFC.getLevel());
 
 199             if(vnfcCapabilities!=null)
 
 200                 return vnfcCapabilities.stream().anyMatch(p -> Capabilties.STOP_APPLICATION.getCapability().equalsIgnoreCase(p));