85e5b85529ee2f7440d5a606b731f122740e4f68
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sdnc / mapper / VnfTopologyOperationRequestMapper.java
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.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27
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                 String sdncReqId = UUID.randomUUID().toString();
62                 String msoRequestId = UUID.randomUUID().toString();
63                 if (requestContext != null && requestContext.getMsoRequestId() != null) {
64                         msoRequestId = requestContext.getMsoRequestId();
65                 } 
66                 GenericResourceApiVnfOperationInformation req = new GenericResourceApiVnfOperationInformation();
67                 GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader = generalTopologyObjectMapper
68                                 .buildSdncRequestHeader(svcAction, sdncReqId);
69                 GenericResourceApiRequestinformationRequestInformation requestInformation = generalTopologyObjectMapper
70                                 .buildGenericResourceApiRequestinformationRequestInformation(msoRequestId, requestAction);
71                 GenericResourceApiServiceinformationServiceInformation serviceInformation = generalTopologyObjectMapper
72                                 .buildServiceInformation(serviceInstance, requestContext, customer, true);
73                 GenericResourceApiVnfinformationVnfInformation vnfInformation = generalTopologyObjectMapper
74                                 .buildVnfInformation(vnf, serviceInstance, true);
75                 GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput = new GenericResourceApiVnfrequestinputVnfRequestInput();
76
77                 vnfRequestInput.setTenant(cloudRegion.getTenantId());
78                 vnfRequestInput.setAicCloudRegion(cloudRegion.getLcpCloudRegionId());
79                 vnfRequestInput.setCloudOwner(cloudRegion.getCloudOwner());
80                 
81                 if (StringUtils.isNotBlank(vnf.getVnfName())) {
82                         vnfRequestInput.setVnfName(vnf.getVnfName());
83                 }
84
85                 req.setRequestInformation(requestInformation);
86                 req.setSdncRequestHeader(sdncRequestHeader);
87                 req.setServiceInformation(serviceInformation);
88                 req.setVnfInformation(vnfInformation);
89
90                 GenericResourceApiParam vnfInputParameters = new GenericResourceApiParam();
91                 if (requestContext != null && requestContext.getUserParams() != null) {
92                         for (Map.Entry<String, Object> entry : requestContext.getUserParams().entrySet()) {
93                                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
94                                 paramItem.setName(entry.getKey());
95                                 paramItem.setValue(generalTopologyObjectMapper.mapUserParamValue(entry.getValue()));
96                                 vnfInputParameters.addParamItem(paramItem);
97                                 vnfRequestInput.setVnfInputParameters(vnfInputParameters);
98                         }
99                 }
100                 if (vnf.getCloudParams() != null) {
101                         for (Map.Entry<String, String> entry : vnf.getCloudParams().entrySet()) {
102                                 GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
103                                 paramItem.setName(entry.getKey());
104                                 paramItem.setValue(entry.getValue());
105                                 vnfInputParameters.addParamItem(paramItem);
106                         }
107                 }
108                 if (homing) {
109                         License license = vnf.getLicense();
110                         if (license != null) {
111                                 if(license.getEntitlementPoolUuids() != null && !license.getEntitlementPoolUuids().isEmpty()) {
112                                         String entitlementPoolUuid = license.getEntitlementPoolUuids().get(0);
113                                         GenericResourceApiParamParam paramItem = new GenericResourceApiParamParam();
114                                         paramItem.setName("entitlement_assignment_group_uuid");
115                                         paramItem.setValue(entitlementPoolUuid);
116                                         vnfInputParameters.addParamItem(paramItem);
117                                 }
118                                 if (license.getLicenseKeyGroupUuids() != null && !license.getLicenseKeyGroupUuids().isEmpty()) {
119                                         String licenseKeyGroupUuid = license.getLicenseKeyGroupUuids().get(0);
120                                         GenericResourceApiParamParam paramItem2 = new GenericResourceApiParamParam();
121                                         paramItem2.setName("license_assignment_group_uuid");
122                                         paramItem2.setValue(licenseKeyGroupUuid);
123                                         vnfInputParameters.addParamItem(paramItem2);
124                                 }
125                         }
126                 }
127                 List<InstanceGroup> instanceGroups = vnf.getInstanceGroups();
128                 List<GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds> networkInstanceGroupIdList = new ArrayList<GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds>();
129
130                 for (InstanceGroup instanceGroup : instanceGroups) {
131                         if (ModelInfoInstanceGroup.TYPE_L3_NETWORK
132                                         .equalsIgnoreCase(instanceGroup.getModelInfoInstanceGroup().getType())) {
133                                 GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds instanceGroupId = new GenericResourceApiVnfrequestinputVnfrequestinputVnfNetworkInstanceGroupIds();
134                                 instanceGroupId.setVnfNetworkInstanceGroupId(instanceGroup.getId());
135                                 networkInstanceGroupIdList.add(instanceGroupId);
136                         }
137                 }
138
139                 vnfRequestInput.setVnfNetworkInstanceGroupIds(networkInstanceGroupIdList);
140                 vnfRequestInput.setVnfInputParameters(vnfInputParameters);
141                 req.setVnfRequestInput(vnfRequestInput);
142                 return req;
143         }
144 }