b337564dab6ce0235930349c7a208ac5af0f6cc2
[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.AAIObjectType;
10 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
11 import org.onap.aaiclient.client.aai.entities.Relationships;
12 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
13 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
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
43     @Autowired
44     private ExceptionBuilder exceptionUtil;
45     @Autowired
46     private ExtractPojosForBB extractPojosForBB;
47     @Autowired
48     private CatalogDbClient catalogDbClient;
49     @Autowired
50     private AAIVnfResources aaiVnfResources;
51
52     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
53         try {
54             Action action = Action.valueOf(actionName);
55             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
56             appcTaskRequest.setAction(action);
57             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
58             GenericVnf vnf = null;
59             try {
60                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
61             } catch (BBObjectNotFoundException e) {
62                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
63             }
64             String vnfId = null;
65             String vnfName = null;
66             String vnfType = null;
67             String vnfHostIpAddress = null;
68
69             if (vnf != null) {
70                 vnfId = vnf.getVnfId();
71                 vnfName = vnf.getVnfName();
72                 vnfType = vnf.getVnfType();
73                 vnfHostIpAddress = vnf.getIpv4OamAddress();
74             }
75             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
76
77             String aicIdentity = execution.getVariable("aicIdentity");
78             String identityUrl = execution.getVariable("identityUrl");
79             appcTaskRequest.setIdentityUrl(identityUrl);
80
81             if (gBBInput.getRequestContext().getRequestParameters() != null) {
82                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
83                 if (payload == null) {
84                     payload = "";
85                 }
86                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing_software_version");
87                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
88                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new_software_version");
89                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
90                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations_timeout");
91                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
92
93                 Map<String, String> configMap = new HashMap<>();
94                 ObjectMapper objectMapper = new ObjectMapper();
95                 String configParamsStr = JsonUtils.getJsonValue(payload, "configuration_parameters");
96                 if (configParamsStr != null) {
97                     configMap =
98                             objectMapper.readValue(configParamsStr, new TypeReference<HashMap<String, String>>() {});
99                 }
100                 appcTaskRequest.setConfigParams(configMap);
101             }
102             ControllerSelectionReference controllerSelectionReference = catalogDbClient
103                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
104             String controllerType = null;
105             if (controllerSelectionReference != null) {
106                 controllerType = controllerSelectionReference.getControllerName();
107             } else {
108                 controllerType = CONTROLLER_TYPE_DEFAULT;
109             }
110             appcTaskRequest.setControllerType(controllerType);
111
112             execution.setVariable("vmIdList", null);
113             execution.setVariable("vserverIdList", null);
114             execution.setVariable("vmIndex", 0);
115             execution.setVariable("vmIdListSize", 0);
116
117             String vfModuleId = null;
118             VfModule vfModule = null;
119             try {
120                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
121             } catch (BBObjectNotFoundException e) {
122             }
123             if (vfModule != null) {
124                 vfModuleId = vfModule.getVfModuleId();
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.getRelatedAAIUris(AAIObjectType.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 }