blob: 3189e10406aac3bf63b6e3c6d3c5004862be07ea [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
Yuta HIGUCHI776f0742016-11-03 15:51:14 -070018import static com.google.common.base.Preconditions.checkArgument;
19import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080020import com.fasterxml.jackson.databind.ObjectMapper;
Yuta HIGUCHI776f0742016-11-03 15:51:14 -070021import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080022
23/**
24 * Context for codecs to use while encoding/decoding.
25 */
26public 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
49 * @return JSON codec; null if no codec available for the class
50 */
Ray Milkey3078fc02015-05-06 16:14:14 -070051 <T> T getService(Class<T> serviceClass);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080052
Yuta HIGUCHI776f0742016-11-03 15:51:14 -070053 /**
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
59 * @return decoded entity
60 */
61 default <T> T decode(JsonNode json, Class<T> entityClass) {
62 checkArgument(json.isObject());
63 return codec(entityClass).decode((ObjectNode) json, this);
64 }
65
66 /**
67 * Encodes the specified entity into JSON using codec
68 * registered to this context.
69 *
70 * @param entity entity to encode
71 * @param entityClass entity class
72 * @return JSON node
73 */
74 default <T> ObjectNode encode(T entity, Class<T> entityClass) {
75 return codec(entityClass).encode(entity, this);
76 }
77
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080078}