2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.core.json;
25 import java.io.IOException;
26 import java.io.Serializable;
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;
35 import com.fasterxml.jackson.databind.DeserializationFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class DecomposeJsonUtil implements Serializable {
42 private static final Logger logger = LoggerFactory.getLogger(DecomposeJsonUtil.class);
46 private static final long serialVersionUID = 1L;
48 private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
50 private DecomposeJsonUtil() {
53 private static ObjectMapper createObjectMapper() {
54 ObjectMapper om = new ObjectMapper();
55 om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
60 * Method to construct Service Decomposition object converting JSON structure
62 * @param jsonString input in JSON format confirming ServiceDecomposition
63 * @return decoded object
64 * @throws JsonDecomposingException thrown when decoding json fails
66 public static ServiceDecomposition jsonToServiceDecomposition(String jsonString) throws JsonDecomposingException {
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);
78 * Method to construct Service Decomposition object converting JSON structure
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
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;
95 * Method to construct Resource Decomposition object converting JSON structure
97 * @param jsonString input in JSON format confirming ResourceDecomposition
98 * @return decoded object
99 * @throws JsonDecomposingException thrown when decoding json fails
101 public static VnfResource jsonToVnfResource(String jsonString) throws JsonDecomposingException {
103 return OBJECT_MAPPER.readValue(jsonString, VnfResource.class);
104 } catch (IOException e) {
105 throw new JsonDecomposingException("Exception while converting json to vnf resource", e);
110 * Method to construct Resource Decomposition object converting JSON structure
112 * @param jsonString input in JSON format confirming ResourceDecomposition
113 * @return decoded object
114 * @throws JsonDecomposingException thrown when decoding json fails
116 public static NetworkResource jsonToNetworkResource(String jsonString) throws JsonDecomposingException {
118 return OBJECT_MAPPER.readValue(jsonString, NetworkResource.class);
119 } catch (IOException e) {
120 throw new JsonDecomposingException("Exception while converting json to network resource", e);
125 * Method to construct Resource Decomposition object converting JSON structure
127 * @param jsonString - input in JSON format confirming ResourceDecomposition
128 * @return decoded object
129 * @throws JsonDecomposingException thrown when decoding json fails
131 public static AllottedResource jsonToAllottedResource(String jsonString) throws JsonDecomposingException {
133 return OBJECT_MAPPER.readValue(jsonString, AllottedResource.class);
134 } catch (IOException e) {
135 throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
139 public static ConfigResource jsonToConfigResource(String jsonString) throws JsonDecomposingException {
141 return OBJECT_MAPPER.readValue(jsonString, ConfigResource.class);
142 } catch (IOException e) {
143 throw new JsonDecomposingException("Exception while converting json to allotted resource", e);