Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [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 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 21 | import org.onlab.packet.EthType; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.net.Annotations; |
| 28 | import org.onosproject.net.DefaultHost; |
| 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.HostLocation; |
| 32 | import org.onosproject.net.provider.ProviderId; |
| 33 | |
| 34 | import java.util.HashSet; |
| 35 | import java.util.Set; |
| 36 | import java.util.stream.Collectors; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 39 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 42 | * JSON codec for Host class. |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 43 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 44 | public final class HostCodec extends AnnotatedCodec<Host> { |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 45 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 46 | // JSON field names |
| 47 | public static final String HOST_ID = "id"; |
| 48 | public static final String MAC = "mac"; |
| 49 | public static final String VLAN = "vlan"; |
| 50 | public static final String INNER_VLAN = "innerVlan"; |
| 51 | public static final String OUTER_TPID = "outerTpid"; |
| 52 | public static final String IS_CONFIGURED = "configured"; |
| 53 | public static final String IS_SUSPENDED = "suspended"; |
| 54 | public static final String IP_ADDRESSES = "ipAddresses"; |
| 55 | public static final String HOST_LOCATIONS = "locations"; |
| 56 | public static final String AUX_LOCATIONS = "auxLocations"; |
| 57 | |
| 58 | private static final String NULL_OBJECT_MSG = "Host cannot be null"; |
| 59 | private static final String MISSING_MEMBER_MESSAGE = " member is required in Host"; |
| 60 | |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 61 | @Override |
| 62 | public ObjectNode encode(Host host, CodecContext context) { |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 63 | checkNotNull(host, NULL_OBJECT_MSG); |
| 64 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 65 | final JsonCodec<HostLocation> locationCodec = |
| 66 | context.codec(HostLocation.class); |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 67 | // keep fields in string for compatibility |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 68 | final ObjectNode result = context.mapper().createObjectNode() |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 69 | .put(HOST_ID, host.id().toString()) |
| 70 | .put(MAC, host.mac().toString()) |
| 71 | .put(VLAN, host.vlan().toString()) |
| 72 | .put(INNER_VLAN, host.innerVlan().toString()) |
| 73 | // use a 4-digit hex string in coding an ethernet type |
| 74 | .put(OUTER_TPID, String.format("0x%04x", host.tpid().toShort())) |
| 75 | .put(IS_CONFIGURED, host.configured()) |
| 76 | .put(IS_SUSPENDED, host.suspended()); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 77 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 78 | final ArrayNode jsonIpAddresses = result.putArray(IP_ADDRESSES); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 79 | for (final IpAddress ipAddress : host.ipAddresses()) { |
| 80 | jsonIpAddresses.add(ipAddress.toString()); |
| 81 | } |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 82 | result.set(IP_ADDRESSES, jsonIpAddresses); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 83 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 84 | final ArrayNode jsonLocations = result.putArray(HOST_LOCATIONS); |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 85 | for (final HostLocation location : host.locations()) { |
| 86 | jsonLocations.add(locationCodec.encode(location, context)); |
| 87 | } |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 88 | result.set(HOST_LOCATIONS, jsonLocations); |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 89 | |
Charles Chan | eb5bd49 | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 90 | if (host.auxLocations() != null) { |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 91 | final ArrayNode jsonAuxLocations = result.putArray(AUX_LOCATIONS); |
Charles Chan | eb5bd49 | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 92 | for (final HostLocation auxLocation : host.auxLocations()) { |
| 93 | jsonAuxLocations.add(locationCodec.encode(auxLocation, context)); |
| 94 | } |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 95 | result.set(AUX_LOCATIONS, jsonAuxLocations); |
Charles Chan | eb5bd49 | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 98 | return annotate(result, host, context); |
| 99 | } |
| 100 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 101 | @Override |
| 102 | public Host decode(ObjectNode json, CodecContext context) { |
| 103 | if (json == null || !json.isObject()) { |
| 104 | return null; |
| 105 | } |
Ray Milkey | 1f95bd3 | 2014-12-10 11:11:00 -0800 | [diff] [blame] | 106 | |
Seyeon Jeong | 9ceeacb | 2020-02-28 01:46:39 -0800 | [diff] [blame^] | 107 | MacAddress mac = MacAddress.valueOf(nullIsIllegal( |
| 108 | json.get(MAC), MAC + MISSING_MEMBER_MESSAGE).asText()); |
| 109 | VlanId vlanId = VlanId.vlanId(nullIsIllegal( |
| 110 | json.get(VLAN), VLAN + MISSING_MEMBER_MESSAGE).asText()); |
| 111 | HostId id = HostId.hostId(mac, vlanId); |
| 112 | |
| 113 | ArrayNode locationNodes = nullIsIllegal( |
| 114 | (ArrayNode) json.get(HOST_LOCATIONS), HOST_LOCATIONS + MISSING_MEMBER_MESSAGE); |
| 115 | Set<HostLocation> hostLocations = |
| 116 | context.codec(HostLocation.class).decode(locationNodes, context) |
| 117 | .stream().collect(Collectors.toSet()); |
| 118 | |
| 119 | ArrayNode ipNodes = nullIsIllegal( |
| 120 | (ArrayNode) json.get(IP_ADDRESSES), IP_ADDRESSES + MISSING_MEMBER_MESSAGE); |
| 121 | Set<IpAddress> ips = new HashSet<>(); |
| 122 | ipNodes.forEach(ipNode -> { |
| 123 | ips.add(IpAddress.valueOf(ipNode.asText())); |
| 124 | }); |
| 125 | |
| 126 | // check optional fields |
| 127 | JsonNode innerVlanIdNode = json.get(INNER_VLAN); |
| 128 | VlanId innerVlanId = (null == innerVlanIdNode) ? VlanId.NONE : |
| 129 | VlanId.vlanId(innerVlanIdNode.asText()); |
| 130 | JsonNode outerTpidNode = json.get(OUTER_TPID); |
| 131 | EthType outerTpid = (null == outerTpidNode) ? EthType.EtherType.UNKNOWN.ethType() : |
| 132 | EthType.EtherType.lookup((short) (Integer.decode(outerTpidNode.asText()) & 0xFFFF)).ethType(); |
| 133 | JsonNode configuredNode = json.get(IS_CONFIGURED); |
| 134 | boolean configured = (null == configuredNode) ? false : configuredNode.asBoolean(); |
| 135 | JsonNode suspendedNode = json.get(IS_SUSPENDED); |
| 136 | boolean suspended = (null == suspendedNode) ? false : suspendedNode.asBoolean(); |
| 137 | |
| 138 | ArrayNode auxLocationNodes = (ArrayNode) json.get(AUX_LOCATIONS); |
| 139 | Set<HostLocation> auxHostLocations = (null == auxLocationNodes) ? null : |
| 140 | context.codec(HostLocation.class).decode(auxLocationNodes, context) |
| 141 | .stream().collect(Collectors.toSet()); |
| 142 | |
| 143 | Annotations annotations = extractAnnotations(json, context); |
| 144 | |
| 145 | return new DefaultHost(ProviderId.NONE, id, mac, vlanId, |
| 146 | hostLocations, auxHostLocations, ips, innerVlanId, |
| 147 | outerTpid, configured, suspended, annotations); |
| 148 | } |
| 149 | |
| 150 | } |