blob: ffd5ab7ba2c298297475bb31820b0aa7711a7960 [file] [log] [blame]
Jian Li091d8d22018-02-20 10:42:06 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.openstacknetworking.util;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Jian Lieb9f77d2018-02-20 11:25:45 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li091d8d22018-02-20 10:42:06 +090021import org.openstack4j.core.transport.ObjectMapperSingleton;
22import org.openstack4j.model.ModelEntity;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import java.io.InputStream;
27
28import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
29
30/**
31 * An utility that used in openstack networking app.
32 */
Jian Lidea0fdb2018-04-02 19:02:48 +090033public final class OpenstackNetworkingUtil {
Jian Li091d8d22018-02-20 10:42:06 +090034
Jian Lidea0fdb2018-04-02 19:02:48 +090035 protected static final Logger log = LoggerFactory.getLogger(OpenstackNetworkingUtil.class);
Jian Li091d8d22018-02-20 10:42:06 +090036
37 /**
38 * Prevents object instantiation from external.
39 */
Jian Lidea0fdb2018-04-02 19:02:48 +090040 private OpenstackNetworkingUtil() {
Jian Li091d8d22018-02-20 10:42:06 +090041 }
42
43 /**
44 * Interprets JSON string to corresponding openstack model entity object.
45 *
46 * @param input JSON string
47 * @param entityClazz openstack model entity class
48 * @return openstack model entity object
49 */
50 public static ModelEntity jsonToModelEntity(InputStream input, Class entityClazz) {
51 ObjectMapper mapper = new ObjectMapper();
52 try {
53 JsonNode jsonTree = mapper.enable(INDENT_OUTPUT).readTree(input);
54 log.trace(new ObjectMapper().writeValueAsString(jsonTree));
55 return ObjectMapperSingleton.getContext(entityClazz)
56 .readerFor(entityClazz)
57 .readValue(jsonTree);
58 } catch (Exception e) {
59 throw new IllegalArgumentException();
60 }
61 }
Jian Lieb9f77d2018-02-20 11:25:45 +090062
63 /**
64 * Converts openstack model entity object into JSON object.
65 *
66 * @param entity openstack model entity object
67 * @param entityClazz openstack model entity class
68 * @return JSON object
69 */
70 public static ObjectNode modelEntityToJson(ModelEntity entity, Class entityClazz) {
71 ObjectMapper mapper = new ObjectMapper();
72 try {
73 String strModelEntity = ObjectMapperSingleton.getContext(entityClazz)
74 .writerFor(entityClazz)
75 .writeValueAsString(entity);
76 log.trace(strModelEntity);
77 return (ObjectNode) mapper.readTree(strModelEntity.getBytes());
78 } catch (Exception e) {
79 throw new IllegalStateException();
80 }
81 }
Jian Li091d8d22018-02-20 10:42:06 +090082}