bef4ec346562bea122655649d48587922b7863cb
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.sdnc.mapper;
22
23 import java.net.URI;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.apache.commons.lang.StringUtils;
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.GenericResourceApiVnfOperationInformation;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfinformationVnfInformation;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfrequestinputVnfRequestInput;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
44 import org.onap.so.bpmn.servicedecomposition.generalobjects.License;
45 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
46 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoInstanceGroup;
47 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
48 import org.onap.so.client.sdnc.beans.SDNCSvcOperation;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.stereotype.Component;
51
52 @Component
53 public class VnfTopologyOperationRequestMapper {
54
55     @Autowired
56     private GeneralTopologyObjectMapper generalTopologyObjectMapper;
57
58     public GenericResourceApiVnfOperationInformation reqMapper(SDNCSvcOperation svcOperation, SDNCSvcAction svcAction,
59             GenericResourceApiRequestActionEnumeration requestAction, GenericVnf vnf, ServiceInstance serviceInstance,
60             Customer customer, CloudRegion cloudRegion, RequestContext requestContext, boolean homing,
61             URI callbackUrl) {
62         String sdncReqId = UUID.randomUUID().toString();
63         String msoRequestId = UUID.randomUUID().toString();
64         if (requestContext != null && requestContext.getMsoRequestId() != null) {
65             msoRequestId = requestContext.getMsoRequestId();
66         }
67         GenericResourceApiVnfOperationInformation req = new GenericResourceApiVnfOperationInformation();
68         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
69                 generalTopologyObjectMapper.buildSdncRequestHeader(svcAction, sdncReqId, callbackUrl.toString());
70         GenericResourceApiRequestinformationRequestInformation requestInformation = generalTopologyObjectMapper
71                 .buildGenericResourceApiRequestinformationRequestInformation(msoRequestId, requestAction);
72         GenericResourceApiServiceinformationServiceInformation serviceInformation =
73                 generalTopologyObjectMapper.buildServiceInformation(serviceInstance, requestContext, customer, true);
74         GenericResourceApiVnfinformationVnfInformation vnfInformation =
75                 generalTopologyObjectMapper.buildVnfInformation(vnf, serviceInstance, true);
76         GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput =
77                 new GenericResourceApiVnfrequestinputVnfRequestInput();
78
79         vnfRequestInput.setTenant(cloudRegion.getTenantId());
80         vnfRequestInput.setAicCloudRegion(cloudRegion.getLcpCloudRegionId());
81         vnfRequestInput.setCloudOwner(cloudRegion.getCloudOwner());
82
83         if (StringUtils.isNotBlank(vnf.getVnfName())) {
84             vnfRequestInput.setVnfName(vnf.getVnfName());
85         }
86
87         req.setRequestInformation(requestInformation);
88         req.setSdncRequestHeader(sdncRequestHeader);
89         req.setServiceInformation(serviceInformation);
90         req.setVnfInformation(vnfInformation);
91
92         GenericResourceApiParam vnfInputParameters = new GenericResourceApiParam();
93         if (requestContext != null && requestContext.getUserParams() != null) {
94             for (Map.Entry<String, Object> entry : requestContext.getUserParams().entrySet()) {
95                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
96                 paramItem.setName(entry.getKey());
97                 paramItem.setValue(generalTopologyObjectMapper.mapUserParamValue(entry.getValue()));
98                 vnfInputParameters.addParamItem(paramItem);
99                 vnfRequestInput.setVnfInputParameters(vnfInputParameters);
100             }
101         }
102         if (vnf.getCloudParams() != null) {
103             for (Map.Entry<String, String> entry : vnf.getCloudParams().entrySet()) {
104                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
105                 paramItem.setName(entry.getKey());
106                 paramItem.setValue(entry.getValue());
107                 vnfInputParameters.addParamItem(paramItem);
108             }
109         }
110         if (homing) {
111             License license = vnf.getLicense();
112             if (license != null) {
113                 if (license.getEntitlementPoolUuids() != null && !license.getEntitlementPoolUuids().isEmpty()) {
114                     String entitlementPoolUuid = license.getEntitlementPoolUuids().get(0);
115                     GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
116                     paramItem.setName("entitlement_assignment_group_uuid");
117                     paramItem.setValue(entitlementPoolUuid);
118                     vnfInputParameters.addParamItem(paramItem);
119                 }
120                 if (license.getLicenseKeyGroupUuids() != null && !license.getLicenseKeyGroupUuids().isEmpty()) {
121                     String licenseKeyGroupUuid = license.getLicenseKeyGroupUuids().get(0);
122                     GenericResourceApiParamParam paramItem2 = new GenericResourceApiParamParam();
123                     paramItem2.setName("license_assignment_group_uuid");
124                     paramItem2.setValue(licenseKeyGroupUuid);
125                     vnfInputParameters.addParamItem(paramItem2);
126                 }
127             }
128         }
129         List<InstanceGroup> instanceGroups = vnf.getInstanceGroups();
130         List<GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds> networkInstanceGroupIdList =
131                 new ArrayList<>();
132
133         for (InstanceGroup instanceGroup : instanceGroups) {
134             if (ModelInfoInstanceGroup.TYPE_L3_NETWORK
135                     .equalsIgnoreCase(instanceGroup.getModelInfoInstanceGroup().getType())) {
136                 GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds instanceGroupId =
137                         new GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds();
138                 instanceGroupId.setVnfNetworkInstanceGroupId(instanceGroup.getId());
139                 networkInstanceGroupIdList.add(instanceGroupId);
140             }
141         }
142
143         vnfRequestInput.setVnfNetworkInstanceGroupIds(networkInstanceGroupIdList);
144         vnfRequestInput.setVnfInputParameters(vnfInputParameters);
145         req.setVnfRequestInput(vnfRequestInput);
146         return req;
147     }
148 }