89f61fec755f2479db880c92ea77508c39d6340f
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.core.json;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27
28 import org.onap.so.bpmn.core.domain.AllottedResource;
29 import org.onap.so.bpmn.core.domain.ConfigResource;
30 import org.onap.so.bpmn.core.domain.NetworkResource;
31 import org.onap.so.bpmn.core.domain.ServiceDecomposition;
32 import org.onap.so.bpmn.core.domain.ServiceInstance;
33 import org.onap.so.bpmn.core.domain.VnfResource;
34
35 import com.fasterxml.jackson.databind.DeserializationFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 public class DecomposeJsonUtil implements Serializable {
42         private static final Logger logger = LoggerFactory.getLogger(DecomposeJsonUtil.class);
43         /**
44          * 
45          */
46         private static final long serialVersionUID = 1L;
47
48     private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
49
50     private DecomposeJsonUtil() {
51     }
52
53     private static ObjectMapper createObjectMapper() {
54         ObjectMapper om = new ObjectMapper();
55         om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
56         return om;
57     }
58
59         /**
60      * Method to construct Service Decomposition object converting JSON structure
61          * 
62      * @param jsonString input in JSON format confirming ServiceDecomposition
63      * @return decoded object
64      * @throws JsonDecomposingException thrown when decoding json fails
65          */
66     public static ServiceDecomposition jsonToServiceDecomposition(String jsonString) throws JsonDecomposingException {
67         try {
68         ObjectMapper om = new ObjectMapper();
69         om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
70         om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
71             return om.readValue(jsonString, ServiceDecomposition.class);
72                 } catch (IOException e) {
73             throw new JsonDecomposingException("Exception while converting json to service decomposition", e);
74         }
75                 }
76                 
77     /**
78      * Method to construct Service Decomposition object converting JSON structure
79      *
80      * @param jsonString input in JSON format confirming ServiceDecomposition
81      * @param serviceInstanceId service instance id to be put in decoded ServiceDecomposition
82      * @return decoded object
83      * @throws JsonDecomposingException thrown when decoding json fails
84      */
85     public static ServiceDecomposition jsonToServiceDecomposition(String jsonString, String serviceInstanceId)
86             throws JsonDecomposingException {
87         ServiceDecomposition serviceDecomposition = jsonToServiceDecomposition(jsonString);
88         ServiceInstance serviceInstance = new ServiceInstance();
89         serviceInstance.setInstanceId(serviceInstanceId);
90         serviceDecomposition.setServiceInstance(serviceInstance);
91                 return serviceDecomposition;
92         }
93         
94         /**
95      * Method to construct Resource Decomposition object converting JSON structure
96          * 
97      * @param jsonString input in JSON format confirming ResourceDecomposition
98      * @return decoded object
99      * @throws JsonDecomposingException thrown when decoding json fails
100          */
101     public static VnfResource jsonToVnfResource(String jsonString) throws JsonDecomposingException {
102                 try {
103             return OBJECT_MAPPER.readValue(jsonString, VnfResource.class);
104                 } catch (IOException e) {
105             throw new JsonDecomposingException("Exception while converting json to vnf resource", e);
106                 }
107         }
108         
109         /**
110      * Method to construct Resource Decomposition object converting JSON structure
111          * 
112      * @param jsonString input in JSON format confirming ResourceDecomposition
113      * @return decoded object
114      * @throws JsonDecomposingException thrown when decoding json fails
115          */
116     public static NetworkResource jsonToNetworkResource(String jsonString) throws JsonDecomposingException {
117                 try {
118             return OBJECT_MAPPER.readValue(jsonString, NetworkResource.class);
119                 } catch (IOException e) {
120             throw new JsonDecomposingException("Exception while converting json to network resource", e);
121                 }
122         }
123         
124         /**
125      * Method to construct Resource Decomposition object converting JSON structure
126          * 
127          * @param jsonString - input in JSON format confirming ResourceDecomposition
128      * @return decoded object
129      * @throws JsonDecomposingException thrown when decoding json fails
130          */
131     public static AllottedResource jsonToAllottedResource(String jsonString) throws JsonDecomposingException {
132                 try {
133             return OBJECT_MAPPER.readValue(jsonString, AllottedResource.class);
134                 } catch (IOException e) {
135             throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
136                 }
137         }
138         
139     public static ConfigResource jsonToConfigResource(String jsonString) throws JsonDecomposingException {
140                 try {
141             return OBJECT_MAPPER.readValue(jsonString, ConfigResource.class);
142                 } catch (IOException e) {
143             throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
144                 }
145         }
146 }