blob: 8fac8493a4b2c7697f5e69f80ffe5ddef05cd2bf [file] [log] [blame]
Jian Lif9ec0832017-04-09 20:49:48 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lif9ec0832017-04-09 20:49:48 +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.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Lists;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.drivers.lisp.extensions.LispNatAddress;
25import org.onosproject.mapping.addresses.MappingAddress;
26
27import java.util.List;
28import java.util.stream.IntStream;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * LISP NAT address codec.
34 */
35public final class LispNatAddressCodec extends JsonCodec<LispNatAddress> {
36
Ray Milkey9c9cde42018-01-12 14:22:06 -080037 static final String MS_UDP_PORT_NUMBER = "msUdpPortNumber";
38 static final String ETR_UDP_PORT_NUMBER = "etrUdpPortNumber";
39 static final String GLOBAL_ETR_RLOC_ADDRESS = "globalEtrRlocAddress";
40 static final String MS_RLOC_ADDRESS = "msRlocAddress";
41 static final String PRIVATE_ETR_RLOC_ADDRESS = "privateEtrRlocAddress";
42 static final String RTR_RLOC_ADDRESSES = "rtrRlocAddresses";
Jian Lif9ec0832017-04-09 20:49:48 +090043
44 private static final String MISSING_MEMBER_MESSAGE =
45 " member is required in LispListAddress";
46
47 @Override
48 public ObjectNode encode(LispNatAddress address, CodecContext context) {
49 checkNotNull(address, "LispListAddress cannot be null");
50
51 final ObjectNode result = context.mapper().createObjectNode()
52 .put(MS_UDP_PORT_NUMBER, address.getMsUdpPortNumber())
53 .put(ETR_UDP_PORT_NUMBER, address.getEtrUdpPortNumber());
54
55 final JsonCodec<MappingAddress> addressCodec =
56 context.codec(MappingAddress.class);
57
58 if (address.getGlobalEtrRlocAddress() != null) {
59 ObjectNode globalEtrRlocNode =
60 addressCodec.encode(address.getGlobalEtrRlocAddress(), context);
61 result.set(GLOBAL_ETR_RLOC_ADDRESS, globalEtrRlocNode);
62 }
63
64 if (address.getMsRlocAddress() != null) {
65 ObjectNode msRlocNode =
66 addressCodec.encode(address.getMsRlocAddress(), context);
67 result.set(MS_RLOC_ADDRESS, msRlocNode);
68 }
69
70 if (address.getPrivateEtrRlocAddress() != null) {
71 ObjectNode privateEtrRlocNode =
72 addressCodec.encode(address.getPrivateEtrRlocAddress(), context);
73 result.set(PRIVATE_ETR_RLOC_ADDRESS, privateEtrRlocNode);
74 }
75
76 final ArrayNode jsonRtrRlocNodes = result.putArray(RTR_RLOC_ADDRESSES);
77
78 if (address.getRtrRlocAddresses() != null) {
79 for (final MappingAddress mappingAddress : address.getRtrRlocAddresses()) {
80 jsonRtrRlocNodes.add(addressCodec.encode(mappingAddress, context));
81 }
82 }
83
84 return result;
85 }
86
87 @Override
88 public LispNatAddress decode(ObjectNode json, CodecContext context) {
89 if (json == null || !json.isObject()) {
90 return null;
91 }
92
93 final JsonCodec<MappingAddress> addressCodec =
94 context.codec(MappingAddress.class);
95
96 short msUdpPortNumber = (short) json.get(MS_UDP_PORT_NUMBER).asInt();
97 short etrUdpPortNumber = (short) json.get(ETR_UDP_PORT_NUMBER).asInt();
98
99 ObjectNode globalEtrRlocJson = get(json, GLOBAL_ETR_RLOC_ADDRESS);
100 ObjectNode msRlocJson = get(json, MS_RLOC_ADDRESS);
101 ObjectNode privateEtrRlocJson = get(json, PRIVATE_ETR_RLOC_ADDRESS);
102 JsonNode rtrRlocJson = json.get(RTR_RLOC_ADDRESSES);
103 MappingAddress globalEtrRlocAddress = null;
104 MappingAddress msRlocAddress = null;
105 MappingAddress privateEtrRlocAddress = null;
106 List<MappingAddress> rtrRlocAddresses = Lists.newArrayList();
107
108 if (globalEtrRlocJson != null) {
109 globalEtrRlocAddress = addressCodec.decode(globalEtrRlocJson, context);
110 }
111
112 if (msRlocJson != null) {
113 msRlocAddress = addressCodec.decode(msRlocJson, context);
114 }
115
116 if (privateEtrRlocJson != null) {
117 privateEtrRlocAddress = addressCodec.decode(privateEtrRlocJson, context);
118 }
119
120 if (rtrRlocJson != null) {
121 IntStream.range(0, rtrRlocJson.size())
122 .forEach(i -> rtrRlocAddresses.add(
123 addressCodec.decode(get(rtrRlocJson, i), context)));
124 }
125
126 return new LispNatAddress.Builder()
127 .withMsUdpPortNumber(msUdpPortNumber)
128 .withEtrUdpPortNumber(etrUdpPortNumber)
129 .withGlobalEtrRlocAddress(globalEtrRlocAddress)
130 .withMsRlocAddress(msRlocAddress)
131 .withPrivateEtrRlocAddress(privateEtrRlocAddress)
132 .withRtrRlocAddresses(rtrRlocAddresses)
133 .build();
134 }
135}