Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Context for codecs to use while encoding/decoding. |
| 25 | */ |
| 26 | public interface CodecContext { |
| 27 | |
| 28 | /** |
| 29 | * Returns the JSON object mapper. |
| 30 | * |
| 31 | * @return object mapper |
| 32 | */ |
| 33 | ObjectMapper mapper(); |
| 34 | |
| 35 | /** |
| 36 | * Returns the JSON codec for the specified entity class. |
| 37 | * |
| 38 | * @param entityClass entity class |
| 39 | * @param <T> entity type |
| 40 | * @return JSON codec; null if no codec available for the class |
| 41 | */ |
| 42 | <T> JsonCodec<T> codec(Class<T> entityClass); |
| 43 | |
| 44 | /** |
| 45 | * Returns reference to the specified service implementation. |
| 46 | * |
| 47 | * @param serviceClass service class |
| 48 | * @param <T> service type |
Yuta HIGUCHI | 73ae7d7 | 2017-01-06 15:39:02 -0800 | [diff] [blame] | 49 | * @return service implementation; null if no implementation available for the class |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 50 | */ |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 51 | <T> T getService(Class<T> serviceClass); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 52 | |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Decodes the specified entity from JSON using codec |
| 55 | * registered to this context. |
| 56 | * |
| 57 | * @param json JSON to decode |
| 58 | * @param entityClass entity class |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 59 | * @param <T> entity type |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 60 | * @return decoded entity |
| 61 | */ |
| 62 | default <T> T decode(JsonNode json, Class<T> entityClass) { |
| 63 | checkArgument(json.isObject()); |
| 64 | return codec(entityClass).decode((ObjectNode) json, this); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Encodes the specified entity into JSON using codec |
| 69 | * registered to this context. |
| 70 | * |
| 71 | * @param entity entity to encode |
| 72 | * @param entityClass entity class |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 73 | * @param <T> entity type |
Yuta HIGUCHI | 776f074 | 2016-11-03 15:51:14 -0700 | [diff] [blame] | 74 | * @return JSON node |
| 75 | */ |
| 76 | default <T> ObjectNode encode(T entity, Class<T> entityClass) { |
| 77 | return codec(entityClass).encode(entity, this); |
| 78 | } |
| 79 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 80 | } |