756e5b068e1c064988b8f04a95df85cd5a14c744
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sdnc / mapper / GeneralTopologyObjectMapper.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.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.onap.sdnc.northbound.client.model.GenericResourceApiConfigurationinformationConfigurationInformation;
28 import org.onap.sdnc.northbound.client.model.GenericResourceApiGcrequestinputGcRequestInput;
29 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkinformationNetworkInformation;
30 import org.onap.sdnc.northbound.client.model.GenericResourceApiOnapmodelinformationOnapModelInformation;
31 import org.onap.sdnc.northbound.client.model.GenericResourceApiParam;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
34 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceinformationServiceInformation;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfmoduleinformationVfModuleInformation;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfinformationVnfInformation;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
44 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
45 import org.onap.so.client.exception.MapperException;
46 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50
51 import com.fasterxml.jackson.core.JsonProcessingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 @Component
55 public class GeneralTopologyObjectMapper {
56         
57         private static final Logger logger = LoggerFactory.getLogger(GeneralTopologyObjectMapper.class);
58         private ObjectMapper mapper = new ObjectMapper();
59
60         /*
61          * Build GenericResourceApiRequestinformationRequestInformation
62          */
63         public GenericResourceApiRequestinformationRequestInformation buildGenericResourceApiRequestinformationRequestInformation(String msoReqId, GenericResourceApiRequestActionEnumeration requestAction){
64
65                 GenericResourceApiRequestinformationRequestInformation requestInformation = new GenericResourceApiRequestinformationRequestInformation();
66                 requestInformation.setRequestId(msoReqId);
67                 requestInformation.setRequestAction(requestAction);
68                 requestInformation.setSource("MSO");
69                 return requestInformation;
70         }
71         /*
72          * Build GenericResourceApiServiceinformationServiceInformation
73          */
74         public GenericResourceApiServiceinformationServiceInformation buildServiceInformation(ServiceInstance serviceInstance, RequestContext requestContext, Customer customer, boolean includeModelInformation){
75                 GenericResourceApiServiceinformationServiceInformation serviceInformation = new GenericResourceApiServiceinformationServiceInformation();
76                 serviceInformation.serviceId(serviceInstance.getServiceInstanceId());
77                 if (requestContext != null) {
78                         serviceInformation.setSubscriptionServiceType(requestContext.getProductFamilyId());
79                 }
80                 if (includeModelInformation) {
81                         GenericResourceApiOnapmodelinformationOnapModelInformation onapModelInformation = new GenericResourceApiOnapmodelinformationOnapModelInformation();
82                         onapModelInformation.setModelInvariantUuid(serviceInstance.getModelInfoServiceInstance().getModelInvariantUuid());
83                         onapModelInformation.setModelVersion(serviceInstance.getModelInfoServiceInstance().getModelVersion());
84                         onapModelInformation.setModelName(serviceInstance.getModelInfoServiceInstance().getModelName());
85                         onapModelInformation.setModelUuid(serviceInstance.getModelInfoServiceInstance().getModelUuid());
86                         serviceInformation.setOnapModelInformation(onapModelInformation );
87                 }
88                 serviceInformation.setServiceInstanceId(serviceInstance.getServiceInstanceId());
89
90                 if (customer != null) {
91                         serviceInformation.setGlobalCustomerId(customer.getGlobalCustomerId());
92                         if(customer.getServiceSubscription() != null){
93                                 serviceInformation.setSubscriptionServiceType(customer.getServiceSubscription().getServiceType());
94                         }
95
96                 }
97                 return serviceInformation;
98         }
99         /*
100          * Build GenericResourceApiNetworkinformationNetworkInformation
101          */
102         public GenericResourceApiNetworkinformationNetworkInformation buildNetworkInformation(L3Network network){
103                 GenericResourceApiNetworkinformationNetworkInformation networkInformation = new GenericResourceApiNetworkinformationNetworkInformation();
104                 GenericResourceApiOnapmodelinformationOnapModelInformation onapModelInformation = new GenericResourceApiOnapmodelinformationOnapModelInformation();
105                 if (network.getModelInfoNetwork() != null) {
106                         onapModelInformation.setModelInvariantUuid(network.getModelInfoNetwork().getModelInvariantUUID());
107                         onapModelInformation.setModelName(network.getModelInfoNetwork().getModelName());
108                         onapModelInformation.setModelVersion(network.getModelInfoNetwork().getModelVersion());
109                         onapModelInformation.setModelUuid(network.getModelInfoNetwork().getModelUUID());
110                         onapModelInformation.setModelCustomizationUuid(network.getModelInfoNetwork().getModelCustomizationUUID());
111                         networkInformation.setOnapModelInformation(onapModelInformation);
112                 }
113
114                 networkInformation.setFromPreload(null);
115                 networkInformation.setNetworkId(network.getNetworkId());
116                 networkInformation.setNetworkType(network.getNetworkType());
117                 networkInformation.setNetworkTechnology(network.getNetworkTechnology());
118                 return networkInformation;
119         }
120         /*
121          * Build GenericResourceApiVnfinformationVnfInformation
122          */
123         public GenericResourceApiVnfinformationVnfInformation buildVnfInformation(GenericVnf vnf, ServiceInstance serviceInstance, boolean includeModelInformation){
124                 GenericResourceApiVnfinformationVnfInformation vnfInformation = new GenericResourceApiVnfinformationVnfInformation();
125                 if (includeModelInformation && vnf.getModelInfoGenericVnf() != null) {
126                         GenericResourceApiOnapmodelinformationOnapModelInformation onapModelInformation = new GenericResourceApiOnapmodelinformationOnapModelInformation();
127                         onapModelInformation.setModelInvariantUuid(vnf.getModelInfoGenericVnf().getModelInvariantUuid());
128                         onapModelInformation.setModelName(vnf.getModelInfoGenericVnf().getModelName());
129                         onapModelInformation.setModelVersion(vnf.getModelInfoGenericVnf().getModelVersion());
130                         onapModelInformation.setModelUuid(vnf.getModelInfoGenericVnf().getModelUuid());
131                         onapModelInformation.setModelCustomizationUuid(vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
132                         vnfInformation.setOnapModelInformation(onapModelInformation);
133                 }
134                 vnfInformation.setVnfId(vnf.getVnfId());
135                 vnfInformation.setVnfType(vnf.getVnfType());
136                 vnfInformation.setVnfName(vnf.getVnfName());
137                 return vnfInformation;
138         }
139         /*
140          * Build GenericResourceApiVfModuleinformationVfModuleInformation
141          */
142         public GenericResourceApiVfmoduleinformationVfModuleInformation buildVfModuleInformation(VfModule vfModule, GenericVnf vnf, ServiceInstance serviceInstance, RequestContext requestContext, boolean includeModelInformation) throws MapperException {
143                 GenericResourceApiVfmoduleinformationVfModuleInformation vfModuleInformation = new GenericResourceApiVfmoduleinformationVfModuleInformation();
144                 if (includeModelInformation) {
145                         if (vfModule.getModelInfoVfModule() == null) {
146                                 throw new MapperException("VF Module model info is null for " + vfModule.getVfModuleId());
147                         }
148                         else {
149                                 GenericResourceApiOnapmodelinformationOnapModelInformation onapModelInformation = new GenericResourceApiOnapmodelinformationOnapModelInformation();
150                                 onapModelInformation.setModelInvariantUuid(vfModule.getModelInfoVfModule().getModelInvariantUUID());
151                                 onapModelInformation.setModelName(vfModule.getModelInfoVfModule().getModelName());
152                                 onapModelInformation.setModelVersion(vfModule.getModelInfoVfModule().getModelVersion());
153                                 onapModelInformation.setModelUuid(vfModule.getModelInfoVfModule().getModelUUID());
154                                 onapModelInformation.setModelCustomizationUuid(vfModule.getModelInfoVfModule().getModelCustomizationUUID());
155                                 vfModuleInformation.setOnapModelInformation(onapModelInformation);
156                         }
157                 }
158                 if (vfModule.getModelInfoVfModule() != null) {
159                         vfModuleInformation.setVfModuleType(vfModule.getModelInfoVfModule().getModelName());
160                 }
161                 vfModuleInformation.setVfModuleId(vfModule.getVfModuleId());
162                 if (requestContext != null && requestContext.getRequestParameters() != null) {
163                         vfModuleInformation.setFromPreload(requestContext.getRequestParameters().getUsePreload());                      
164                 }
165                 else {
166                         vfModuleInformation.setFromPreload(true);
167                 }
168                 
169                 return vfModuleInformation;
170         }
171         
172         
173         public GenericResourceApiSdncrequestheaderSdncRequestHeader buildSdncRequestHeader(SDNCSvcAction svcAction, String sdncReqId){
174                 return buildSdncRequestHeader(svcAction, sdncReqId, null);
175         }
176         
177         public GenericResourceApiSdncrequestheaderSdncRequestHeader buildSdncRequestHeader(SDNCSvcAction svcAction, String sdncReqId, String callbackUrl){
178                 GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader = new GenericResourceApiSdncrequestheaderSdncRequestHeader();
179                 sdncRequestHeader.setSvcAction(svcAction.getSdncApiAction());
180                 sdncRequestHeader.setSvcRequestId(sdncReqId);
181                 sdncRequestHeader.setSvcNotificationUrl(callbackUrl);
182                 return sdncRequestHeader;
183         }
184
185     /**
186          * Build ConfigurationInformation
187      * @param configuration
188      * @param includeModelInformation
189      * @return
190      */
191     public GenericResourceApiConfigurationinformationConfigurationInformation buildConfigurationInformation(Configuration configuration, boolean includeModelInformation) {
192         GenericResourceApiConfigurationinformationConfigurationInformation configurationInformation = new GenericResourceApiConfigurationinformationConfigurationInformation();
193         configurationInformation.setConfigurationId(configuration.getConfigurationId());
194         configurationInformation.setConfigurationName(configuration.getConfigurationName());
195         configurationInformation.setConfigurationType(configuration.getConfigurationType());
196         if(includeModelInformation) {
197             GenericResourceApiOnapmodelinformationOnapModelInformation onapModelInformation = new GenericResourceApiOnapmodelinformationOnapModelInformation();
198             onapModelInformation.setModelInvariantUuid(configuration.getModelInfoConfiguration().getModelInvariantId());
199             onapModelInformation.setModelUuid(configuration.getModelInfoConfiguration().getModelVersionId());
200             onapModelInformation.setModelCustomizationUuid(configuration.getModelInfoConfiguration().getModelCustomizationId());
201             configurationInformation.setOnapModelInformation(onapModelInformation);
202         }
203         return configurationInformation;
204     }
205
206
207     /**
208          * Build GcRequestInformation
209      * @param vnf
210      * @param genericResourceApiParam
211      * @return
212      */
213     public GenericResourceApiGcrequestinputGcRequestInput buildGcRequestInformation(GenericVnf vnf,GenericResourceApiParam genericResourceApiParam) {
214         GenericResourceApiGcrequestinputGcRequestInput gcRequestInput = new GenericResourceApiGcrequestinputGcRequestInput();
215         gcRequestInput.setVnfId(vnf.getVnfId());
216         if(genericResourceApiParam != null) {
217             gcRequestInput.setInputParameters(genericResourceApiParam);
218         }
219             return gcRequestInput;
220         }
221     
222     
223     public String mapUserParamValue(Object value) {
224         if (value == null) {
225                 return null;
226         } else {
227                 if (value instanceof Map || value instanceof Set || value instanceof List) {
228                         try {
229                                         return mapper.writeValueAsString(value);
230                                 } catch (JsonProcessingException e) {
231                                         logger.error("could not map value to string", e);
232                                         throw new IllegalArgumentException(e);
233                                 }
234                 } else {
235                         return value.toString();
236                 }
237         }
238     }
239 }