2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.core.json;
23 import java.io.IOException;
24 import java.io.Serializable;
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;
34 import com.fasterxml.jackson.databind.DeserializationFeature;
35 import com.fasterxml.jackson.databind.ObjectMapper;
38 public class DecomposeJsonUtil implements Serializable {
39 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DecomposeJsonUtil.class);
43 private static final long serialVersionUID = 1L;
45 private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();
47 private DecomposeJsonUtil() {
50 private static ObjectMapper createObjectMapper() {
51 ObjectMapper om = new ObjectMapper();
52 om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
57 * Method to construct Service Decomposition object converting JSON structure
59 * @param jsonString input in JSON format confirming ServiceDecomposition
60 * @return decoded object
61 * @throws JsonDecomposingException thrown when decoding json fails
63 public static ServiceDecomposition jsonToServiceDecomposition(String jsonString) throws JsonDecomposingException {
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);
75 * Method to construct Service Decomposition object converting JSON structure
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
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;
92 * Method to construct Resource Decomposition object converting JSON structure
94 * @param jsonString input in JSON format confirming ResourceDecomposition
95 * @return decoded object
96 * @throws JsonDecomposingException thrown when decoding json fails
98 public static VnfResource jsonToVnfResource(String jsonString) throws JsonDecomposingException {
100 return OBJECT_MAPPER.readValue(jsonString, VnfResource.class);
101 } catch (IOException e) {
102 throw new JsonDecomposingException("Exception while converting json to vnf resource", e);
107 * Method to construct Resource Decomposition object converting JSON structure
109 * @param jsonString input in JSON format confirming ResourceDecomposition
110 * @return decoded object
111 * @throws JsonDecomposingException thrown when decoding json fails
113 public static NetworkResource jsonToNetworkResource(String jsonString) throws JsonDecomposingException {
115 return OBJECT_MAPPER.readValue(jsonString, NetworkResource.class);
116 } catch (IOException e) {
117 throw new JsonDecomposingException("Exception while converting json to network resource", e);
122 * Method to construct Resource Decomposition object converting JSON structure
124 * @param jsonString - input in JSON format confirming ResourceDecomposition
125 * @return decoded object
126 * @throws JsonDecomposingException thrown when decoding json fails
128 public static AllottedResource jsonToAllottedResource(String jsonString) throws JsonDecomposingException {
130 return OBJECT_MAPPER.readValue(jsonString, AllottedResource.class);
131 } catch (IOException e) {
132 throw new JsonDecomposingException("Exception while converting json to allotted resource", e);
136 public static ConfigResource jsonToConfigResource(String jsonString) throws JsonDecomposingException {
138 return OBJECT_MAPPER.readValue(jsonString, ConfigResource.class);
139 } catch (IOException e) {
140 throw new JsonDecomposingException("Exception while converting json to allotted resource", e);