blob: f5a1efa8bc0161f1985e59599b975e7f3d7ab403 [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.LispMulticastAddress;
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 multicast address codec.
29 */
30public final class LispMulticastAddressCodec extends JsonCodec<LispMulticastAddress> {
31
32 protected static final String INSTANCE_ID = "instanceId";
33 protected static final String SRC_MASK_LENGTH = "srcMaskLength";
34 protected static final String GRP_MASK_LENGTH = "grpMaskLength";
35 protected static final String SRC_ADDRESS = "srcAddress";
36 protected static final String GRP_ADDRESS = "grpAddress";
37
38 private static final String MISSING_MEMBER_MESSAGE =
39 " member is required in LispMulticastAddress";
40
41 @Override
42 public ObjectNode encode(LispMulticastAddress address, CodecContext context) {
43 checkNotNull(address, "LispMulticastAddress cannot be null");
44
45 final ObjectNode result = context.mapper().createObjectNode()
46 .put(INSTANCE_ID, address.getInstanceId())
47 .put(SRC_MASK_LENGTH, address.getSrcMaskLength())
48 .put(GRP_MASK_LENGTH, address.getGrpMaskLength());
49
50 final JsonCodec<MappingAddress> addressCodec =
51 context.codec(MappingAddress.class);
52
53 if (address.getSrcAddress() != null) {
54 ObjectNode srcAddressNode = addressCodec.encode(address.getSrcAddress(), context);
55 result.set(SRC_ADDRESS, srcAddressNode);
56 }
57
58 if (address.getGrpAddress() != null) {
59 ObjectNode grpAddressNode = addressCodec.encode(address.getGrpAddress(), context);
60 result.set(GRP_ADDRESS, grpAddressNode);
61 }
62
63 return result;
64 }
65
66 @Override
67 public LispMulticastAddress decode(ObjectNode json, CodecContext context) {
68 if (json == null || !json.isObject()) {
69 return null;
70 }
71
72 int instanceId = nullIsIllegal(json.get(INSTANCE_ID),
73 INSTANCE_ID + MISSING_MEMBER_MESSAGE).asInt();
74 byte srcMaskLength = (byte) nullIsIllegal(json.get(SRC_MASK_LENGTH),
75 SRC_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
76 byte grpMaskLength = (byte) nullIsIllegal(json.get(GRP_MASK_LENGTH),
77 GRP_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
78
79 final JsonCodec<MappingAddress> addressCodec =
80 context.codec(MappingAddress.class);
81 ObjectNode srcAddressJson = get(json, SRC_ADDRESS);
82 MappingAddress srcAddress = null;
83
84 if (srcAddressJson != null) {
85 srcAddress = addressCodec.decode(srcAddressJson, context);
86 }
87
88 ObjectNode grpAddressJson = get(json, GRP_ADDRESS);
89 MappingAddress grpAddress = null;
90
91 if (grpAddressJson != null) {
92 grpAddress = addressCodec.decode(grpAddressJson, context);
93 }
94
95 return new LispMulticastAddress.Builder()
96 .withInstanceId(instanceId)
97 .withSrcMaskLength(srcMaskLength)
98 .withGrpMaskLength(grpMaskLength)
99 .withSrcAddress(srcAddress)
100 .withGrpAddress(grpAddress)
101 .build();
102 }
103}