Adding CNF Upgrade Code Enhancements
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / sdnc / mapper / VfModuleTopologyOperationRequestMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.sdnc.mapper;
24
25 import java.net.URI;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.onap.so.logger.LoggingAnchor;
29 import org.onap.sdnc.northbound.client.model.GenericResourceApiParam;
30 import org.onap.sdnc.northbound.client.model.GenericResourceApiParamParam;
31 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
34 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceinformationServiceInformation;
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleResponseInformation;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduleinformationVfModuleInformation;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmodulerequestinputVfModuleRequestInput;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfinformationVnfInformation;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
47 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
48 import org.onap.so.client.exception.MapperException;
49 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
50 import org.onap.so.client.sdnc.beans.SDNCSvcOperation;
51 import org.onap.logging.filter.base.ErrorCode;
52 import org.onap.so.logger.MessageEnum;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Component;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58
59 @Component
60 public class VfModuleTopologyOperationRequestMapper {
61     private static final Logger logger = LoggerFactory.getLogger(VfModuleTopologyOperationRequestMapper.class);
62
63     @Autowired
64     private GeneralTopologyObjectMapper generalTopologyObjectMapper;
65
66     public GenericResourceApiVfModuleOperationInformation reqMapper(SDNCSvcOperation svcOperation,
67             SDNCSvcAction svcAction, VfModule vfModule, VolumeGroup volumeGroup, GenericVnf vnf,
68             ServiceInstance serviceInstance, Customer customer, CloudRegion cloudRegion, RequestContext requestContext,
69             String sdncAssignResponse, URI callbackURL) throws MapperException {
70         GenericResourceApiVfModuleOperationInformation req = new GenericResourceApiVfModuleOperationInformation();
71
72         boolean includeModelInformation = false;
73
74         GenericResourceApiRequestActionEnumeration requestAction =
75                 GenericResourceApiRequestActionEnumeration.CREATEVFMODULEINSTANCE;
76         GenericResourceApiSvcActionEnumeration genericResourceApiSvcAction =
77                 GenericResourceApiSvcActionEnumeration.ASSIGN;
78
79         if (svcAction.equals(SDNCSvcAction.ACTIVATE)) {
80             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.ACTIVATE;
81             requestAction = GenericResourceApiRequestActionEnumeration.CREATEVFMODULEINSTANCE;
82             includeModelInformation = true;
83         } else if (svcAction.equals(SDNCSvcAction.ASSIGN)) {
84             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.ASSIGN;
85             requestAction = GenericResourceApiRequestActionEnumeration.CREATEVFMODULEINSTANCE;
86             includeModelInformation = true;
87         } else if (svcAction.equals(SDNCSvcAction.DEACTIVATE)) {
88             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.DEACTIVATE;
89             requestAction = GenericResourceApiRequestActionEnumeration.DELETEVFMODULEINSTANCE;
90             includeModelInformation = true;
91         } else if (svcAction.equals(SDNCSvcAction.DELETE)) {
92             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.DELETE;
93             requestAction = GenericResourceApiRequestActionEnumeration.DELETEVFMODULEINSTANCE;
94             includeModelInformation = false;
95         } else if (svcAction.equals(SDNCSvcAction.UNASSIGN)) {
96             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.UNASSIGN;
97             requestAction = GenericResourceApiRequestActionEnumeration.DELETEVFMODULEINSTANCE;
98             includeModelInformation = false;
99         } else if (svcAction.equals(SDNCSvcAction.CHANGE_ASSIGN)) {
100             genericResourceApiSvcAction = GenericResourceApiSvcActionEnumeration.CHANGEASSIGN;
101             requestAction = GenericResourceApiRequestActionEnumeration.CREATEVFMODULEINSTANCE;
102             includeModelInformation = true;
103         }
104
105         String sdncReqId = UUID.randomUUID().toString();
106         String msoRequestId = UUID.randomUUID().toString();
107         if (requestContext != null && requestContext.getMsoRequestId() != null) {
108             msoRequestId = requestContext.getMsoRequestId();
109         }
110
111         GenericResourceApiRequestinformationRequestInformation requestInformation = generalTopologyObjectMapper
112                 .buildGenericResourceApiRequestinformationRequestInformation(msoRequestId, requestAction);
113         GenericResourceApiServiceinformationServiceInformation serviceInformation = generalTopologyObjectMapper
114                 .buildServiceInformation(serviceInstance, requestContext, customer, includeModelInformation);
115         GenericResourceApiVnfinformationVnfInformation vnfInformation =
116                 generalTopologyObjectMapper.buildVnfInformation(vnf, serviceInstance, includeModelInformation);
117         GenericResourceApiVfmoduleinformationVfModuleInformation vfModuleInformation = generalTopologyObjectMapper
118                 .buildVfModuleInformation(vfModule, vnf, serviceInstance, requestContext, includeModelInformation);
119         GenericResourceApiVfmodulerequestinputVfModuleRequestInput vfModuleRequestInput =
120                 buildVfModuleRequestInput(vfModule, volumeGroup, cloudRegion, requestContext);
121         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
122                 buildVfModuleSdncRequestHeader(sdncReqId, genericResourceApiSvcAction, callbackURL);
123
124         req.setRequestInformation(requestInformation);
125         req.setSdncRequestHeader(sdncRequestHeader);
126         req.setServiceInformation(serviceInformation);
127         req.setVnfInformation(vnfInformation);
128         req.setVfModuleInformation(vfModuleInformation);
129         req.setVfModuleRequestInput(vfModuleRequestInput);
130
131         return req;
132     }
133
134     private GenericResourceApiVfmodulerequestinputVfModuleRequestInput buildVfModuleRequestInput(VfModule vfModule,
135             VolumeGroup volumeGroup, CloudRegion cloudRegion, RequestContext requestContext) {
136         GenericResourceApiVfmodulerequestinputVfModuleRequestInput vfModuleRequestInput =
137                 new GenericResourceApiVfmodulerequestinputVfModuleRequestInput();
138         if (cloudRegion != null) {
139             vfModuleRequestInput.setTenant(cloudRegion.getTenantId());
140             vfModuleRequestInput.setAicCloudRegion(cloudRegion.getLcpCloudRegionId());
141             vfModuleRequestInput.setCloudOwner(cloudRegion.getCloudOwner());
142         }
143         if (vfModule.getVfModuleName() != null && !vfModule.getVfModuleName().equals("")) {
144             vfModuleRequestInput.setVfModuleName(vfModule.getVfModuleName());
145         }
146         GenericResourceApiParam vfModuleInputParameters = new GenericResourceApiParam();
147
148         if (requestContext != null && requestContext.getUserParams() != null) {
149             for (Map.Entry<String, Object> entry : requestContext.getUserParams().entrySet()) {
150                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
151                 paramItem.setName(entry.getKey());
152                 paramItem.setValue(generalTopologyObjectMapper.mapUserParamValue(entry.getValue()));
153                 vfModuleInputParameters.addParamItem(paramItem);
154             }
155         }
156
157         if (vfModule.getCloudParams() != null) {
158             for (Map.Entry<String, String> entry : vfModule.getCloudParams().entrySet()) {
159                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
160                 paramItem.setName(entry.getKey());
161                 paramItem.setValue(entry.getValue());
162                 vfModuleInputParameters.addParamItem(paramItem);
163             }
164         }
165
166         if (volumeGroup != null) {
167             GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
168             paramItem.setName("volume-group-id");
169             paramItem.setValue(volumeGroup.getVolumeGroupId());
170             vfModuleInputParameters.addParamItem(paramItem);
171         }
172         vfModuleRequestInput.setVfModuleInputParameters(vfModuleInputParameters);
173
174         return vfModuleRequestInput;
175     }
176
177     private GenericResourceApiSdncrequestheaderSdncRequestHeader buildVfModuleSdncRequestHeader(String sdncReqId,
178             GenericResourceApiSvcActionEnumeration svcAction, URI callbackUrl) {
179         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
180                 new GenericResourceApiSdncrequestheaderSdncRequestHeader();
181         sdncRequestHeader.setSvcRequestId(sdncReqId);
182         sdncRequestHeader.setSvcAction(svcAction);
183         sdncRequestHeader.setSvcNotificationUrl(callbackUrl.toString());
184         return sdncRequestHeader;
185     }
186
187     public String buildObjectPath(String sdncAssignResponse) {
188         String objectPath = null;
189         if (sdncAssignResponse != null) {
190             ObjectMapper mapper = new ObjectMapper();
191             try {
192                 GenericResourceApiVfModuleResponseInformation assignResponseInfo =
193                         mapper.readValue(sdncAssignResponse, GenericResourceApiVfModuleResponseInformation.class);
194                 objectPath = assignResponseInfo.getVfModuleResponseInformation().getObjectPath();
195             } catch (Exception e) {
196                 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), e.getMessage(), "BPMN",
197                         ErrorCode.UnknownError.getValue(), e.getMessage());
198             }
199         }
200         return objectPath;
201     }
202 }