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