[ONOS-6169] Implement codec for segment and SrcDst ext addresses

Change-Id: Id928a3bb2dd914f8411cc6353bb41c5f05fb54a4
This commit is contained in:
Jian Li 2017-04-10 20:25:18 +09:00 committed by Ray Milkey
parent b7d6b8e795
commit 5baef5144a
7 changed files with 562 additions and 0 deletions

View File

@ -28,6 +28,8 @@ import org.onosproject.drivers.lisp.extensions.codec.LispListAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispMulticastAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispNatAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispNonceAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispSegmentAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispSrcDstAddressCodec;
import org.onosproject.mapping.web.MappingCodecRegistrator;
import org.slf4j.Logger;
@ -60,6 +62,8 @@ public class LispMappingExtensionCodecRegistrator extends MappingCodecRegistrato
codecService.registerCodec(LispMulticastAddress.class, new LispMulticastAddressCodec());
codecService.registerCodec(LispNatAddress.class, new LispNatAddressCodec());
codecService.registerCodec(LispNonceAddress.class, new LispNonceAddressCodec());
codecService.registerCodec(LispSegmentAddress.class, new LispSegmentAddressCodec());
codecService.registerCodec(LispSrcDstAddress.class, new LispSrcDstAddressCodec());
log.info("Started");
}
@ -73,6 +77,8 @@ public class LispMappingExtensionCodecRegistrator extends MappingCodecRegistrato
codecService.unregisterCodec(LispMulticastAddress.class);
codecService.unregisterCodec(LispNatAddress.class);
codecService.unregisterCodec(LispNonceAddress.class);
codecService.unregisterCodec(LispSegmentAddress.class);
codecService.unregisterCodec(LispSrcDstAddress.class);
registrator.deactivate();
registrator = null;

View File

@ -0,0 +1,78 @@
/*
* 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.LispSegmentAddress;
import org.onosproject.mapping.addresses.MappingAddress;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.nullIsIllegal;
/**
* LISP segment address codec.
*/
public final class LispSegmentAddressCodec extends JsonCodec<LispSegmentAddress> {
protected static final String INSTANCE_ID = "instanceId";
protected static final String ADDRESS = "address";
private static final String MISSING_MEMBER_MESSAGE =
" member is required in LispSegmentAddress";
@Override
public ObjectNode encode(LispSegmentAddress address, CodecContext context) {
checkNotNull(address, "LispSegmentAddress cannot be null");
final ObjectNode result = context.mapper().createObjectNode()
.put(INSTANCE_ID, address.getInstanceId());
if (address.getAddress() != null) {
final JsonCodec<MappingAddress> addressCodec =
context.codec(MappingAddress.class);
ObjectNode addressNode = addressCodec.encode(address.getAddress(), context);
result.set(ADDRESS, addressNode);
}
return result;
}
@Override
public LispSegmentAddress decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
int instanceId = nullIsIllegal(json.get(INSTANCE_ID),
INSTANCE_ID + MISSING_MEMBER_MESSAGE).asInt();
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 LispSegmentAddress.Builder()
.withInstanceId(instanceId)
.withAddress(mappingAddress)
.build();
}
}

View File

