blob: 9bf3b7fd7a2ab6a2243ec550e501740600de9a9b [file] [log] [blame]
Jian Lif2acb662017-04-06 02:06:16 +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.LispAppDataAddress;
22import org.onosproject.mapping.addresses.MappingAddress;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * LISP application data address codec.
28 */
29public final class LispAppDataAddressCodec extends JsonCodec<LispAppDataAddress> {
30
31 protected static final String PROTOCOL = "protocol";
32 protected static final String IP_TOS = "ipTos";
33 protected static final String LOCAL_PORT_LOW = "localPortLow";
34 protected static final String LOCAL_PORT_HIGH = "localPortHigh";
35 protected static final String REMOTE_PORT_LOW = "remotePortLow";
36 protected static final String REMOTE_PORT_HIGH = "remotePortHigh";
37 protected static final String ADDRESS = "address";
38
39 @Override
40 public ObjectNode encode(LispAppDataAddress address, CodecContext context) {
41 checkNotNull(address, "LispAppDataAddress cannot be null");
42
43 final ObjectNode result = context.mapper().createObjectNode()
44 .put(PROTOCOL, address.getProtocol())
45 .put(IP_TOS, address.getIpTos())
46 .put(LOCAL_PORT_LOW, address.getLocalPortLow())
47 .put(LOCAL_PORT_HIGH, address.getLocalPortHigh())
48 .put(REMOTE_PORT_LOW, address.getRemotePortLow())
49 .put(REMOTE_PORT_HIGH, address.getRemotePortHigh());
50
51 if (address.getAddress() != null) {
52 final JsonCodec<MappingAddress> addressCodec =
53 context.codec(MappingAddress.class);
54 ObjectNode addressNode = addressCodec.encode(address.getAddress(), context);
55 result.set(ADDRESS, addressNode);
56 }
57
58 return result;
59 }
60
61 @Override
62 public LispAppDataAddress decode(ObjectNode json, CodecContext context) {
63 if (json == null || !json.isObject()) {
64 return null;
65 }
66
67 byte protocol = (byte) json.get(PROTOCOL).asInt();
68 int ipTos = json.get(IP_TOS).asInt();
69 short localPortLow = (short) json.get(LOCAL_PORT_LOW).asInt();
70 short localPortHigh = (short) json.get(LOCAL_PORT_HIGH).asInt();
71 short remotePortLow = (short) json.get(REMOTE_PORT_LOW).asInt();
72 short remotePortHigh = (short) json.get(REMOTE_PORT_HIGH).asInt();
73
74 ObjectNode addressJson = get(json, ADDRESS);
75 MappingAddress mappingAddress = null;
76
77 if (addressJson != null) {
78 final JsonCodec<MappingAddress> addressCodec =
79 context.codec(MappingAddress.class);
80 mappingAddress = addressCodec.decode(addressJson, context);
81 }
82
83 return new LispAppDataAddress.Builder()
84 .withProtocol(protocol)
85 .withIpTos(ipTos)
86 .withLocalPortLow(localPortLow)
87 .withLocalPortHigh(localPortHigh)
88 .withRemotePortLow(remotePortLow)
89 .withRemotePortHigh(remotePortHigh)
90 .withAddress(mappingAddress)
91 .build();
92 }
93}