a5d23da1446b9325553d895004a368b1c1798f3b
[so.git] /
1 package org.onap.so.bpmn.infrastructure.appc.tasks;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Optional;
8 import org.onap.aai.domain.yang.Vserver;
9 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
10 import org.onap.aaiclient.client.aai.entities.Relationships;
11 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
12 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
13 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
14 import org.onap.appc.client.lcm.model.Action;
15 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerTaskRequest;
16 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVm;
17 import org.onap.so.appc.orchestrator.service.beans.ApplicationControllerVnf;
18 import org.onap.so.bpmn.common.BuildingBlockExecution;
19 import org.onap.so.bpmn.core.json.JsonUtils;
20 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
21 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
22 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
23 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
24 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
25 import org.onap.so.client.exception.BBObjectNotFoundException;
26 import org.onap.so.client.exception.ExceptionBuilder;
27 import org.onap.so.client.orchestration.AAIVnfResources;
28 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
29 import org.onap.so.db.catalog.client.CatalogDbClient;
30 import org.onap.so.exceptions.ValidationException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 @Component
39 public class AppcOrchestratorPreProcessor {
40     private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
41     public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
42     private static final ObjectMapper objectMapper = new ObjectMapper();
43
44     @Autowired
45     private ExceptionBuilder exceptionUtil;
46     @Autowired
47     private ExtractPojosForBB extractPojosForBB;
48     @Autowired
49     private CatalogDbClient catalogDbClient;
50     @Autowired
51     private AAIVnfResources aaiVnfResources;
52
53     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
54         try {
55             Action action = Action.valueOf(actionName);
56             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
57             appcTaskRequest.setAction(action);
58             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
59             GenericVnf vnf = null;
60             try {
61                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
62             } catch (BBObjectNotFoundException e) {
63                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
64             }
65             String vnfId = null;
66             String vnfName = null;
67             String vnfType = null;
68             String vnfHostIpAddress = null;
69
70             if (vnf != null) {
71                 vnfId = vnf.getVnfId();
72                 vnfName = vnf.getVnfName();
73                 vnfType = vnf.getVnfType();
74                 vnfHostIpAddress = vnf.getIpv4OamAddress();
75             }
76             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
77
78             String aicIdentity = execution.getVariable("aicIdentity");
79             String identityUrl = execution.getVariable("identityUrl");
80             appcTaskRequest.setIdentityUrl(identityUrl);
81
82             String requestorId = gBBInput.getRequestContext().getRequestorId();
83             appcTaskRequest.setRequestorId(requestorId);
84
85             if (gBBInput.getRequestContext().getRequestParameters() != null) {
86                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
87                 if (payload == null) {
88                     payload = "";
89                 }
90                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing_software_version");
91                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
92                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new_software_version");
93                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
94                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations_timeout");
95                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
96
97                 Map<String, String> configMap = new HashMap<>();
98                 String configParamsStr = JsonUtils.getJsonValue(payload, "configuration_parameters");
99                 if (configParamsStr != null) {
100                     configMap =
101                             objectMapper.readValue(configParamsStr, new TypeReference<HashMap<String, String>>() {});
102                 }
103                 appcTaskRequest.setConfigParams(configMap);
104             }
105             ControllerSelectionReference controllerSelectionReference = catalogDbClient
106                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
107             String controllerType = null;
108             if (controllerSelectionReference != null) {
109                 controllerType = controllerSelectionReference.getControllerName();
110             } else {
111                 controllerType = CONTROLLER_TYPE_DEFAULT;
112             }
113             appcTaskRequest.setControllerType(controllerType);
114
115             execution.setVariable("vmIdList", null);
116             execution.setVariable("vserverIdList", null);
117             execution.setVariable("vmIndex", 0);
118             execution.setVariable("vmIdListSize", 0);
119             VfModule vfModule = null;
120             try {
121                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
122             } catch (BBObjectNotFoundException e) {
123             }
124             if (vfModule != null) {
125             }
126             if (action.equals(Action.Snapshot)) {
127                 try {
128                     getVserversForAppc(execution, vnf);
129                 } catch (Exception e) {
130                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
131                 }
132             }
133
134             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
135             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
136             applicationControllerVnf.setVnfId(vnfId);
137             applicationControllerVnf.setVnfName(vnfName);
138             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
139
140             verifyApplicationControllerTaskRequest(execution, appcTaskRequest);
141
142             execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
143             logger.debug("SET APPC ORCHESTRATOR REQUEST");
144         } catch (Exception e) {
145             logger.error("Error building ApplicationControllerTaskRequest Object", e.getMessage());
146             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
147         }
148     }
149
150     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
151         try {
152             ApplicationControllerTaskRequest appcTaskRequest =
153                     (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
154             ArrayList<String> vmIdList = execution.getVariable("vmIdList");
155             ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
156             Integer vmIndex = (Integer) execution.getVariable("vmIndex");
157
158             if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
159                 execution.setVariable("vmIdListSize", vmIdList.size());
160                 if (vmIndex < vmIdList.size()) {
161                     ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
162                     applicationControllerVm.setVmId(vmIdList.get(vmIndex));
163                     applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
164                     if (appcTaskRequest.getApplicationControllerVnf() == null) {
165                         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
166                         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
167                     }
168                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
169                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
170                     vmIndex++;
171                     execution.setVariable("vmIndex", vmIndex);
172                 }
173             }
174         } catch (Exception e) {
175             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
176             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
177         }
178     }
179
180     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
181         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
182
183         if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
184             Relationships relationships = aaiRW.getRelationships().get();
185             if (relationships != null) {
186                 List<AAIResourceUri> vserverUris = relationships.getRelatedUris(Types.VSERVER);
187                 ArrayList<String> vserverIds = new ArrayList<String>();
188                 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
189                 for (AAIResourceUri j : vserverUris) {
190                     String vserverId = j.getURIKeys().get(AAIFluentTypeBuilder.Types.VSERVER.getUriParams().vserverId);
191                     vserverIds.add(vserverId);
192                     Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
193                     if (oVserver.isPresent()) {
194                         Vserver vserver = oVserver.get();
195                         String vserverSelfLink = vserver.getVserverSelflink();
196                         vserverSelfLinks.add(vserverSelfLink);
197                     }
198                 }
199                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
200                 logger.debug("vserverIdsArray is: {}", vserverIds);
201                 execution.setVariable("vmIdList", vserverSelfLinks);
202                 execution.setVariable("vserverIdList", vserverIds);
203             }
204         }
205     }
206
207     protected void verifyApplicationControllerTaskRequest(BuildingBlockExecution execution,
208             ApplicationControllerTaskRequest appcTaskRequest) throws ValidationException {
209         String errorMessage = null;
210         switch (appcTaskRequest.getAction()) {
211             case QuiesceTraffic:
212                 if (appcTaskRequest.getOperationsTimeout() == null
213                         || appcTaskRequest.getOperationsTimeout().isEmpty()) {
214                     errorMessage = "APPC action QuiesceTraffic is missing operations_timeout parameter. ";
215                 }
216                 break;
217             case UpgradePreCheck:
218             case UpgradePostCheck:
219             case UpgradeBackup:
220             case UpgradeSoftware:
221                 if (appcTaskRequest.getExistingSoftwareVersion() == null
222                         || appcTaskRequest.getExistingSoftwareVersion().isEmpty()) {
223                     errorMessage =
224                             "APPC action " + appcTaskRequest.getAction() + " is missing existing_software parameter. ";
225                 }
226                 if (appcTaskRequest.getNewSoftwareVersion() == null
227                         || appcTaskRequest.getNewSoftwareVersion().isEmpty()) {
228                     errorMessage =
229                             "APPC action " + appcTaskRequest.getAction() + " is missing new_software parameter. ";
230                 }
231                 break;
232             case Snapshot:
233                 if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm() != null) {
234                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId() == null
235                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVmId()
236                                     .isEmpty()) {
237                         errorMessage = "APPC action Snapshot is missing vmId parameter. ";
238                     }
239                     if (appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm()
240                             .getVserverId() == null
241                             || appcTaskRequest.getApplicationControllerVnf().getApplicationControllerVm().getVserverId()
242                                     .isEmpty()) {
243                         errorMessage = "APPC action Snapshot is missing vserverId parameter. ";
244                     }
245                 }
246                 break;
247             case ConfigModify:
248                 if (appcTaskRequest.getConfigParams().isEmpty() || appcTaskRequest.getConfigParams() == null) {
249                     errorMessage = "APPC action ConfigModify is missing Configuration parameters. ";
250                 }
251                 break;
252             default:
253                 break;
254         }
255         if (errorMessage != null) {
256             logger.debug("verifyApplicationControllerTaskRequest() failed with " + errorMessage);
257             throw new ValidationException(errorMessage, false);
258         }
259         return;
260     }
261 }