@ -0,0 +1,99 @@
/*
* 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.LispSrcDstAddress;
import org.onosproject.mapping.addresses.MappingAddress;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.util.Tools.nullIsIllegal;
/**
* LISP source destination address codec.
*/
public final class LispSrcDstAddressCodec extends JsonCodec<LispSrcDstAddress> {
protected static final String SRC_MASK_LENGTH = "srcMaskLength";
protected static final String DST_MASK_LENGTH = "dstMaskLength";
protected static final String SRC_PREFIX = "srcPrefix";
protected static final String DST_PREFIX = "dstPrefix";
private static final String MISSING_MEMBER_MESSAGE =
" member is required in LispSrcDstAddress";
@Override
public ObjectNode encode(LispSrcDstAddress address, CodecContext context) {
checkNotNull(address, "LispSrcDstAddress cannot be null");
final JsonCodec<MappingAddress> addressCodec =
context.codec(MappingAddress.class);
final ObjectNode result = context.mapper().createObjectNode()
.put(SRC_MASK_LENGTH, address.getSrcMaskLength())
.put(DST_MASK_LENGTH, address.getDstMaskLength());
if (address.getSrcPrefix() != null) {
ObjectNode srcPrefix = addressCodec.encode(address.getSrcPrefix(), context);
result.set(SRC_PREFIX, srcPrefix);
}
if (address.getDstPrefix() != null) {
ObjectNode dstPrefix = addressCodec.encode(address.getDstPrefix(), context);
result.set(DST_PREFIX, dstPrefix);
}
return result;
}
@Override
public LispSrcDstAddress decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
byte srcMaskLength = (byte) nullIsIllegal(json.get(SRC_MASK_LENGTH),
SRC_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
byte dstMaskLength = (byte) nullIsIllegal(json.get(DST_MASK_LENGTH),
DST_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
final JsonCodec<MappingAddress> addressCodec =
context.codec(MappingAddress.class);
ObjectNode srcPrefixJson = get(json, SRC_PREFIX);
MappingAddress srcPrefix = null;
ObjectNode dstPrefixJson = get(json, DST_PREFIX);
MappingAddress dstPrefix = null;
if (srcPrefixJson != null) {
srcPrefix = addressCodec.decode(srcPrefixJson, context);
}
if (dstPrefixJson != null) {
dstPrefix = addressCodec.decode(dstPrefixJson, context);
}
return new LispSrcDstAddress.Builder()
.withSrcMaskLength(srcMaskLength)
.withDstMaskLength(dstMaskLength)
.withSrcPrefix(srcPrefix)
.withDstPrefix(dstPrefix)
.build();
}
}

View File

@ -0,0 +1,170 @@
/*
* 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.ObjectNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpPrefix;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.codec.impl.CodecManager;
import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
import org.onosproject.drivers.lisp.extensions.LispSegmentAddress;
import org.onosproject.mapping.addresses.MappingAddresses;
import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
import java.io.IOException;
import java.io.InputStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Unit tests for LispSegmentAddressCodec.
*/
public class LispSegmentAddressCodecTest {
private static final int INSTANCE_ID = 1;
private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
private CodecContext context;
private JsonCodec<LispSegmentAddress> segmentAddressCodec;
private LispMappingExtensionCodecRegistrator registrator;
/**
* Sets up for each test.
* Creates a context and fetches the LispSegmentAddress codec.
*/
@Before
public void setUp() {
CodecManager manager = new CodecManager();
registrator = new LispMappingExtensionCodecRegistrator();
registrator.codecService = manager;
registrator.activate();
context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
segmentAddressCodec = context.codec(LispSegmentAddress.class);
assertThat("segment address codec should not be null",
segmentAddressCodec, notNullValue());
}
/**
* Deactivates the codec registrator.
*/
@After
public void tearDown() {
registrator.deactivate();
}
/**
* Tests encoding of a LispSegmentAddress object.
*/
@Test
public void testLispSegmentAddressEncode() {
LispSegmentAddress address = new LispSegmentAddress.Builder()
.withInstanceId(INSTANCE_ID)
.withAddress(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX))
.build();
ObjectNode addressJson = segmentAddressCodec.encode(address, context);
assertThat("errors in encoding segment address JSON",
addressJson, LispSegmentAddressJsonMatcher.matchesSegmentAddress(address));
}
/**
* Tests decoding of a LispSegmentAddress JSON object.
*/
@Test
public void testLispSegmentAddressDecode() throws IOException {
LispSegmentAddress address = getLispSegmentAddress("LispSegmentAddress.json");
assertThat("incorrect instance ID", address.getInstanceId(), is(INSTANCE_ID));
assertThat("incorrect mapping address", address.getAddress(),
is(MappingAddresses.ipv4MappingAddress(IPV4_PREFIX)));
}
/**
* Hamcrest matcher for LispSegmentAddress.
*/
public static final class LispSegmentAddressJsonMatcher
extends TypeSafeDiagnosingMatcher<JsonNode> {
private final LispSegmentAddress address;
/**
* Default constructor.
*
* @param address LispSegmentAddress object
*/
private LispSegmentAddressJsonMatcher(LispSegmentAddress address) {
this.address = address;
}
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check instance ID
int jsonInstanceId = jsonNode.get(LispSegmentAddressCodec.INSTANCE_ID).asInt();
int instanceId = address.getInstanceId();
if (jsonInstanceId != instanceId) {
description.appendText("Instance ID was " + jsonInstanceId);
return false;
}
// check address
MappingAddressJsonMatcher addressMatcher =
MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress());
return addressMatcher.matches(jsonNode.get(LispSegmentAddressCodec.ADDRESS));
}
@Override
public void describeTo(Description description) {
description.appendText(address.toString());
}
/**
* Factory to allocate a LispSegmentAddress matcher.
*
* @param address LispSegmentAddress object we are looking for
* @return matcher
*/
public static LispSegmentAddressJsonMatcher matchesSegmentAddress(LispSegmentAddress address) {
return new LispSegmentAddressJsonMatcher(address);
}
}
/**
* Reads in a LispSegmentAddress from the given resource and decodes it.
*
* @param resourceName resource to use to read the JSON for the rule
* @return decoded LispSegmentAddress
* @throws IOException if processing the resource fails
*/
private LispSegmentAddress getLispSegmentAddress(String resourceName) throws IOException {
InputStream jsonStream = LispSegmentAddressCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat("JSON string should not be null", json, notNullValue());
LispSegmentAddress segmentAddress = segmentAddressCodec.decode((ObjectNode) json, context);
assertThat("decoded address should not be null", segmentAddress, notNullValue());
return segmentAddress;
}
}

