Containerization feature of SO
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / onap / so / bpmn / core / json / DecomposeJsonUtil.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.bpmn.core.json;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25
26 import org.onap.so.bpmn.core.domain.AllottedResource;
27 import org.onap.so.bpmn.core.domain.ConfigResource;
28 import org.onap.so.bpmn.core.domain.NetworkResource;
29 import org.onap.so.bpmn.core.domain.ServiceDecomposition;
30 import org.onap.so.bpmn.core.domain.ServiceInstance;
31 import org.onap.so.bpmn.core.domain.VnfResource;
32 import org.onap.so.logger.MsoLogger;
33
34 import com.fasterxml.jackson.databind.DeserializationFeature;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37
38 public class DecomposeJsonUtil implements Serializable {
39         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DecomposeJsonUtil.class);
40         /**
41          * 
42          */
43         private static final long serialVersionUID = 1L;
44
45     private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
46
47     private DecomposeJsonUtil() {
48     }
49
50     private static ObjectMapper createObjectMapper() {
51         ObjectMapper om = new ObjectMapper();
52         om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
53         return om;
54     }
55
56         /**
57      * Method to construct Service Decomposition object converting JSON structure
58          * 
59      * @param jsonString input in JSON format confirming ServiceDecomposition
60      * @return decoded object
61      * @throws JsonDecomposingException thrown when decoding json fails
62          */
63     public static ServiceDecomposition jsonToServiceDecomposition(String jsonString) throws JsonDecomposingException {
64         try {
65         ObjectMapper om = new ObjectMapper();
66         om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
67         om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
68             return om.readValue(jsonString, ServiceDecomposition.class);
69                 } catch (IOException e) {
70             throw new JsonDecomposingException("Exception while converting json to service decomposition", e);
71         }
72                 }
73                 
74     /**
75      * Method to construct Service Decomposition object converting JSON structure
76      *
77      * @param jsonString input in JSON format confirming ServiceDecomposition
78      * @param serviceInstanceId service instance id to be put in decoded ServiceDecomposition
79      * @return decoded object
80      * @throws JsonDecomposingException thrown when decoding json fails
81      */
82     public static ServiceDecomposition jsonToServiceDecomposition(String jsonString, String serviceInstanceId)
83             throws JsonDecomposingException {
84         ServiceDecomposition serviceDecomposition = jsonToServiceDecomposition(jsonString);
85         ServiceInstance serviceInstance = new ServiceInstance();
86         serviceInstance.setInstanceId(serviceInstanceId);
87         serviceDecomposition.setServiceInstance(serviceInstance);
88                 return serviceDecomposition;
89         }
90         
91         /**
92      * Method to construct Resource Decomposition object converting JSON structure
93          * 
94      * @param jsonString input in JSON format confirming ResourceDecomposition
95      * @return decoded object
96      * @throws JsonDecomposingException thrown when decoding json fails
97          */
98     public static VnfResource jsonToVnfResource(String jsonString) throws JsonDecomposingException {
99                 try {
100             return OBJECT_MAPPER.readValue(jsonString, VnfResource.class);
101                 } catch (IOException e) {
102             throw new JsonDecomposingException("Exception while converting json to vnf resource", e);
103                 }
104         }
105         
106         /**
107      * Method to construct Resource Decomposition object converting JSON structure
108          * 
109      * @param jsonString input in JSON format confirming ResourceDecomposition
110      * @return decoded object
111      * @throws JsonDecomposingException thrown when decoding json fails
112          */
113     public static NetworkResource jsonToNetworkResource(String jsonString) throws JsonDecomposingException {
114                 try {
115             return OBJECT_MAPPER.readValue(jsonString, NetworkResource.class);
116                 } catch (IOException e) {
117             throw new JsonDecomposingException("Exception while converting json to network resource", e);
118                 }
119         }
120         
121         /**
122      * Method to construct Resource Decomposition object converting JSON structure
123          * 
124          * @param jsonString - input in JSON format confirming ResourceDecomposition
125      * @return decoded object
126      * @throws JsonDecomposingException thrown when decoding json fails
127          */
128     public static AllottedResource jsonToAllottedResource(String jsonString) throws JsonDecomposingException {
129                 try {
130             return OBJECT_MAPPER.readValue(jsonString, AllottedResource.class);
131                 } catch (IOException e) {
132             throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
133                 }
134         }
135         
136     public static ConfigResource jsonToConfigResource(String jsonString) throws JsonDecomposingException {
137                 try {
138             return OBJECT_MAPPER.readValue(jsonString, ConfigResource.class);
139                 } catch (IOException e) {
140             throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
141                 }
142         }
143 }