blob: 7515f1c47432659ecb7fd6289499618e04543d1b [file] [log] [blame]
Claudine Chiu25f07be2016-06-27 16:21:21 +00001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.incubator.net.virtual.DefaultVirtualHost;
27import org.onosproject.incubator.net.virtual.NetworkId;
28import org.onosproject.incubator.net.virtual.VirtualHost;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.HostId;
31import org.onosproject.net.HostLocation;
32import org.onosproject.net.PortNumber;
33
34import java.util.HashSet;
35import java.util.Iterator;
36import java.util.Set;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.onlab.util.Tools.nullIsIllegal;
40
41/**
42 * Codec for the VirtualHost class.
43 */
44public class VirtualHostCodec extends JsonCodec<VirtualHost> {
45
46 // JSON field names
47 private static final String NETWORK_ID = "networkId";
48 private static final String HOST_ID = "id";
49 private static final String MAC_ADDRESS = "mac";
50 private static final String VLAN = "vlan";
51 private static final String IP_ADDRESSES = "ipAddresses";
52 private static final String HOST_LOCATION = "location";
53
54 private static final String NULL_OBJECT_MSG = "VirtualHost cannot be null";
55 private static final String MISSING_MEMBER_MSG = " member is required in VirtualHost";
56
57 @Override
58 public ObjectNode encode(VirtualHost vHost, CodecContext context) {
59 checkNotNull(vHost, NULL_OBJECT_MSG);
60
61 final JsonCodec<HostLocation> locationCodec =
62 context.codec(HostLocation.class);
63 final ObjectNode result = context.mapper().createObjectNode()
64 .put(NETWORK_ID, vHost.networkId().toString())
65 .put(HOST_ID, vHost.id().toString())
66 .put(MAC_ADDRESS, vHost.mac().toString())
67 .put(VLAN, vHost.vlan().toString());
68
69 final ArrayNode jsonIpAddresses = result.putArray(IP_ADDRESSES);
70 for (final IpAddress ipAddress : vHost.ipAddresses()) {
71 jsonIpAddresses.add(ipAddress.toString());
72 }
73 result.set(IP_ADDRESSES, jsonIpAddresses);
74 result.set(HOST_LOCATION, locationCodec.encode(vHost.location(), context));
75
76 return result;
77 }
78
79 @Override
80 public VirtualHost decode(ObjectNode json, CodecContext context) {
81 if (json == null || !json.isObject()) {
82 return null;
83 }
84
85 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
86 MacAddress mac = MacAddress.valueOf(json.get("mac").asText());
87 VlanId vlanId = VlanId.vlanId((short) json.get("vlan").asInt(VlanId.UNTAGGED));
88 JsonNode locationNode = json.get("location");
89 PortNumber portNumber = PortNumber.portNumber(locationNode.get("port").asText());
90 DeviceId deviceId = DeviceId.deviceId(locationNode.get("elementId").asText());
91 HostLocation hostLocation = new HostLocation(deviceId, portNumber, 0);
92 HostId id = HostId.hostId(mac, vlanId);
93
94 Iterator<JsonNode> ipStrings = json.get("ipAddresses").elements();
95 Set<IpAddress> ips = new HashSet<>();
96 while (ipStrings.hasNext()) {
97 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
98 }
99
100 return new DefaultVirtualHost(nId, id, mac, vlanId, hostLocation, ips);
101 }
102
103 /**
104 * Extract member from JSON ObjectNode.
105 *
106 * @param key key for which value is needed
107 * @param json JSON ObjectNode
108 * @return member value
109 */
110 private String extractMember(String key, ObjectNode json) {
111 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
112 }
113}