Add LinkInfo and LinkStatsInfo JSON codec with unit tests

Change-Id: I9c9f50496c55f617438dab3641ce43354352dff6
This commit is contained in:
Jian Li 2019-01-30 21:58:39 +09:00
parent 866416f5ad
commit b4ce5b66d1
9 changed files with 569 additions and 2 deletions

View File

@ -52,7 +52,7 @@ public final class DefaultLinkStatsInfoTest {
private LinkStatsInfo sameAsInfo1;
private LinkStatsInfo info2;
static LinkStatsInfo createLinkStatsInfo1() {
public static LinkStatsInfo createLinkStatsInfo1() {
return DefaultLinkStatsInfo.builder()
.withTxPacket(TX_PACKET_1)
.withRxPacket(RX_PACKET_1)
@ -64,7 +64,7 @@ public final class DefaultLinkStatsInfoTest {
.build();
}
static LinkStatsInfo createLinkStatsInfo2() {
public static LinkStatsInfo createLinkStatsInfo2() {
return DefaultLinkStatsInfo.builder()
.withTxPacket(TX_PACKET_2)
.withRxPacket(RX_PACKET_2)

View File

@ -23,6 +23,7 @@ COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + CLI + REST + [
TEST_DEPS = TEST_ADAPTERS + [
"//core/api:onos-api-tests",
"//core/common:onos-core-common-tests",
"//apps/openstacktelemetry/api:onos-apps-openstacktelemetry-api-tests",
]
osgi_jar_with_tests(

View File

@ -0,0 +1,56 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import org.onosproject.codec.CodecService;
import org.onosproject.openstacktelemetry.api.LinkInfo;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Implementation of the JSON codec brokering service for OpenstackTelemetry.
*/
@Component(immediate = true)
public class OpenstackJsonCodecRegister {
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected CodecService codecService;
@Activate
protected void activate() {
codecService.registerCodec(LinkInfo.class, new ThreeDVLinkInfoJsonCodec());
codecService.registerCodec(LinkStatsInfo.class, new ThreeDVLinkStatsInfoJsonCodec());
log.info("Started");
}
@Deactivate
protected void deactivate() {
codecService.unregisterCodec(LinkInfo.class);
codecService.unregisterCodec(LinkStatsInfo.class);
log.info("Stopped");
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.openstacktelemetry.api.LinkInfo;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* JSON codec for 3DV's LinkInfo.
*/
public final class ThreeDVLinkInfoJsonCodec extends JsonCodec<LinkInfo> {
private static final String LINK_ID = "linkId";
private static final String SRC_IP = "srcIp";
private static final String DST_IP = "dstIp";
private static final String SRC_PORT = "srcPort";
private static final String DST_PORT = "dstPort";
private static final String PROTOCOL = "protocol";
private static final String STATS_INFO = "statsInfo";
@Override
public ObjectNode encode(LinkInfo info, CodecContext context) {
checkNotNull(info, "LinkInfo cannot be null");
ObjectNode result = context.mapper().createObjectNode()
.put(LINK_ID, info.linkId())
.put(SRC_IP, info.srcIp())
.put(DST_IP, info.dstIp())
.put(SRC_PORT, info.srcPort())
.put(DST_PORT, info.dstPort())
.put(PROTOCOL, info.protocol());
ObjectNode statsInfoJson =
context.codec(LinkStatsInfo.class).encode(info.linkStats(), context);
result.set(STATS_INFO, statsInfoJson);
return result;
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* JSON codec for 3DV's LinkStatsInfo.
*/
public final class ThreeDVLinkStatsInfoJsonCodec extends JsonCodec<LinkStatsInfo> {
private static final String TX_PACKET = "txPacket";
private static final String RX_PACKET = "rxPacket";
private static final String TX_BYTE = "txByte";
private static final String RX_BYTE = "rxByte";
private static final String TX_DROP = "txDrop";
private static final String RX_DROP = "rxDrop";
private static final String TIMESTAMP = "timestamp";
@Override
public ObjectNode encode(LinkStatsInfo info, CodecContext context) {
checkNotNull(info, "LinkStatsInfo cannot be null");
return context.mapper().createObjectNode()
.put(TX_PACKET, info.getTxPacket())
.put(RX_PACKET, info.getRxPacket())
.put(TX_BYTE, info.getTxByte())
.put(RX_BYTE, info.getRxByte())
.put(TX_DROP, info.getTxDrop())
.put(RX_DROP, info.getRxDrop())
.put(TIMESTAMP, info.getTimestamp());
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.
*/
/**
* Implementations of the codec broker and openstack telemetry entity codecs.
*/
package org.onosproject.openstacktelemetry.codec.json;

View File

@ -0,0 +1,138 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.codec.impl.CodecManager;
import org.onosproject.core.CoreService;
import org.onosproject.openstacktelemetry.api.DefaultLinkInfo;
import org.onosproject.openstacktelemetry.api.LinkInfo;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
import java.util.HashMap;
import java.util.Map;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.onosproject.net.NetTestTools.APP_ID;
import static org.onosproject.openstacktelemetry.api.DefaultLinkStatsInfoTest.createLinkStatsInfo1;
import static org.onosproject.openstacktelemetry.codec.json.ThreeDVLinkInfoJsonMatcher.matchesLinkInfo;
/**
* Unit tests for 3DV LinkInfo codec.
*/
public class ThreeDVLinkInfoJsonCodecTest {
MockCodecContext context;
JsonCodec<LinkInfo> linkInfoCodec;
JsonCodec<LinkStatsInfo> linkStatsInfoCodec;
final CoreService mockCoreService = createMock(CoreService.class);
private static final String REST_APP_ID = "org.onosproject.rest";
private static final String LINK_ID = "of:0000000000000001_1";
private static final String SRC_IP = "10.10.10.1";
private static final String DST_IP = "20.20.20.1";
private static final int SRC_PORT = 100;
private static final int DST_PORT = 200;
private static final String PROTOCOL = "TCP";
private static final LinkStatsInfo LINK_STATS = createLinkStatsInfo1();
@Before
public void setUp() {
context = new MockCodecContext();
linkInfoCodec = new ThreeDVLinkInfoJsonCodec();
linkStatsInfoCodec = new ThreeDVLinkStatsInfoJsonCodec();
assertThat(linkInfoCodec, notNullValue());
assertThat(linkStatsInfoCodec, notNullValue());
expect(mockCoreService.registerApplication(REST_APP_ID))
.andReturn(APP_ID).anyTimes();
replay(mockCoreService);
context.registerService(CoreService.class, mockCoreService);
}
/**
* Tests the Link Info encode.
*/
@Test
public void testLinkInfoEncode() {
LinkInfo linkInfo = DefaultLinkInfo.builder()
.withLinkId(LINK_ID)
.withSrcIp(SRC_IP)
.withDstIp(DST_IP)
.withSrcPort(SRC_PORT)
.withDstPort(DST_PORT)
.withProtocol(PROTOCOL)
.withLinkStats(LINK_STATS)
.build();
ObjectNode nodeJson = linkInfoCodec.encode(linkInfo, context);
assertThat(nodeJson, matchesLinkInfo(linkInfo));
}
/**
* Mock codec context for use in codec unit tests.
*/
private class MockCodecContext implements CodecContext {
private final ObjectMapper mapper = new ObjectMapper();
private final CodecManager manager = new CodecManager();
private final Map<Class<?>, Object> services = new HashMap<>();
/**
* Constructs a new mock codec context.
*/
public MockCodecContext() {
manager.activate();
}
@Override
public ObjectMapper mapper() {
return mapper;
}
@Override
@SuppressWarnings("unchecked")
public <T> JsonCodec<T> codec(Class<T> entityClass) {
if (entityClass == LinkInfo.class) {
return (JsonCodec<T>) linkInfoCodec;
}
if (entityClass == LinkStatsInfo.class) {
return (JsonCodec<T>) linkStatsInfoCodec;
}
return manager.getCodec(entityClass);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getService(Class<T> serviceClass) {
return (T) services.get(serviceClass);
}
// for registering mock services
public <T> void registerService(Class<T> serviceClass, T impl) {
services.put(serviceClass, impl);
}
}
}

View File

@ -0,0 +1,118 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import com.fasterxml.jackson.databind.JsonNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.openstacktelemetry.api.LinkInfo;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
import static org.onosproject.openstacktelemetry.codec.json.ThreeDVLinkStatsInfoJsonMatcher.matchesLinkStatsInfo;
/**
* Hamcrest matcher for ThreeDVLinkInfoJsonCodec.
*/
public final class ThreeDVLinkInfoJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
private final LinkInfo info;
private static final String LINK_ID = "linkId";
private static final String SRC_IP = "srcIp";
private static final String DST_IP = "dstIp";
private static final String SRC_PORT = "srcPort";
private static final String DST_PORT = "dstPort";
private static final String PROTOCOL = "protocol";
private static final String STATS_INFO = "statsInfo";
private ThreeDVLinkInfoJsonMatcher(LinkInfo info) {
this.info = info;
}
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check link ID
String jsonLinkId = jsonNode.get(LINK_ID).asText();
String linkId = info.linkId();
if (!jsonLinkId.equals(linkId)) {
description.appendText("Link ID was " + jsonLinkId);
return false;
}
// check source IP
String jsonSrcIp = jsonNode.get(SRC_IP).asText();
String srcIp = info.srcIp();
if (!jsonSrcIp.equals(srcIp)) {
description.appendText("Source IP was " + jsonSrcIp);
return false;
}
// check destination IP
String jsonDstIp = jsonNode.get(DST_IP).asText();
String dstIp = info.dstIp();
if (!jsonDstIp.equals(dstIp)) {
description.appendText("Destination IP was " + jsonDstIp);
return false;
}
// check source port
int jsonSrcPort = jsonNode.get(SRC_PORT).asInt();
int srcPort = info.srcPort();
if (jsonSrcPort != srcPort) {
description.appendText("Source port was " + jsonSrcPort);
return false;
}
// check destination port
int jsonDstPort = jsonNode.get(DST_PORT).asInt();
int dstPort = info.dstPort();
if (jsonDstPort != dstPort) {
description.appendText("Destination port was " + jsonDstPort);
return false;
}
// check protocol
String jsonProtocol = jsonNode.get(PROTOCOL).asText();
String protocol = info.protocol();
if (!jsonProtocol.equals(protocol)) {
description.appendText("Protocol was " + jsonProtocol);
return false;
}
// check statsInfo
LinkStatsInfo statsInfo = info.linkStats();
ThreeDVLinkStatsInfoJsonMatcher statsInfoJsonMatcher =
matchesLinkStatsInfo(statsInfo);
return statsInfoJsonMatcher.matches(jsonNode.get(STATS_INFO));
}
@Override
public void describeTo(Description description) {
description.appendText(info.toString());
}
/**
* Factory to allocate a link info matcher.
*
* @param info link info object we are looking for
* @return matcher
*/
public static ThreeDVLinkInfoJsonMatcher matchesLinkInfo(LinkInfo info) {
return new ThreeDVLinkInfoJsonMatcher(info);
}
}

View File

@ -0,0 +1,126 @@
/*
* Copyright 2019-present Open Networking Foundation
*
* 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.openstacktelemetry.codec.json;
import com.fasterxml.jackson.databind.JsonNode;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.openstacktelemetry.api.LinkStatsInfo;
/**
* Hamcrest matcher for ThreeDVLinkStatsInfoJsonCodec.
*/
public final class ThreeDVLinkStatsInfoJsonMatcher
extends TypeSafeDiagnosingMatcher<JsonNode> {
private final LinkStatsInfo statsInfo;
private static final String TX_PACKET = "txPacket";
private static final String RX_PACKET = "rxPacket";
private static final String TX_BYTE = "txByte";
private static final String RX_BYTE = "rxByte";
private static final String TX_DROP = "txDrop";
private static final String RX_DROP = "rxDrop";
private static final String TIMESTAMP = "timestamp";
private ThreeDVLinkStatsInfoJsonMatcher(LinkStatsInfo statsInfo) {
this.statsInfo = statsInfo;
}
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check TX packet count
long jsonTxPacket = jsonNode.get(TX_PACKET).asLong();
long txPacket = statsInfo.getTxPacket();
if (jsonTxPacket != txPacket) {
description.appendText("TX packet was " + jsonTxPacket);
return false;
}
// check RX packet count
long jsonRxPacket = jsonNode.get(RX_PACKET).asLong();
long rxPacket = statsInfo.getRxPacket();
if (jsonRxPacket != rxPacket) {
description.appendText("RX packet was " + jsonRxPacket);
return false;
}
// check TX byte count
long jsonTxByte = jsonNode.get(TX_BYTE).asLong();
long txByte = statsInfo.getTxByte();
if (jsonTxByte != txByte) {
description.appendText("TX byte was " + jsonTxByte);
return false;
}
// check RX byte count
long jsonRxByte = jsonNode.get(RX_BYTE).asLong();
long rxByte = statsInfo.getRxByte();
if (jsonRxByte != rxByte) {
description.appendText("RX byte was " + jsonRxByte);
return false;
}
// check TX drop count
long jsonTxDrop = jsonNode.get(TX_DROP).asLong();
long txDrop = statsInfo.getTxDrop();
if (jsonTxDrop != txDrop) {
description.appendText("TX drop was " + jsonTxDrop);
return false;
}
// check RX drop count
long jsonRxDrop = jsonNode.get(RX_DROP).asLong();
long rxDrop = statsInfo.getRxDrop();
if (jsonRxDrop != rxDrop) {
description.appendText("RX drop was " + jsonRxDrop);
return false;
}
// check timestamp
long jsonTimestamp = jsonNode.get(TIMESTAMP).asLong();
long timestamp = statsInfo.getTimestamp();
if (jsonTimestamp != timestamp) {
description.appendText("Timestamp was " + jsonTimestamp);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText(statsInfo.toString());
}
/**
* Factory to allocate a stats info matcher.
*
* @param statsInfo stats info object we are looking for
* @return matcher
*/
public static ThreeDVLinkStatsInfoJsonMatcher matchesLinkStatsInfo(LinkStatsInfo statsInfo) {
return new ThreeDVLinkStatsInfoJsonMatcher(statsInfo);
}
}