blob: 2ddb5895fe953a00a15d59387446da8a092f91e7 [file] [log] [blame]
Claudine Chiu25f07be2016-06-27 16:21:21 +00001/*
Thomas Vachuska52f2cd12018-11-08 21:20:04 -08002 * Copyright 2018-present Open Networking Foundation
Claudine Chiu25f07be2016-06-27 16:21:21 +00003 *
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 */
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080016package org.onosproject.incubator.net.virtual.codec;
Claudine Chiu25f07be2016-06-27 16:21:21 +000017
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
Ray Milkey5d08e1e2016-11-02 13:02:12 +010047 static final String NETWORK_ID = "networkId";
48 static final String HOST_ID = "id";
49 static final String MAC_ADDRESS = "mac";
50 static final String VLAN = "vlan";
51 static final String IP_ADDRESSES = "ipAddresses";
Charles Chancd06c692017-04-27 20:46:06 -070052 static final String HOST_LOCATION = "locations";
Claudine Chiu25f07be2016-06-27 16:21:21 +000053
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);
Charles Chancd06c692017-04-27 20:46:06 -070074
75 final ArrayNode jsonLocations = result.putArray("locations");
76 for (final HostLocation location : vHost.locations()) {
77 jsonLocations.add(locationCodec.encode(location, context));
78 }
79 result.set("locations", jsonLocations);
Claudine Chiu25f07be2016-06-27 16:21:21 +000080
81 return result;
82 }
83
84 @Override
85 public VirtualHost decode(ObjectNode json, CodecContext context) {
86 if (json == null || !json.isObject()) {
87 return null;
88 }
89
90 NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json)));
91 MacAddress mac = MacAddress.valueOf(json.get("mac").asText());
92 VlanId vlanId = VlanId.vlanId((short) json.get("vlan").asInt(VlanId.UNTAGGED));
Charles Chancd06c692017-04-27 20:46:06 -070093
94 Set<HostLocation> locations = new HashSet<>();
95 JsonNode locationNodes = json.get("locations");
96 locationNodes.forEach(locationNode -> {
97 PortNumber portNumber = PortNumber.portNumber(locationNode.get("port").asText());
98 DeviceId deviceId = DeviceId.deviceId(locationNode.get("elementId").asText());
99 locations.add(new HostLocation(deviceId, portNumber, 0));
100 });
101
Claudine Chiu25f07be2016-06-27 16:21:21 +0000102 HostId id = HostId.hostId(mac, vlanId);
103
104 Iterator<JsonNode> ipStrings = json.get("ipAddresses").elements();
105 Set<IpAddress> ips = new HashSet<>();
106 while (ipStrings.hasNext()) {
107 ips.add(IpAddress.valueOf(ipStrings.next().asText()));
108 }
109
Charles Chancd06c692017-04-27 20:46:06 -0700110 return new DefaultVirtualHost(nId, id, mac, vlanId, locations, ips);
Claudine Chiu25f07be2016-06-27 16:21:21 +0000111 }
112
113 /**
114 * Extract member from JSON ObjectNode.
115 *
116 * @param key key for which value is needed
117 * @param json JSON ObjectNode
118 * @return member value
119 */
120 private String extractMember(String key, ObjectNode json) {
121 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
122 }
123}