Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.cordfabric; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Codec for encoding/decoding a FabricVlan object to/from JSON. |
| 34 | */ |
| 35 | public final class FabricVlanCodec extends JsonCodec<FabricVlan> { |
| 36 | |
| 37 | // JSON field names |
| 38 | private static final String VLAN = "vlan"; |
| 39 | private static final String PORTS = "ports"; |
alshabib | c4b5d46 | 2015-06-08 23:06:24 -0700 | [diff] [blame] | 40 | private static final String IPTV = "iptv"; |
Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
| 43 | public ObjectNode encode(FabricVlan vlan, CodecContext context) { |
| 44 | checkNotNull(vlan, "Vlan cannot be null"); |
| 45 | final ObjectNode result = context.mapper().createObjectNode() |
| 46 | .put(VLAN, vlan.vlan().toShort()); |
| 47 | |
| 48 | final ArrayNode jsonPorts = result.putArray(PORTS); |
| 49 | |
| 50 | vlan.ports().forEach(cp -> jsonPorts.add(context.codec(ConnectPoint.class).encode(cp, context))); |
| 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public FabricVlan decode(ObjectNode json, CodecContext context) { |
| 57 | short vlan = json.path(VLAN).shortValue(); |
alshabib | c4b5d46 | 2015-06-08 23:06:24 -0700 | [diff] [blame] | 58 | boolean iptv = json.path(IPTV).booleanValue(); |
Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 59 | List<ConnectPoint> ports = new ArrayList<>(); |
| 60 | |
| 61 | ArrayNode portArray = (ArrayNode) json.path(PORTS); |
| 62 | for (JsonNode o : portArray) { |
| 63 | ports.add(context.codec(ConnectPoint.class).decode((ObjectNode) o, context)); |
| 64 | } |
| 65 | |
alshabib | c4b5d46 | 2015-06-08 23:06:24 -0700 | [diff] [blame] | 66 | return new FabricVlan(VlanId.vlanId(vlan), ports, iptv); |
Jonathan Hart | 443ebed | 2015-05-05 14:02:12 -0700 | [diff] [blame] | 67 | } |
| 68 | } |