blob: cf97f012650c7978237ca7147bbcac3415d0227d [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
2 * Copyright 2017-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 */
16package org.onosproject.cfm.web;
17
Sean Condon3a1efef2018-02-24 13:16:03 +000018import static org.onlab.packet.VlanId.MAX_VLAN;
Sean Condon0e89bda2017-03-21 14:23:19 +000019import static org.onlab.util.Tools.nullIsIllegal;
20
21import java.util.ArrayList;
22import java.util.List;
23
24import org.onlab.packet.VlanId;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27
28import com.fasterxml.jackson.databind.JsonNode;
29import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31
32/**
33 * Encode and decode to/from JSON to Vid object.
34 */
35public class VidCodec extends JsonCodec<VlanId> {
36
Sean Condon3a1efef2018-02-24 13:16:03 +000037 /**
38 * Encodes the VlanId entity into JSON.
39 *
40 * @param vid VlanId to encode
41 * @param context encoding context
42 * @return JSON node
43 * @throws java.lang.UnsupportedOperationException if the codec does not
44 * support encode operations
45 */
Sean Condon0e89bda2017-03-21 14:23:19 +000046 @Override
47 public ObjectNode encode(VlanId vid, CodecContext context) {
48 return context.mapper().createObjectNode().put("vid", vid.toString());
49 }
50
Sean Condon3a1efef2018-02-24 13:16:03 +000051 /**
52 * Encodes the collection of the VlanId entities.
53 *
54 * @param vids collection of VlanId to encode
55 * @param context encoding context
56 * @return JSON array
57 * @throws java.lang.UnsupportedOperationException if the codec does not
58 * support encode operations
59 */
Sean Condon0e89bda2017-03-21 14:23:19 +000060 @Override
61 public ArrayNode encode(Iterable<VlanId> vids, CodecContext context) {
62 ArrayNode an = context.mapper().createArrayNode();
Sean Condon3a1efef2018-02-24 13:16:03 +000063 vids.forEach(vid -> an.add(encode(vid, context)));
Sean Condon0e89bda2017-03-21 14:23:19 +000064 return an;
65 }
66
Sean Condon3a1efef2018-02-24 13:16:03 +000067 /**
68 * Decodes the VlanId entity from JSON.
69 *
70 * @param json JSON to decode
71 * @param context decoding context
72 * @return decoded VlanId
73 * @throws java.lang.UnsupportedOperationException if the codec does not
74 * support decode operations
75 */
Sean Condon0e89bda2017-03-21 14:23:19 +000076 @Override
77 public VlanId decode(ObjectNode json, CodecContext context) {
78 if (json == null || !json.isObject()) {
79 return null;
80 }
81
82 JsonNode vidNode = json.get("vid");
83
84 int vid = (nullIsIllegal(vidNode.asInt(), "vid is required"));
Sean Condon3a1efef2018-02-24 13:16:03 +000085 if (vid < 0 || vid > MAX_VLAN) {
Sean Condon0e89bda2017-03-21 14:23:19 +000086 throw new IllegalArgumentException("VID values must be between 0 and 4095");
87 }
88 return VlanId.vlanId((short) vid);
89 }
90
Sean Condon3a1efef2018-02-24 13:16:03 +000091 /**
92 * Decodes the VlanId JSON array into a collection of entities.
93 *
94 * @param json JSON array to decode
95 * @param context decoding context
96 * @return collection of decoded VlanId
97 * @throws java.lang.UnsupportedOperationException if the codec does not
98 * support decode operations
99 */
Sean Condon0e89bda2017-03-21 14:23:19 +0000100 @Override
101 public List<VlanId> decode(ArrayNode json, CodecContext context) {
102 List<VlanId> vidList = new ArrayList<>();
103 json.forEach(node -> vidList.add(decode((ObjectNode) node, context)));
104 return vidList;
105 }
106}