[ONOS-6169] Implement codec for LispTeAddress with unit test
Change-Id: I47edf0d20e2a8abdfd38bde19afa51b5abfb7240
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java
new file mode 100644
index 0000000..ff518f4
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP traffic engineering address codec.
+ */
+public final class LispTeAddressCodec extends JsonCodec<LispTeAddress> {
+
+ protected static final String TE_RECORDS = "records";
+
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in LispTeAddress";
+
+ @Override
+ public ObjectNode encode(LispTeAddress address, CodecContext context) {
+ checkNotNull(address, "LispTeAddress cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode();
+ final ArrayNode jsonRecords = result.putArray(TE_RECORDS);
+
+ final JsonCodec<LispTeAddress.TeRecord> recordCodec =
+ context.codec(LispTeAddress.TeRecord.class);
+
+ for (final LispTeAddress.TeRecord record : address.getTeRecords()) {
+ jsonRecords.add(recordCodec.encode(record, context));
+ }
+
+ return result;
+ }
+
+ @Override
+ public LispTeAddress decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ final JsonCodec<LispTeAddress.TeRecord> recordCodec =
+ context.codec(LispTeAddress.TeRecord.class);
+
+ JsonNode recordsJson = nullIsIllegal(json.get(TE_RECORDS),
+ TE_RECORDS + MISSING_MEMBER_MESSAGE);
+ List<LispTeAddress.TeRecord> records = Lists.newArrayList();
+
+ if (recordsJson != null) {
+ IntStream.range(0, recordsJson.size())
+ .forEach(i -> records.add(
+ recordCodec.decode(get(recordsJson, i), context)));
+ }
+
+ return new LispTeAddress.Builder()
+ .withTeRecords(records)
+ .build();
+ }
+}
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java
new file mode 100644
index 0000000..b5a3cc8
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP traffic engineering record codec.
+ */
+public final class LispTeRecordCodec extends JsonCodec<LispTeAddress.TeRecord> {
+
+ protected static final String LOOKUP = "lookup";
+ protected static final String RLOC_PROBE = "rlocProbe";
+ protected static final String STRICT = "strict";
+ protected static final String ADDRESS = "address";
+
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in LispTeRecord";
+
+ @Override
+ public ObjectNode encode(LispTeAddress.TeRecord record, CodecContext context) {
+ checkNotNull(record, "LispTeRecord cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(LOOKUP, record.isLookup())
+ .put(RLOC_PROBE, record.isRlocProbe())
+ .put(STRICT, record.isStrict());
+
+ if (record.getAddress() != null) {
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+ ObjectNode address = addressCodec.encode(record.getAddress(), context);
+ result.set(ADDRESS, address);
+ }
+
+ return result;
+ }
+
+ @Override
+ public LispTeAddress.TeRecord decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ boolean isLookup = nullIsIllegal(json.get(LOOKUP),
+ LOOKUP + MISSING_MEMBER_MESSAGE).asBoolean();
+ boolean isRlocProbe = nullIsIllegal(json.get(RLOC_PROBE),
+ RLOC_PROBE + MISSING_MEMBER_MESSAGE).asBoolean();
+ boolean isStrict = nullIsIllegal(json.get(STRICT),
+ STRICT + MISSING_MEMBER_MESSAGE).asBoolean();
+
+ ObjectNode addressJson = get(json, ADDRESS);
+ MappingAddress mappingAddress = null;
+
+ if (addressJson != null) {
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+ mappingAddress = addressCodec.decode(addressJson, context);
+ }
+
+ return new LispTeAddress.TeRecord.Builder()
+ .withIsLookup(isLookup)
+ .withIsRlocProbe(isRlocProbe)
+ .withIsStrict(isStrict)
+ .withRtrRlocAddress(mappingAddress)
+ .build();
+ }
+
+}