blob: 7c69f3b4f986235c6674d70008f0573a6bc07e05 [file] [log] [blame]
Jian Li45083522017-03-20 18:46:07 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li45083522017-03-20 18:46:07 +09003 *
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 Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Li45083522017-03-20 18:46:07 +090017
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.Maps;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onosproject.mapping.addresses.MappingAddress;
23import org.onosproject.mapping.addresses.MappingAddresses;
24
25import java.util.Map;
26
27import static org.onlab.util.Tools.nullIsIllegal;
28
29/**
30 * Decode portion of the mapping address codec.
31 */
32public final class DecodeMappingAddressCodecHelper {
33
34 private final ObjectNode json;
35
36 protected static final String MISSING_MEMBER_MESSAGE =
37 " member is required in Mapping Address";
38
39 private final Map<String, MappingAddressDecoder> decoderMap;
40
41 /**
42 * Creates a decode mapping address codec object.
43 * Initializes the lookup map for mapping address subclass decoders.
44 *
45 * @param json JSON object to decode
46 */
47 public DecodeMappingAddressCodecHelper(ObjectNode json) {
48 this.json = json;
49 decoderMap = Maps.newHashMap();
50
51 decoderMap.put(MappingAddress.Type.IPV4.name(), new Ipv4TypeDecoder());
52 decoderMap.put(MappingAddress.Type.IPV6.name(), new Ipv6TypeDecoder());
53 decoderMap.put(MappingAddress.Type.AS.name(), new AsTypeDecoder());
54 decoderMap.put(MappingAddress.Type.DN.name(), new DnTypeDecoder());
55 decoderMap.put(MappingAddress.Type.ETH.name(), new EthTypeDecoder());
56 }
57
58 /**
59 * An interface of mapping address type decoder.
60 */
61 private interface MappingAddressDecoder {
62 MappingAddress decodeMappingAddress(ObjectNode json);
63 }
64
65 /**
66 * Implementation of IPv4 mapping address decoder.
67 */
68 private class Ipv4TypeDecoder implements MappingAddressDecoder {
69
70 @Override
71 public MappingAddress decodeMappingAddress(ObjectNode json) {
72 String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV4),
73 MappingAddressCodec.IPV4 + MISSING_MEMBER_MESSAGE).asText();
74 return MappingAddresses.ipv4MappingAddress(IpPrefix.valueOf(ip));
75 }
76 }
77
78 /**
79 * Implementation of IPv6 mapping address decoder.
80 */
81 private class Ipv6TypeDecoder implements MappingAddressDecoder {
82
83 @Override
84 public MappingAddress decodeMappingAddress(ObjectNode json) {
85 String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV6),
86 MappingAddressCodec.IPV6 + MISSING_MEMBER_MESSAGE).asText();
87 return MappingAddresses.ipv6MappingAddress(IpPrefix.valueOf(ip));
88 }
89 }
90
91 /**
92 * Implementation of AS mapping address decoder.
93 */
94 private class AsTypeDecoder implements MappingAddressDecoder {
95
96 @Override
97 public MappingAddress decodeMappingAddress(ObjectNode json) {
98 String as = nullIsIllegal(json.get(MappingAddressCodec.AS),
99 MappingAddressCodec.AS + MISSING_MEMBER_MESSAGE).asText();
100 return MappingAddresses.asMappingAddress(as);
101 }
102 }
103
104 /**
105 * Implementation of DN mapping address decoder.
106 */
107 private class DnTypeDecoder implements MappingAddressDecoder {
108
109 @Override
110 public MappingAddress decodeMappingAddress(ObjectNode json) {
111 String dn = nullIsIllegal(json.get(MappingAddressCodec.DN),
112 MappingAddressCodec.DN + MISSING_MEMBER_MESSAGE).asText();
113 return MappingAddresses.dnMappingAddress(dn);
114 }
115 }
116
117 /**
118 * Implementation of Ethernet mapping address decoder.
119 */
120 private class EthTypeDecoder implements MappingAddressDecoder {
121
122 @Override
123 public MappingAddress decodeMappingAddress(ObjectNode json) {
124 MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(MappingAddressCodec.MAC),
125 MappingAddressCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
126 return MappingAddresses.ethMappingAddress(mac);
127 }
128 }
129
130 /**
131 * Decodes the JSON into a mapping address object.
132 *
133 * @return MappingAddress object
134 * @throws IllegalArgumentException if the JSON is invalid
135 */
136 public MappingAddress decode() {
137 String type =
138 nullIsIllegal(json.get(MappingAddressCodec.TYPE),
139 "Type not specified").asText();
140
141 MappingAddressDecoder decoder = decoderMap.get(type);
142 if (decoder != null) {
143 return decoder.decodeMappingAddress(json);
144 }
145
146 throw new IllegalArgumentException("Type " + type + " is unknown");
147 }
148}