blob: 7c5ec805dbc5f1149de07b52331a1f3f6150f24c [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
Yuta HIGUCHI73ae7d72017-01-06 15:39:02 -080049 * @return service implementation; null if no implementation available for the class
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080050 */
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
Ray Milkeyef794342016-11-09 16:20:29 -080059 * @param <T> entity type
Yuta HIGUCHI776f0742016-11-03 15:51:14 -070060 * @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 Milkeyef794342016-11-09 16:20:29 -080073 * @param <T> entity type
Yuta HIGUCHI776f0742016-11-03 15:51:14 -070074 * @return JSON node
75 */
76 default <T> ObjectNode encode(T entity, Class<T> entityClass) {
77 return codec(entityClass).encode(entity, this);
78 }
79
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080080}