blob: 07a0fca3aafa843baaca66195cacf7974fcf89cb [file] [log] [blame]
Jian Li5baef512017-04-10 20:25:18 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li5baef512017-04-10 20:25:18 +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 */
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.LispSrcDstAddress;
22import org.onosproject.mapping.addresses.MappingAddress;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.nullIsIllegal;
26
27/**
28 * LISP source destination address codec.
29 */
30public final class LispSrcDstAddressCodec extends JsonCodec<LispSrcDstAddress> {
31
32 protected static final String SRC_MASK_LENGTH = "srcMaskLength";
33 protected static final String DST_MASK_LENGTH = "dstMaskLength";
34 protected static final String SRC_PREFIX = "srcPrefix";
35 protected static final String DST_PREFIX = "dstPrefix";
36
37 private static final String MISSING_MEMBER_MESSAGE =
38 " member is required in LispSrcDstAddress";
39
40 @Override
41 public ObjectNode encode(LispSrcDstAddress address, CodecContext context) {
42 checkNotNull(address, "LispSrcDstAddress cannot be null");
43
44 final JsonCodec<MappingAddress> addressCodec =
45 context.codec(MappingAddress.class);
46
47 final ObjectNode result = context.mapper().createObjectNode()
48 .put(SRC_MASK_LENGTH, address.getSrcMaskLength())
49 .put(DST_MASK_LENGTH, address.getDstMaskLength());
50
51 if (address.getSrcPrefix() != null) {
52 ObjectNode srcPrefix = addressCodec.encode(address.getSrcPrefix(), context);
53 result.set(SRC_PREFIX, srcPrefix);
54 }
55
56 if (address.getDstPrefix() != null) {
57 ObjectNode dstPrefix = addressCodec.encode(address.getDstPrefix(), context);
58 result.set(DST_PREFIX, dstPrefix);
59 }
60
61 return result;
62 }
63
64 @Override
65 public LispSrcDstAddress decode(ObjectNode json, CodecContext context) {
66 if (json == null || !json.isObject()) {
67 return null;
68 }
69
70 byte srcMaskLength = (byte) nullIsIllegal(json.get(SRC_MASK_LENGTH),
71 SRC_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
72 byte dstMaskLength = (byte) nullIsIllegal(json.get(DST_MASK_LENGTH),
73 DST_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
74
75 final JsonCodec<MappingAddress> addressCodec =
76 context.codec(MappingAddress.class);
77
78 ObjectNode srcPrefixJson = get(json, SRC_PREFIX);
79 MappingAddress srcPrefix = null;
80
81 ObjectNode dstPrefixJson = get(json, DST_PREFIX);
82 MappingAddress dstPrefix = null;
83
84 if (srcPrefixJson != null) {
85 srcPrefix = addressCodec.decode(srcPrefixJson, context);
86 }
87
88 if (dstPrefixJson != null) {
89 dstPrefix = addressCodec.decode(dstPrefixJson, context);
90 }
91
92 return new LispSrcDstAddress.Builder()
93 .withSrcMaskLength(srcMaskLength)
94 .withDstMaskLength(dstMaskLength)
95 .withSrcPrefix(srcPrefix)
96 .withDstPrefix(dstPrefix)
97 .build();
98 }
99}