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