Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.util; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jian Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 21 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 22 | import org.openstack4j.model.ModelEntity; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import java.io.InputStream; |
| 27 | |
| 28 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 29 | |
| 30 | /** |
| 31 | * An utility that used in openstack networking app. |
| 32 | */ |
| 33 | public final class OpenstackUtil { |
| 34 | |
| 35 | protected static final Logger log = LoggerFactory.getLogger(OpenstackUtil.class); |
| 36 | |
| 37 | /** |
| 38 | * Prevents object instantiation from external. |
| 39 | */ |
| 40 | private OpenstackUtil() { |
| 41 | } |
| 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 Li | eb9f77d | 2018-02-20 11:25:45 +0900 | [diff] [blame] | 62 | |
| 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 Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 82 | } |