Add vm caps fixes to sequence-generator code (vm caps)
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / main / java / org / onap / appc / seqgen / impl / RestartSequenceGenerator.java
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 com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.onap.appc.domainmodel.Vserver;
31 import org.onap.appc.seqgen.SequenceGenerator;
32 import org.onap.appc.seqgen.objects.ActionIdentifier;
33 import org.onap.appc.seqgen.objects.Constants;
34 import org.onap.appc.seqgen.objects.Constants.Action;
35 import org.onap.appc.seqgen.objects.Constants.ResponseAction;
36 import org.onap.appc.seqgen.objects.Response;
37 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
38 import org.onap.appc.seqgen.objects.Transaction;
39
40 import java.util.HashMap;
41 import java.util.LinkedList;
42 import java.util.List;
43 import java.util.Map;
44
45 public class RestartSequenceGenerator implements SequenceGenerator{
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestartSequenceGenerator.class);
47
48     @Override
49     public List<Transaction> generateSequence(SequenceGeneratorInput input) throws Exception {
50             logger.info("Generating sequence without dependency model");
51             return generateSequenceWithOutDependency(input);
52     }
53
54     private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input) throws Exception{
55         String payload = null;
56         List<Transaction> transactionList = new LinkedList<>();
57         Integer transactionId = 1;
58         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
59         List<Integer> transactionIds = new LinkedList<>();
60         PayloadGenerator payloadGenerator = new PayloadGenerator();
61             for (Vserver vm : vservers) {
62                 // check vm-Restart-capabilities for this vm's vnfc-function-code (before incrementing transactionId)
63                 if (!vmSupportsRestart(input, vm)) {
64                     continue;
65                 }
66                 Transaction transactionStop = new Transaction();
67                 transactionStop.setTransactionId(transactionId);
68                 transactionIds.add(transactionId++);
69                 transactionStop.setAction(Constants.Action.STOP.getActionType());
70                 transactionStop.setActionLevel(Constants.ActionLevel.VM.getAction());
71                 ActionIdentifier actionIdentifier = new ActionIdentifier();
72                 actionIdentifier.setvServerId(vm.getId());
73                 transactionStop.setActionIdentifier(actionIdentifier);
74                 String vmId = vm.getId();
75                 String url = vm.getUrl();
76                 payload = payloadGenerator.getPayload(input, vmId, url);
77                 transactionStop.setPayload(payload);
78                 if (vservers.size()>1) {
79                     Response failureResponse = new Response();
80                     failureResponse.setResponseMessage(Constants.ResponseMessage.FAILURE.getResponse());
81                     Map<String,String> failureAction = new HashMap<>();
82                     //if(!checkLastVM(vservers,vm.getId())) {
83                         //failureAction.put(Constants.ResponseAction.STOP.getAction(), String.valueOf(transactionId+1));
84                         failureAction.put(ResponseAction.STOP.getAction(), Boolean.TRUE.toString());
85                         failureResponse.setResponseAction(failureAction);
86                         transactionStop.addResponse(failureResponse);
87                     //}
88                 }
89                 transactionList.add(transactionStop);
90                 Transaction transactionStart = new Transaction();
91                 transactionStart.setTransactionId(transactionId);
92                 transactionIds.add(transactionId++);
93                 transactionStart.setAction(Constants.Action.START.getActionType());
94                 transactionStart.setActionLevel(Constants.ActionLevel.VM.getAction());
95                 ActionIdentifier actionIdentifierStart = new ActionIdentifier();
96                 actionIdentifierStart.setvServerId(vm.getId());
97                 transactionStart.setActionIdentifier(actionIdentifierStart);
98                 payload = payloadGenerator.getPayload(input, vmId, url);
99                 transactionStart.setPayload(payload);
100                 if (vservers.size()>1) {
101                     Response failureResponse = new Response();
102                     failureResponse.setResponseMessage(Constants.ResponseMessage.FAILURE.getResponse());
103                     Map<String,String> failureAction = new HashMap<>();
104                     //if(!checkLastVM(vservers,vm.getId())) {
105                         //failureAction.put(Constants.ResponseAction.JUMP.getAction(),transactionId.toString());
106                         failureAction.put(ResponseAction.STOP.getAction(), Boolean.TRUE.toString());
107                         failureResponse.setResponseAction(failureAction);
108                         transactionStart.addResponse(failureResponse);
109                     //}
110                 }
111                 transactionList.add(transactionStart);
112             }
113         return transactionList;
114     }
115     
116     private boolean vmSupportsRestart(SequenceGeneratorInput input, Vserver vm) {
117         boolean vmSupported = true;
118         if (input.getCapability() == null) {
119             logger.info("vmSupportsRestart: " + "Capabilities model is null, returning vmSupported=" + vmSupported);
120             return vmSupported;
121         }
122         Map<String, List<String>> vmCapabilities = input.getCapability().getVmCapabilities();
123         if (vmCapabilities != null) {
124             if (!vmCapabilities.isEmpty()) {
125                 List<String> vmCapsForThisAction = vmCapabilities.get(Action.RESTART.getActionType());
126                 if (vmCapsForThisAction != null) {
127                     vmSupported = false;
128                     if (!vmCapsForThisAction.isEmpty()) {
129                         if (vm.getVnfc() != null) {
130                             String vnfcFunctionCode = vm.getVnfc().getVnfcFunctionCode();
131                             if (vnfcFunctionCode != null && !vnfcFunctionCode.isEmpty()) {
132                                 for (String s : vmCapabilities.get(Action.RESTART.getActionType()) ) {
133                                     if (s.equalsIgnoreCase(vnfcFunctionCode)) {
134                                         vmSupported = true;
135                                         logger.info("vmSupportsRestart: vnfcFunctionCode=" + vnfcFunctionCode + " found in vmCapabilities");
136                                         break;
137                                     }
138                                 }
139                             } else {
140                                 logger.info("vmSupportsRestart: " + "Inventory vnfcFunctionCode is null or empty");
141                             }
142                         } else {
143                             logger.info("vmSupportsRestart: " + "Inventory vnfc is null or empty");
144                         } 
145                     } else {
146                         logger.info("vmSupportsRestart: " + "Given action in vm entry in Capabilities model is empty");
147                     }
148                 } else {
149                     logger.info("vmSupportsRestart: " + "Given action in vm entry in Capabilities model is null");
150                 }
151             } else {
152                 logger.info("vmSupportsRestart: " + "Vm entry in Capabilities model is empty");
153             }
154         } else {
155             logger.info("vmSupportsRestart: " + "Vm entry in Capabilities model is null");
156         }
157
158         logger.info("vmSupportsRestart: " + "returning vmSupported=" + vmSupported + ", " + ((vmSupported)?"including":"excluding") + " vm=" + vm.getId());
159         return vmSupported;
160     }
161
162
163     private boolean checkLastVM(List<Vserver> vservers, String vmId){
164         Vserver vm= vservers.get(vservers.size()-1);
165         return vm.getId().equals(vmId);
166     }
167 }