blob: 4e5c1974cb745b79504605462cadbea616d80ef5 [file] [log] [blame]
Jian Lid1445012017-04-09 02:12:07 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.onosproject.drivers.lisp.extensions.codec;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.drivers.lisp.extensions.LispListAddress;
22import org.onosproject.mapping.addresses.MappingAddress;
23import org.slf4j.Logger;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.slf4j.LoggerFactory.getLogger;
27
28/**
29 * LISP list address codec.
30 */
31public final class LispListAddressCodec extends JsonCodec<LispListAddress> {
32
33 private final Logger log = getLogger(getClass());
34
35 protected static final String IPV4 = "ipv4";
36 protected static final String IPV6 = "ipv6";
37
38 private static final String MISSING_MEMBER_MESSAGE =
39 " member is required in LispListAddress";
40
41 @Override
42 public ObjectNode encode(LispListAddress address, CodecContext context) {
43 checkNotNull(address, "LispListAddress cannot be null");
44
45 final ObjectNode result = context.mapper().createObjectNode();
46
47 final JsonCodec<MappingAddress> addressCodec =
48 context.codec(MappingAddress.class);
49
50 if (address.getIpv4() != null) {
51 ObjectNode ipv4Node = addressCodec.encode(address.getIpv4(), context);
52 result.set(IPV4, ipv4Node);
53 }
54
55 if (address.getIpv6() != null) {
56 ObjectNode ipv6Node = addressCodec.encode(address.getIpv6(), context);
57 result.set(IPV6, ipv6Node);
58 }
59
60 if (address.getIpv4() == null && address.getIpv6() == null) {
61 log.error("Either IPv4 or IPv6 address should be specified.");
62 }
63
64 return result;
65 }
66
67 @Override
68 public LispListAddress decode(ObjectNode json, CodecContext context) {
69 if (json == null || !json.isObject()) {
70 return null;
71 }
72
73 final JsonCodec<MappingAddress> addressCodec =
74 context.codec(MappingAddress.class);
75 ObjectNode ipv4Json = get(json, IPV4);
76 ObjectNode ipv6Json = get(json, IPV6);
77 MappingAddress ipv4Address = null;
78 MappingAddress ipv6Address = null;
79
80 if (ipv4Json != null) {
81 ipv4Address = addressCodec.decode(ipv4Json, context);
82 }
83
84 if (ipv6Json != null) {
85 ipv6Address = addressCodec.decode(ipv6Json, context);
86 }
87
88 if (ipv4Json == null && ipv6Json == null) {
89 log.error("Either IPv4 or IPv6 address should be specified.");
90 }
91
92 return new LispListAddress.Builder()
93 .withIpv4(ipv4Address)
94 .withIpv6(ipv6Address)
95 .build();
96 }
97}