Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2015-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.impl; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 19 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.net.Annotated; |
| 23 | import org.onosproject.net.Annotations; |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DefaultAnnotations; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Base JSON codec for annotated entities. |
| 28 | */ |
| 29 | public abstract class AnnotatedCodec<T extends Annotated> extends JsonCodec<T> { |
| 30 | |
| 31 | /** |
| 32 | * Adds JSON encoding of the given item annotations to the specified node. |
| 33 | * |
| 34 | * @param node node to add annotations to |
| 35 | * @param entity annotated entity |
| 36 | * @param context encode context |
| 37 | * @return the given node |
| 38 | */ |
| 39 | protected ObjectNode annotate(ObjectNode node, T entity, CodecContext context) { |
| 40 | if (!entity.annotations().keys().isEmpty()) { |
| 41 | JsonCodec<Annotations> codec = context.codec(Annotations.class); |
| 42 | node.set("annotations", codec.encode(entity.annotations(), context)); |
| 43 | } |
| 44 | return node; |
| 45 | } |
| 46 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Extracts annotations of given Object. |
| 49 | * |
| 50 | * @param objNode annotated JSON object node |
| 51 | * @param context decode context |
| 52 | * @return extracted Annotations |
| 53 | */ |
| 54 | protected Annotations extractAnnotations(ObjectNode objNode, CodecContext context) { |
| 55 | |
| 56 | JsonCodec<Annotations> codec = context.codec(Annotations.class); |
| 57 | if (objNode.has("annotations") && objNode.isObject()) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 58 | return codec.decode(get(objNode, "annotations"), context); |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 59 | } else { |
| 60 | return DefaultAnnotations.EMPTY; |
| 61 | } |
| 62 | } |
| 63 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 64 | } |