Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.incubator.net.virtual.DefaultVirtualHost; |
| 27 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.HostLocation; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | |
| 34 | import java.util.HashSet; |
| 35 | import java.util.Iterator; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static org.onlab.util.Tools.nullIsIllegal; |
| 40 | |
| 41 | /** |
| 42 | * Codec for the VirtualHost class. |
| 43 | */ |
| 44 | public class VirtualHostCodec extends JsonCodec<VirtualHost> { |
| 45 | |
| 46 | // JSON field names |
Ray Milkey | 5d08e1e | 2016-11-02 13:02:12 +0100 | [diff] [blame] | 47 | 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 Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 52 | static final String HOST_LOCATION = "locations"; |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 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); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 74 | |
| 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 Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 80 | |
| 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 Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 93 | |
| 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 Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 102 | 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 Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 110 | return new DefaultVirtualHost(nId, id, mac, vlanId, locations, ips); |
Claudine Chiu | 25f07be | 2016-06-27 16:21:21 +0000 | [diff] [blame] | 111 | } |
| 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 | } |