fd0af3a4dd0658c068362b85cea07801fa8b8830
[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     /**
59      * This method is used for creating the vnf request.
60      *
61      * By these parameter it will get he detailas and prepare the request.
62      *
63      * @param svcOperation
64      * @param svcAction
65      * @param requestAction
66      * @param vnf
67      * @param serviceInstance
68      * @param customer
69      * @param cloudRegion
70      * @param requestContext
71      * @param homing
72      * @return request
73      */
74     public GenericResourceApiVnfOperationInformation reqMapper(SDNCSvcOperation svcOperation, SDNCSvcAction svcAction,
75             GenericResourceApiRequestActionEnumeration requestAction, GenericVnf vnf, ServiceInstance serviceInstance,
76             Customer customer, CloudRegion cloudRegion, RequestContext requestContext, boolean homing,
77             URI callbackUrl) {
78         String sdncReqId = UUID.randomUUID().toString();
79         String msoRequestId = UUID.randomUUID().toString();
80         if (requestContext != null && requestContext.getMsoRequestId() != null) {
81             msoRequestId = requestContext.getMsoRequestId();
82         }
83         GenericResourceApiVnfOperationInformation req = new GenericResourceApiVnfOperationInformation();
84         GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
85                 generalTopologyObjectMapper.buildSdncRequestHeader(svcAction, sdncReqId, callbackUrl.toString());
86         GenericResourceApiRequestinformationRequestInformation requestInformation = generalTopologyObjectMapper
87                 .buildGenericResourceApiRequestinformationRequestInformation(msoRequestId, requestAction);
88         GenericResourceApiServiceinformationServiceInformation serviceInformation =
89                 generalTopologyObjectMapper.buildServiceInformation(serviceInstance, requestContext, customer, true);
90         GenericResourceApiVnfinformationVnfInformation vnfInformation =
91                 generalTopologyObjectMapper.buildVnfInformation(vnf, serviceInstance, true);
92         GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput =
93                 new GenericResourceApiVnfrequestinputVnfRequestInput();
94
95         vnfRequestInput.setTenant(cloudRegion.getTenantId());
96         vnfRequestInput.setAicCloudRegion(cloudRegion.getLcpCloudRegionId());
97         vnfRequestInput.setCloudOwner(cloudRegion.getCloudOwner());
98
99         if (StringUtils.isNotBlank(vnf.getVnfName())) {
100             vnfRequestInput.setVnfName(vnf.getVnfName());
101         }
102
103         req.setRequestInformation(requestInformation);
104         req.setSdncRequestHeader(sdncRequestHeader);
105         req.setServiceInformation(serviceInformation);
106         req.setVnfInformation(vnfInformation);
107
108         GenericResourceApiParam vnfInputParameters = new GenericResourceApiParam();
109         if (requestContext != null && requestContext.getUserParams() != null) {
110             for (Map.Entry<String, Object> entry : requestContext.getUserParams().entrySet()) {
111                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
112                 paramItem.setName(entry.getKey());
113                 paramItem.setValue(generalTopologyObjectMapper.mapUserParamValue(entry.getValue()));
114                 vnfInputParameters.addParamItem(paramItem);
115                 vnfRequestInput.setVnfInputParameters(vnfInputParameters);
116             }
117         }
118         if (vnf.getCloudParams() != null) {
119             for (Map.Entry<String, String> entry : vnf.getCloudParams().entrySet()) {
120                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
121                 paramItem.setName(entry.getKey());
122                 paramItem.setValue(entry.getValue());
123                 vnfInputParameters.addParamItem(paramItem);
124             }
125         }
126         if (homing) {
127             License license = vnf.getLicense();
128             if (license != null) {
129                 if (license.getEntitlementPoolUuids() != null && !license.getEntitlementPoolUuids().isEmpty()) {
130                     String entitlementPoolUuid = license.getEntitlementPoolUuids().get(0);
131                     GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
132                     paramItem.setName("entitlement_assignment_group_uuid");
133                     paramItem.setValue(entitlementPoolUuid);
134                     vnfInputParameters.addParamItem(paramItem);
135                 }
136                 if (license.getLicenseKeyGroupUuids() != null && !license.getLicenseKeyGroupUuids().isEmpty()) {
137                     String licenseKeyGroupUuid = license.getLicenseKeyGroupUuids().get(0);
138                     GenericResourceApiParamParam paramItem2 = new GenericResourceApiParamParam();
139                     paramItem2.setName("license_assignment_group_uuid");
140                     paramItem2.setValue(licenseKeyGroupUuid);
141                     vnfInputParameters.addParamItem(paramItem2);
142                 }
143             }
144         }
145         List<InstanceGroup> instanceGroups = vnf.getInstanceGroups();
146         List<GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds> networkInstanceGroupIdList =
147                 new ArrayList<>();
148
149         for (InstanceGroup instanceGroup : instanceGroups) {
150             if (ModelInfoInstanceGroup.TYPE_L3_NETWORK
151                     .equalsIgnoreCase(instanceGroup.getModelInfoInstanceGroup().getType())) {
152                 GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds instanceGroupId =
153                         new GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds();
154                 instanceGroupId.setVnfNetworkInstanceGroupId(instanceGroup.getId());
155                 networkInstanceGroupIdList.add(instanceGroupId);
156             }
157         }
158
159         vnfRequestInput.setVnfNetworkInstanceGroupIds(networkInstanceGroupIdList);
160         vnfRequestInput.setVnfInputParameters(vnfInputParameters);
161         req.setVnfRequestInput(vnfRequestInput);
162         return req;
163     }
164 }