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