2f5080d0bb76afda5bbda95526b8ad88c0a402e1
[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.Response;
36 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
37 import org.onap.appc.seqgen.objects.Transaction;
38
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Map;
43
44 public class RestartSequenceGenerator implements SequenceGenerator{
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestartSequenceGenerator.class);
46
47     @Override
48     public List<Transaction> generateSequence(SequenceGeneratorInput input) throws Exception {
49             logger.info("Generating sequence without dependency model");
50             return generateSequenceWithOutDependency(input);
51     }
52
53     private List<Transaction> generateSequenceWithOutDependency(SequenceGeneratorInput input) throws Exception{
54         String payload = null;
55         List<Transaction> transactionList = new LinkedList<>();
56         Integer transactionId = 1;
57         List<Vserver> vservers = input.getInventoryModel().getVnf().getVservers();
58         List<Integer> transactionIds = new LinkedList<>();
59         PayloadGenerator payloadGenerator = new PayloadGenerator();
60             for (Vserver vm : vservers) {
61                 // check vm-Restart-capabilities for this vm's vnfc-function-code (before incrementing transactionId)
62                 String vmVnfcFunctionCode = vm.getVnfc().getVnfcFunctionCode();
63                 if (!vmSupportsRestart(input, vmVnfcFunctionCode)) {
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                     //{
84                         failureAction.put(Constants.ResponseAction.STOP.getAction(), String.valueOf(transactionId+1));
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                     //{
106                     //failureAction.put(Constants.ResponseAction.JUMP.getAction(),transactionId.toString());
107                         failureAction.put(Constants.ResponseAction.STOP.getAction(),transactionId.toString());
108                         failureResponse.setResponseAction(failureAction);
109                         transactionStart.addResponse(failureResponse);
110                     //}
111                 }
112                 transactionList.add(transactionStart);
113             }
114         return transactionList;
115     }
116     
117     private boolean vmSupportsRestart(SequenceGeneratorInput input, String vnfcFunctionCode) {
118         boolean vmSupported = true;
119         if (input.getCapability() == null) {
120             logger.info("vmSupportsRestart: " + "Capabilities model is null, returning vmSupported=" + vmSupported);
121             return vmSupported;
122         }
123         Map<String, List<String>> vmCapabilities = input.getCapability().getVmCapabilities();
124         logger.info("vmSupportsRestart: vnfcFunctionCode=" + vnfcFunctionCode + ", vmCapabilities=" + vmCapabilities);
125         if (vmCapabilities != null) {
126             if (!vmCapabilities.isEmpty()) {
127                 vmSupported = false;
128                 if (vmCapabilities.get(Action.RESTART.getActionType()) != null) {
129                     if (vnfcFunctionCode != null && !vnfcFunctionCode.isEmpty()) {
130                         for (String enabledFuncCode : vmCapabilities.get(Action.RESTART.getActionType()) ) {
131                             if (enabledFuncCode.equalsIgnoreCase(vnfcFunctionCode)) {
132                                 vmSupported = true;
133                                 logger.info("vmSupportsRestart: vnfcFunctionCode=" + vnfcFunctionCode + " found in vmCapabilties");
134                                 break;
135                             }
136                         }
137                     } else {
138                         logger.info("vmSupportsRestart: " + "Inventory vnfcFunctionCode is null or empty");
139                     }
140                 } else {
141                     logger.info("vmSupportsRestart: " + "Given action in vm entry in Capabilities model is null");
142                 }
143             } else {
144                 logger.info("vmSupportsRestart: " + "Vm entry in Capabilities model is empty");
145             }
146         } else {
147             logger.info("vmSupportsRestart: " + "Vm entry in Capabilities model is null");
148         }
149
150         logger.info("vmSupportsRestart: " + "returning vmSupported=" + vmSupported);
151         return vmSupported;
152     }
153
154     private boolean checkLastVM(List<Vserver> vservers, String vmId){
155         Vserver vm= vservers.get(vservers.size()-1);
156         return vm.getId().equals(vmId);
157     }
158 }