Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [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 | */ |
Jian Li | 2e818b0 | 2017-04-12 19:28:30 +0900 | [diff] [blame] | 16 | package org.onosproject.mapping.codec; |
Jian Li | 4508352 | 2017-03-20 18:46:07 +0900 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.mapping.addresses.ASMappingAddress; |
| 21 | import org.onosproject.mapping.addresses.DNMappingAddress; |
| 22 | import org.onosproject.mapping.addresses.EthMappingAddress; |
| 23 | import org.onosproject.mapping.addresses.IPMappingAddress; |
| 24 | import org.onosproject.mapping.addresses.MappingAddress; |
| 25 | |
| 26 | import java.util.EnumMap; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Encode portion of the mapping address codec. |
| 32 | */ |
| 33 | public final class EncodeMappingAddressCodecHelper { |
| 34 | |
| 35 | private final MappingAddress address; |
| 36 | private final CodecContext context; |
| 37 | |
| 38 | private final EnumMap<MappingAddress.Type, MappingAddressTypeFormatter> formatMap; |
| 39 | |
| 40 | /** |
| 41 | * Creates an encoder object for a mapping address. |
| 42 | * Initializes the formatter lookup map for the mapping address subclasses. |
| 43 | * |
| 44 | * @param address MappingAddress to encode |
| 45 | * @param context context of the JSON encoding |
| 46 | */ |
| 47 | public EncodeMappingAddressCodecHelper(MappingAddress address, CodecContext context) { |
| 48 | this.address = address; |
| 49 | this.context = context; |
| 50 | |
| 51 | formatMap = new EnumMap<>(MappingAddress.Type.class); |
| 52 | |
| 53 | formatMap.put(MappingAddress.Type.IPV4, new FormatIpv4()); |
| 54 | formatMap.put(MappingAddress.Type.IPV6, new FormatIpv6()); |
| 55 | formatMap.put(MappingAddress.Type.AS, new FormatAs()); |
| 56 | formatMap.put(MappingAddress.Type.DN, new FormatDn()); |
| 57 | formatMap.put(MappingAddress.Type.ETH, new FormatEth()); |
| 58 | |
| 59 | // TODO: not process extension mapping address for now |
| 60 | formatMap.put(MappingAddress.Type.EXTENSION, new FormatUnknown()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * An interface of mapping address type formatter. |
| 65 | */ |
| 66 | private interface MappingAddressTypeFormatter { |
| 67 | ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Implementation of IPv4 mapping address type formatter. |
| 72 | */ |
| 73 | private static class FormatIpv4 implements MappingAddressTypeFormatter { |
| 74 | |
| 75 | @Override |
| 76 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 77 | final IPMappingAddress ipv4 = (IPMappingAddress) address; |
| 78 | return root.put(MappingAddressCodec.IPV4, ipv4.ip().toString()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Implementation of IPv6 mapping address type formatter. |
| 84 | */ |
| 85 | private static class FormatIpv6 implements MappingAddressTypeFormatter { |
| 86 | |
| 87 | @Override |
| 88 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 89 | final IPMappingAddress ipv6 = (IPMappingAddress) address; |
| 90 | return root.put(MappingAddressCodec.IPV6, ipv6.ip().toString()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Implementation of AS mapping address type formatter. |
| 96 | */ |
| 97 | private static class FormatAs implements MappingAddressTypeFormatter { |
| 98 | |
| 99 | @Override |
| 100 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 101 | final ASMappingAddress as = (ASMappingAddress) address; |
| 102 | return root.put(MappingAddressCodec.AS, as.asNumber()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Implementation of Distinguished Name mapping address type formatter. |
| 108 | */ |
| 109 | private static class FormatDn implements MappingAddressTypeFormatter { |
| 110 | |
| 111 | @Override |
| 112 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 113 | final DNMappingAddress dn = (DNMappingAddress) address; |
| 114 | return root.put(MappingAddressCodec.DN, dn.name()); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Implementation of Ethernet mapping address type formatter. |
| 120 | */ |
| 121 | private static class FormatEth implements MappingAddressTypeFormatter { |
| 122 | |
| 123 | @Override |
| 124 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 125 | final EthMappingAddress eth = (EthMappingAddress) address; |
| 126 | return root.put(MappingAddressCodec.MAC, eth.mac().toString()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Implementation of Extension mapping address type formatter. |
| 132 | */ |
| 133 | private static class FormatExtension implements MappingAddressTypeFormatter { |
| 134 | |
| 135 | @Override |
| 136 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 137 | return null; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Implementation of Unknown mapping address type formatter. |
| 143 | */ |
| 144 | private static class FormatUnknown implements MappingAddressTypeFormatter { |
| 145 | |
| 146 | @Override |
| 147 | public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) { |
| 148 | return root; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Encodes a mapping address into a JSON node. |
| 154 | * |
| 155 | * @return encoded JSON object for the given mapping address |
| 156 | */ |
| 157 | public ObjectNode encode() { |
| 158 | final ObjectNode result = context.mapper().createObjectNode() |
| 159 | .put(MappingAddressCodec.TYPE, address.type().toString()); |
| 160 | |
| 161 | MappingAddressTypeFormatter formatter = |
| 162 | checkNotNull(formatMap.get(address.type()), |
| 163 | "No formatter found for mapping address type " |
| 164 | + address.type().toString()); |
| 165 | |
| 166 | return formatter.encodeMappingAddress(result, address); |
| 167 | } |
| 168 | } |