6ddb292a6c026e0f1a6bde8d9473c398aeb1d8bb
[so.git] /
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.client.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.so.logger.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 = false;
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         }
100
101         String sdncReqId = UUID.randomUUID().toString();
102         String msoRequestId = UUID.randomUUID().toString();
103         if (requestContext != null && requestContext.getMsoRequestId() != null) {
104             msoRequestId = requestContext.getMsoRequestId();
105         }
106
107         GenericResourceApiRequestinformationRequestInformation requestInformation = generalTopologyObjectMapper
108                 .buildGenericResourceApiRequestinformationRequestInformation(msoRequestId, requestAction);
109         GenericResourceApiServiceinformationServiceInformation serviceInformation = generalTopologyObjectMapper
110                 .buildServiceInformation(serviceInstance, requestContext, customer, includeModelInformation);
111         GenericResourceApiVnfinformationVnfInformation vnfInformation =
112                 generalTopologyObjectMapper.buildVnfInformation(vnf, serviceInstance, includeModelInformation);
113         GenericResourceApiVfmoduleinformationVfModuleInformation vfModuleInformation = generalTopologyObjectMapper
114                 .buildVfModuleInformation(vfModule, vnf, serviceInstance, requestContext, includeModelInformation);
115         GenericResourceApiVfmodulerequestinputVfModuleRequestInput vfModuleRequestInput =
116                 buildVfModuleRequestInput(vfModule, volumeGroup, cloudRegion, requestContext);
117         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
118                 buildVfModuleSdncRequestHeader(sdncReqId, genericResourceApiSvcAction, callbackURL);
119
120         req.setRequestInformation(requestInformation);
121         req.setSdncRequestHeader(sdncRequestHeader);
122         req.setServiceInformation(serviceInformation);
123         req.setVnfInformation(vnfInformation);
124         req.setVfModuleInformation(vfModuleInformation);
125         req.setVfModuleRequestInput(vfModuleRequestInput);
126
127         return req;
128     }
129
130     private GenericResourceApiVfmodulerequestinputVfModuleRequestInput buildVfModuleRequestInput(VfModule vfModule,
131             VolumeGroup volumeGroup, CloudRegion cloudRegion, RequestContext requestContext) {
132         GenericResourceApiVfmodulerequestinputVfModuleRequestInput vfModuleRequestInput =
133                 new GenericResourceApiVfmodulerequestinputVfModuleRequestInput();
134         if (cloudRegion != null) {
135             vfModuleRequestInput.setTenant(cloudRegion.getTenantId());
136             vfModuleRequestInput.setAicCloudRegion(cloudRegion.getLcpCloudRegionId());
137             vfModuleRequestInput.setCloudOwner(cloudRegion.getCloudOwner());
138         }
139         if (vfModule.getVfModuleName() != null && !vfModule.getVfModuleName().equals("")) {
140             vfModuleRequestInput.setVfModuleName(vfModule.getVfModuleName());
141         }
142         GenericResourceApiParam vfModuleInputParameters = new GenericResourceApiParam();
143
144         if (requestContext != null && requestContext.getUserParams() != null) {
145             for (Map.Entry<String, Object> entry : requestContext.getUserParams().entrySet()) {
146                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
147                 paramItem.setName(entry.getKey());
148                 paramItem.setValue(generalTopologyObjectMapper.mapUserParamValue(entry.getValue()));
149                 vfModuleInputParameters.addParamItem(paramItem);
150             }
151         }
152
153         if (vfModule.getCloudParams() != null) {
154             for (Map.Entry<String, String> entry : vfModule.getCloudParams().entrySet()) {
155                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
156                 paramItem.setName(entry.getKey());
157                 paramItem.setValue(entry.getValue());
158                 vfModuleInputParameters.addParamItem(paramItem);
159             }
160         }
161
162         if (volumeGroup != null) {
163             GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
164             paramItem.setName("volume-group-id");
165             paramItem.setValue(volumeGroup.getVolumeGroupId());
166             vfModuleInputParameters.addParamItem(paramItem);
167         }
168         vfModuleRequestInput.setVfModuleInputParameters(vfModuleInputParameters);
169
170         return vfModuleRequestInput;
171     }
172
173     private GenericResourceApiSdncrequestheaderSdncRequestHeader buildVfModuleSdncRequestHeader(String sdncReqId,
174             GenericResourceApiSvcActionEnumeration svcAction, URI callbackUrl) {
175         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
176                 new GenericResourceApiSdncrequestheaderSdncRequestHeader();
177         sdncRequestHeader.setSvcRequestId(sdncReqId);
178         sdncRequestHeader.setSvcAction(svcAction);
179         sdncRequestHeader.setSvcNotificationUrl(callbackUrl.toString());
180         return sdncRequestHeader;
181     }
182
183     public String buildObjectPath(String sdncAssignResponse) {
184         String objectPath = null;
185         if (sdncAssignResponse != null) {
186             ObjectMapper mapper = new ObjectMapper();
187             try {
188                 GenericResourceApiVfModuleResponseInformation assignResponseInfo =
189                         mapper.readValue(sdncAssignResponse, GenericResourceApiVfModuleResponseInformation.class);
190                 objectPath = assignResponseInfo.getVfModuleResponseInformation().getObjectPath();
191             } catch (Exception e) {
192                 logger.error(LoggingAnchor.FIVE, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), e.getMessage(), "BPMN",
193                         ErrorCode.UnknownError.getValue(), e.getMessage());
194             }
195         }
196         return objectPath;
197     }
198 }