View File

@ -0,0 +1,190 @@
/*
* 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.ObjectNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpPrefix;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.codec.impl.CodecManager;
import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
import org.onosproject.drivers.lisp.extensions.LispSrcDstAddress;
import org.onosproject.mapping.addresses.MappingAddresses;
import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
import java.io.IOException;
import java.io.InputStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Unit tests for LispSrcDstAddressCodec.
*/
public class LispSrcDstAddressCodecTest {
private static final byte SRC_MASK_LENGTH = (byte) 1;
private static final byte DST_MASK_LENGTH = (byte) 2;
private static final IpPrefix IPV4_SRC_PREFIX = IpPrefix.valueOf("10.1.1.1/24");
private static final IpPrefix IPV4_DST_PREFIX = IpPrefix.valueOf("10.1.1.2/24");
private CodecContext context;
private JsonCodec<LispSrcDstAddress> srcDstAddressCodec;
private LispMappingExtensionCodecRegistrator registrator;
/**
* Sets up for each test.
* Creates a context and fetches the LispSrcDstAddress codec.
*/
@Before
public void setUp() {
CodecManager manager = new CodecManager();
registrator = new LispMappingExtensionCodecRegistrator();
registrator.codecService = manager;
registrator.activate();
context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
srcDstAddressCodec = context.codec(LispSrcDstAddress.class);
assertThat("Source and Destination address codec should not be null",
srcDstAddressCodec, notNullValue());
}
/**
* Deactivates the codec registrator.
*/
@After
public void tearDown() {
registrator.deactivate();
}
/**
* Tests encoding of a LispSrcDstAddress object.
*/
@Test
public void testLispSrcDstAddressEncode() {
LispSrcDstAddress address = new LispSrcDstAddress.Builder()
.withSrcMaskLength(SRC_MASK_LENGTH)
.withDstMaskLength(DST_MASK_LENGTH)
.withSrcPrefix(MappingAddresses.ipv4MappingAddress(IPV4_SRC_PREFIX))
.withDstPrefix(MappingAddresses.ipv4MappingAddress(IPV4_DST_PREFIX))
.build();
ObjectNode addressJson = srcDstAddressCodec.encode(address, context);
assertThat("errors in encoding Source and Destination address JSON",
addressJson, LispSrcDstAddressJsonMatcher.matchesSrcDstAddress(address));
}
/**
* Tests decoding of a LispSrcDstAddress JSON object.
*/
@Test
public void testLispSrcDstAddressDecode() throws IOException {
LispSrcDstAddress address = getLispSrcDstAddress("LispSrcDstAddress.json");
assertThat("incorrect srcMaskLength", address.getSrcMaskLength(), is(SRC_MASK_LENGTH));
assertThat("incorrect dstMaskLength", address.getDstMaskLength(), is(DST_MASK_LENGTH));
assertThat("incorrect srcPrefix", address.getSrcPrefix(),
is(MappingAddresses.ipv4MappingAddress(IPV4_SRC_PREFIX)));
assertThat("incorrect dstPrefix", address.getDstPrefix(),
is(MappingAddresses.ipv4MappingAddress(IPV4_DST_PREFIX)));
}
/**
* Hamcrest matcher for LispSrcDstAddress.
*/
public static final class LispSrcDstAddressJsonMatcher
extends TypeSafeDiagnosingMatcher<JsonNode> {
private final LispSrcDstAddress address;
/**
* Default constructor.
*
* @param address LispSrcDstAddress object
*/
private LispSrcDstAddressJsonMatcher(LispSrcDstAddress address) {
this.address = address;
}
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check src mask length
byte jsonSrcMaskLength = (byte) jsonNode.get(LispSrcDstAddressCodec.SRC_MASK_LENGTH).asInt();
byte srcMaskLength = address.getSrcMaskLength();
if (jsonSrcMaskLength != srcMaskLength) {
description.appendText("Source mask length was " + jsonSrcMaskLength);
return false;
}
// check dst mask length
byte jsonDstMaskLength = (byte) jsonNode.get(LispSrcDstAddressCodec.DST_MASK_LENGTH).asInt();
byte dstMaskLength = address.getDstMaskLength();
if (jsonDstMaskLength != dstMaskLength) {
description.appendText("Destination mask length was " + jsonDstMaskLength);
return false;
}
// src prefix
MappingAddressJsonMatcher srcPrefixMatcher =
MappingAddressJsonMatcher.matchesMappingAddress(address.getSrcPrefix());
// dst prefix
MappingAddressJsonMatcher dstPrefixMatcher =
MappingAddressJsonMatcher.matchesMappingAddress(address.getDstPrefix());
return srcPrefixMatcher.matches(jsonNode.get(LispSrcDstAddressCodec.SRC_PREFIX)) ||
dstPrefixMatcher.matches(jsonNode.get(LispSrcDstAddressCodec.DST_PREFIX));
}
@Override
public void describeTo(Description description) {
description.appendText(address.toString());
}
/**
* Factory to allocate a LispSrcDstAddress matcher.
*
* @param address LispSrcDstAddress object we are looking for
* @return matcher
*/
public static LispSrcDstAddressJsonMatcher matchesSrcDstAddress(LispSrcDstAddress address) {
return new LispSrcDstAddressJsonMatcher(address);
}
}
/**
* Reads in a LispSrcDstAddress from the given resource and decodes it.
*
* @param resourceName resource to use to read the JSON for the rule
* @return decoded LispSrcDstAddress
* @throws IOException if processing the resource fails
*/
private LispSrcDstAddress getLispSrcDstAddress(String resourceName) throws IOException {
InputStream jsonStream = LispSrcDstAddressCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat("JSON string should not be null", json, notNullValue());
LispSrcDstAddress srcDstAddress = srcDstAddressCodec.decode((ObjectNode) json, context);
assertThat("decoded address should not be null", srcDstAddress, notNullValue());
return srcDstAddress;
}
}

View File

@ -0,0 +1,7 @@
{
"instanceId": 1,
"address": {
"type": "IPV4",
"ipv4": "10.1.1.0/24"
}
}

View File

@ -0,0 +1,12 @@
{
"srcMaskLength": 1,
"dstMaskLength": 2,
"srcPrefix": {
"type": "IPV4",
"ipv4": "10.1.1.1/24"
},
"dstPrefix": {
"type": "IPV4",
"ipv4": "10.1.1.2/24"
}
}