blob: 237a4045821d466d726600331455aba5a22df3b2 [file] [log] [blame]
Jian Li73c74442017-04-10 23:30:12 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li73c74442017-04-10 23:30:12 +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.LispTeAddress;
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 traffic engineering record codec.
29 */
30public final class LispTeRecordCodec extends JsonCodec<LispTeAddress.TeRecord> {
31
Ray Milkey9c9cde42018-01-12 14:22:06 -080032 static final String LOOKUP = "lookup";
33 static final String RLOC_PROBE = "rlocProbe";
34 static final String STRICT = "strict";
35 static final String ADDRESS = "address";
Jian Li73c74442017-04-10 23:30:12 +090036
37 private static final String MISSING_MEMBER_MESSAGE =
38 " member is required in LispTeRecord";
39
40 @Override
41 public ObjectNode encode(LispTeAddress.TeRecord record, CodecContext context) {
42 checkNotNull(record, "LispTeRecord cannot be null");
43
44 final ObjectNode result = context.mapper().createObjectNode()
45 .put(LOOKUP, record.isLookup())
46 .put(RLOC_PROBE, record.isRlocProbe())
47 .put(STRICT, record.isStrict());
48
49 if (record.getAddress() != null) {
50 final JsonCodec<MappingAddress> addressCodec =
51 context.codec(MappingAddress.class);
52 ObjectNode address = addressCodec.encode(record.getAddress(), context);
53 result.set(ADDRESS, address);
54 }
55
56 return result;
57 }
58
59 @Override
60 public LispTeAddress.TeRecord decode(ObjectNode json, CodecContext context) {
61 if (json == null || !json.isObject()) {
62 return null;
63 }
64
65 boolean isLookup = nullIsIllegal(json.get(LOOKUP),
66 LOOKUP + MISSING_MEMBER_MESSAGE).asBoolean();
67 boolean isRlocProbe = nullIsIllegal(json.get(RLOC_PROBE),
68 RLOC_PROBE + MISSING_MEMBER_MESSAGE).asBoolean();
69 boolean isStrict = nullIsIllegal(json.get(STRICT),
70 STRICT + MISSING_MEMBER_MESSAGE).asBoolean();
71
72 ObjectNode addressJson = get(json, ADDRESS);
73 MappingAddress mappingAddress = null;
74
75 if (addressJson != null) {
76 final JsonCodec<MappingAddress> addressCodec =
77 context.codec(MappingAddress.class);
78 mappingAddress = addressCodec.decode(addressJson, context);
79 }
80
81 return new LispTeAddress.TeRecord.Builder()
82 .withIsLookup(isLookup)
83 .withIsRlocProbe(isRlocProbe)
84 .withIsStrict(isStrict)
85 .withRtrRlocAddress(mappingAddress)
86 .build();
87 }
88
89}