4b967c7bc4f32233b6e7ca626154759b98bdbb42
[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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class AppcOrchestratorPreProcessor {
34     private static final Logger logger = LoggerFactory.getLogger(AppcOrchestratorPreProcessor.class);
35     public static final String CONTROLLER_TYPE_DEFAULT = "APPC";
36
37     @Autowired
38     private ExceptionBuilder exceptionUtil;
39     @Autowired
40     private ExtractPojosForBB extractPojosForBB;
41     @Autowired
42     private CatalogDbClient catalogDbClient;
43     @Autowired
44     private AAIVnfResources aaiVnfResources;
45
46     public void buildAppcTaskRequest(BuildingBlockExecution execution, String actionName) {
47         try {
48             Action action = Action.valueOf(actionName);
49             ApplicationControllerTaskRequest appcTaskRequest = new ApplicationControllerTaskRequest();
50             appcTaskRequest.setAction(action);
51             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
52             GenericVnf vnf = null;
53             try {
54                 vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
55             } catch (BBObjectNotFoundException e) {
56                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "No valid VNF exists");
57             }
58             String vnfId = null;
59             String vnfName = null;
60             String vnfType = null;
61             String vnfHostIpAddress = null;
62
63             if (vnf != null) {
64                 vnfId = vnf.getVnfId();
65                 vnfName = vnf.getVnfName();
66                 vnfType = vnf.getVnfType();
67                 vnfHostIpAddress = vnf.getIpv4OamAddress();
68             }
69             String msoRequestId = gBBInput.getRequestContext().getMsoRequestId();
70
71             String aicIdentity = execution.getVariable("aicIdentity");
72             String identityUrl = execution.getVariable("identityUrl");
73             appcTaskRequest.setIdentityUrl(identityUrl);
74
75             if (gBBInput.getRequestContext().getRequestParameters() != null) {
76                 String payload = gBBInput.getRequestContext().getRequestParameters().getPayload();
77                 if (payload == null) {
78                     payload = "";
79                 }
80                 String existingSoftwareVersion = JsonUtils.getJsonValue(payload, "existing-software-version");
81                 appcTaskRequest.setExistingSoftwareVersion(existingSoftwareVersion);
82                 String newSoftwareVersion = JsonUtils.getJsonValue(payload, "new-software-version");
83                 appcTaskRequest.setNewSoftwareVersion(newSoftwareVersion);
84                 String operationsTimeout = JsonUtils.getJsonValue(payload, "operations-timeout");
85                 appcTaskRequest.setOperationsTimeout(operationsTimeout);
86             }
87
88             ControllerSelectionReference controllerSelectionReference = catalogDbClient
89                     .getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, action.toString());
90             String controllerType = null;
91             if (controllerSelectionReference != null) {
92                 controllerType = controllerSelectionReference.getControllerName();
93             } else {
94                 controllerType = CONTROLLER_TYPE_DEFAULT;
95             }
96             appcTaskRequest.setControllerType(controllerType);
97
98             execution.setVariable("vmIdList", null);
99             execution.setVariable("vserverIdList", null);
100             execution.setVariable("vmIndex", 0);
101             execution.setVariable("vmIdListSize", 0);
102
103             String vfModuleId = null;
104             VfModule vfModule = null;
105             try {
106                 vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
107             } catch (BBObjectNotFoundException e) {
108             }
109             if (vfModule != null) {
110                 vfModuleId = vfModule.getVfModuleId();
111             }
112             if (action.equals(Action.Snapshot)) {
113                 try {
114                     getVserversForAppc(execution, vnf);
115                 } catch (Exception e) {
116                     logger.warn("Unable to retrieve vservers for vnf: " + vnfId);
117                 }
118             }
119
120             ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
121             applicationControllerVnf.setVnfHostIpAddress(vnfHostIpAddress);
122             applicationControllerVnf.setVnfId(vnfId);
123             applicationControllerVnf.setVnfName(vnfName);
124             appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
125
126             execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
127         } catch (Exception e) {
128             logger.error("Error building ApplicationControllerTaskRequest Object", e);
129             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
130         }
131     }
132
133     public void addVmInfoToAppcTaskRequest(BuildingBlockExecution execution) {
134         try {
135             ApplicationControllerTaskRequest appcTaskRequest =
136                     (ApplicationControllerTaskRequest) execution.getVariable("appcOrchestratorRequest");
137             ArrayList<String> vmIdList = execution.getVariable("vmIdList");
138             ArrayList<String> vserverIdList = execution.getVariable("vserverIdList");
139             Integer vmIndex = (Integer) execution.getVariable("vmIndex");
140
141             if (vmIdList != null && !vmIdList.isEmpty() && vserverIdList != null && !vserverIdList.isEmpty()) {
142                 execution.setVariable("vmIdListSize", vmIdList.size());
143                 if (vmIndex < vmIdList.size()) {
144                     ApplicationControllerVm applicationControllerVm = new ApplicationControllerVm();
145                     applicationControllerVm.setVmId(vmIdList.get(vmIndex));
146                     applicationControllerVm.setVserverId(vserverIdList.get(vmIndex));
147                     if (appcTaskRequest.getApplicationControllerVnf() == null) {
148                         ApplicationControllerVnf applicationControllerVnf = new ApplicationControllerVnf();
149                         appcTaskRequest.setApplicationControllerVnf(applicationControllerVnf);
150                     }
151                     appcTaskRequest.getApplicationControllerVnf().setApplicationControllerVm(applicationControllerVm);
152                     execution.setVariable("appcOrchestratorRequest", appcTaskRequest);
153                     vmIndex++;
154                     execution.setVariable("vmIndex", vmIndex);
155                 }
156             }
157         } catch (Exception e) {
158             logger.error("Error adding VM info to ApplicationControllerTaskRequest Object", e);
159             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, e);
160         }
161     }
162
163     protected void getVserversForAppc(BuildingBlockExecution execution, GenericVnf vnf) throws Exception {
164         AAIResultWrapper aaiRW = aaiVnfResources.queryVnfWrapperById(vnf);
165
166         if (aaiRW != null && aaiRW.getRelationships().isPresent()) {
167             Relationships relationships = aaiRW.getRelationships().get();
168             if (relationships != null) {
169                 List<AAIResourceUri> vserverUris = relationships.getRelatedAAIUris(AAIObjectType.VSERVER);
170                 ArrayList<String> vserverIds = new ArrayList<String>();
171                 ArrayList<String> vserverSelfLinks = new ArrayList<String>();
172                 for (AAIResourceUri j : vserverUris) {
173                     String vserverId = j.getURIKeys().get("vserver-id");
174                     vserverIds.add(vserverId);
175                     Optional<Vserver> oVserver = aaiVnfResources.getVserver(j);
176                     if (oVserver.isPresent()) {
177                         Vserver vserver = oVserver.get();
178                         String vserverSelfLink = vserver.getVserverSelflink();
179                         vserverSelfLinks.add(vserverSelfLink);
180                     }
181                 }
182                 logger.debug("vmIdsArray is: {}", vserverSelfLinks);
183                 logger.debug("vserverIdsArray is: {}", vserverIds);
184                 execution.setVariable("vmIdList", vserverSelfLinks);
185                 execution.setVariable("vserverIdList", vserverIds);
186             }
187         }
188     }
189 }