Add link info and stats class with unit tests for openstacktelemetry

Change-Id: If2e28119bdad0cf9949131c046eba2cbb81c1cd2
This commit is contained in:
Jian Li 2019-01-10 18:20:03 +09:00
parent 1070de9e0a
commit 866416f5ad
6 changed files with 911 additions and 0 deletions

View File

@ -0,0 +1,190 @@
/*
* 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.api;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
/**
* Implementation of Link info.
*/
public final class DefaultLinkInfo implements LinkInfo {
private final String linkId;
private final String srcIp;
private final int srcPort;
private final String dstIp;
private final int dstPort;
private final LinkStatsInfo statsInfo;
private final String protocol;
// default constructor not intended for invoked outside of this class
private DefaultLinkInfo(String linkId, String srcIp, int srcPort,
String dstIp, int dstPort,
LinkStatsInfo statsInfo, String protocol) {
this.linkId = linkId;
this.srcIp = srcIp;
this.srcPort = srcPort;
this.dstIp = dstIp;
this.dstPort = dstPort;
this.statsInfo = statsInfo;
this.protocol = protocol;
}
@Override
public String linkId() {
return linkId;
}
@Override
public String srcIp() {
return srcIp;
}
@Override
public int srcPort() {
return srcPort;
}
@Override
public String dstIp() {
return dstIp;
}
@Override
public int dstPort() {
return dstPort;
}
@Override
public LinkStatsInfo linkStats() {
return statsInfo;
}
@Override
public String protocol() {
return protocol;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultLinkInfo that = (DefaultLinkInfo) o;
return srcPort == that.srcPort &&
dstPort == that.dstPort &&
Objects.equal(linkId, that.linkId) &&
Objects.equal(srcIp, that.srcIp) &&
Objects.equal(dstIp, that.dstIp) &&
Objects.equal(statsInfo, that.statsInfo) &&
Objects.equal(protocol, that.protocol);
}
@Override
public int hashCode() {
return Objects.hashCode(linkId, srcIp, srcPort, dstIp, dstPort, statsInfo, protocol);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("linkId", linkId)
.add("srcIp", srcIp)
.add("srcPort", srcPort)
.add("dstIp", dstIp)
.add("dstPort", dstPort)
.add("statsInfo", statsInfo)
.add("protocol", protocol)
.toString();
}
/**
* Obtains a default link info builder object.
*
* @return link info builder object
*/
public static LinkInfo.Builder builder() {
return new DefaultLinkInfo.DefaultBuilder();
}
/**
* Builder class of LinkInfo.
*/
public static final class DefaultBuilder implements LinkInfo.Builder {
private String linkId;
private String srcIp;
private int srcPort;
private String dstIp;
private int dstPort;
private LinkStatsInfo linkStats;
private String protocol;
private DefaultBuilder() {
}
@Override
public Builder withLinkId(String linkId) {
this.linkId = linkId;
return this;
}
@Override
public Builder withSrcIp(String srcIp) {
this.srcIp = srcIp;
return this;
}
@Override
public Builder withSrcPort(int srcPort) {
this.srcPort = srcPort;
return this;
}
@Override
public Builder withDstIp(String dstIp) {
this.dstIp = dstIp;
return this;
}
@Override
public Builder withDstPort(int dstPort) {
this.dstPort = dstPort;
return this;
}
@Override
public Builder withLinkStats(LinkStatsInfo linkStats) {
this.linkStats = linkStats;
return this;
}
@Override
public Builder withProtocol(String protocol) {
this.protocol = protocol;
return this;
}
@Override
public DefaultLinkInfo build() {
return new DefaultLinkInfo(linkId, srcIp, srcPort, dstIp,
dstPort, linkStats, protocol);
}
}
}

View File

@ -0,0 +1,189 @@
/*
* 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.api;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
/**
* Implementation of Link stats info.
*/
public final class DefaultLinkStatsInfo implements LinkStatsInfo {
private final long txPacket;
private final long rxPacket;
private final long txByte;
private final long rxByte;
private final long txDrop;
private final long rxDrop;
private final long timestamp;
// private constructor not indented for invoked at outside of this class
private DefaultLinkStatsInfo(long txPacket, long rxPacket, long txByte,
long rxByte, long txDrop, long rxDrop, long timestamp) {
this.txPacket = txPacket;
this.rxPacket = rxPacket;
this.txByte = txByte;
this.rxByte = rxByte;
this.txDrop = txDrop;
this.rxDrop = rxDrop;
this.timestamp = timestamp;
}
@Override
public long getTxPacket() {
return txPacket;
}
@Override
public long getRxPacket() {
return rxPacket;
}
@Override
public long getTxByte() {
return txByte;
}
@Override
public long getRxByte() {
return rxByte;
}
@Override
public long getTxDrop() {
return txDrop;
}
@Override
public long getRxDrop() {
return rxDrop;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultLinkStatsInfo that = (DefaultLinkStatsInfo) o;
return txPacket == that.txPacket &&
rxPacket == that.rxPacket &&
txByte == that.txByte &&
rxByte == that.rxByte &&
txDrop == that.txDrop &&
rxDrop == that.rxDrop &&
timestamp == that.timestamp;
}
@Override
public int hashCode() {
return Objects.hashCode(txPacket, rxPacket, txByte, rxByte, txDrop, rxDrop, timestamp);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("txPacket", txPacket)
.add("rxPacket", rxPacket)
.add("txByte", txByte)
.add("rxByte", rxByte)
.add("txDrop", txDrop)
.add("rxDrop", rxDrop)
.add("timestamp", timestamp)
.toString();
}
/**
* Obtains a default link stats info builder object.
*
* @return link stats info builder object
*/
public static LinkStatsInfo.Builder builder() {
return new DefaultLinkStatsInfo.DefaultBuilder();
}
/**
* Builder class of LinkStatsInfo.
*/
public static final class DefaultBuilder implements LinkStatsInfo.Builder {
private long txPacket;
private long rxPacket;
private long txByte;
private long rxByte;
private long txDrop;
private long rxDrop;
private long timestamp;
private DefaultBuilder() {
}
@Override
public DefaultBuilder withTxPacket(long txPacket) {
this.txPacket = txPacket;
return this;
}
@Override
public DefaultBuilder withRxPacket(long rxPacket) {
this.rxPacket = rxPacket;
return this;
}
@Override
public DefaultBuilder withTxByte(long txByte) {
this.txByte = txByte;
return this;
}
@Override
public DefaultBuilder withRxByte(long rxByte) {
this.rxByte = rxByte;
return this;
}
@Override
public DefaultBuilder withTxDrop(long txDrop) {
this.txDrop = txDrop;
return this;
}
@Override
public DefaultBuilder withRxDrop(long rxDrop) {
this.rxDrop = rxDrop;
return this;
}
@Override
public DefaultBuilder withTimestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
@Override
public LinkStatsInfo build() {
return new DefaultLinkStatsInfo(txPacket, rxPacket, txByte, rxByte,
txDrop, rxDrop, timestamp);
}
}
}

View File

@ -0,0 +1,140 @@
/*
* 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.api;
/**
* Link info interface.
*/
public interface LinkInfo {
/**
* Obtains the link identification.
*
* @return link identification
*/
String linkId();
/**
* Obtains the source IP.
*
* @return source IP
*/
String srcIp();
/**
* Obtains source port number.
*
* @return source port index
*/
int srcPort();
/**
* Obtains the destination IP.
*
* @return destination IP
*/
String dstIp();
/**
* Obtains destination port number.
*
* @return destination port number
*/
int dstPort();
/**
* Obtains link stats.
*
* @return link stats
*/
LinkStatsInfo linkStats();
/**
* Obtains protocol (e.g., TCP/UDP/ICMP).
*
* @return protocol
*/
String protocol();
/**
* Interface of link info builder.
*/
interface Builder {
/**
* Sets link identifier.
*
* @param linkId link identifier
* @return builder instance
*/
Builder withLinkId(String linkId);
/**
* Sets the link source IP.
*
* @param srcIp link source IP
* @return builder instance
*/
Builder withSrcIp(String srcIp);
/**
* Sets the source port number.
*
* @param srcPort source port number
* @return builder instance
*/
Builder withSrcPort(int srcPort);
/**
* Sets the link destination IP.
*
* @param dstIp link destination IP
* @return builder instance
*/
Builder withDstIp(String dstIp);
/**
* Sets the destination port number.
*
* @param dstPort destination port number
* @return builder instance
*/
Builder withDstPort(int dstPort);
/**
* Sets the link stats.
*
* @param linkStats link stats
* @return builder instance
*/
Builder withLinkStats(LinkStatsInfo linkStats);
/**
* Sets protocol.
*
* @param protocol protocol
* @return builder instance
*/
Builder withProtocol(String protocol);
/**
* Creates a LinkInfo instance.
*
* @return LinkInfo instance
*/
DefaultLinkInfo build();
}
}

View File

@ -0,0 +1,140 @@
/*
* 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.api;
/**
* Link stats info interface.
*/
public interface LinkStatsInfo {
/**
* Obtains TX packet count.
*
* @return TX packet count
*/
long getTxPacket();
/**
* Obtains RX packet count.
*
* @return RX packet count
*/
long getRxPacket();
/**
* Obtains TX byte count.
*
* @return TX byte count
*/
long getTxByte();
/**
* Obtains RX byte count.
*
* @return RX byte count
*/
long getRxByte();
/**
* Obtains TX drop count.
*
* @return TX drop count
*/
long getTxDrop();
/**
* Obtains RX drop count.
*
* @return RX drop count
*/
long getRxDrop();
/**
* Obtains timestamp.
*
* @return timestamp
*/
long getTimestamp();
/**
* Builder class of LinkStatsInfo.
*/
interface Builder {
/**
* Sets TX packet count.
*
* @param txPacket TX packet count
* @return builder instance
*/
Builder withTxPacket(long txPacket);
/**
* Sets RX packet count.
*
* @param rxPacket RX packet count
* @return builder instance
*/
Builder withRxPacket(long rxPacket);
/**
* Sets TX byte count.
*
* @param txByte TX byte count
* @return builder instance
*/
Builder withTxByte(long txByte);
/**
* Sets RX byte count.
*
* @param rxByte RX byte count
* @return builder instance
*/
Builder withRxByte(long rxByte);
/**
* Sets TX drop count.
*
* @param txDrop TX drop count
* @return builder instance
*/
Builder withTxDrop(long txDrop);
/**
* Sets RX drop count.
*
* @param rxDrop RX drop count
* @return builder instance
*/
Builder withRxDrop(long rxDrop);
/**
* Sets timestamp.
*
* @param timestamp timestamp
* @return builder instance
*/
Builder withTimestamp(long timestamp);
/**
* Creates a LinkStatsInfo instance.
*
* @return builder instance
*/
LinkStatsInfo build();
}
}

View File

@ -0,0 +1,130 @@
/*
* 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.api;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onosproject.openstacktelemetry.api.DefaultLinkStatsInfoTest.createLinkStatsInfo1;
import static org.onosproject.openstacktelemetry.api.DefaultLinkStatsInfoTest.createLinkStatsInfo2;
/**
* Unit tests for DefaultLinkInfo class.
*/
public final class DefaultLinkInfoTest {
private static final String LINK_ID_1 = "L100";
private static final String LINK_ID_2 = "L200";
private static final String SRC_IP_1 = "10.10.10.1";
private static final String SRC_IP_2 = "10.10.10.2";
private static final int SRC_PORT_1 = 80;
private static final int SRC_PORT_2 = 90;
private static final String DST_IP_1 = "20.20.20.1";
private static final String DST_IP_2 = "20.20.20.2";
private static final int DST_PORT_1 = 100;
private static final int DST_PORT_2 = 110;
private static final LinkStatsInfo LINK_STATS_1 = createLinkStatsInfo1();
private static final LinkStatsInfo LINK_STATS_2 = createLinkStatsInfo2();
private static final String PROTOCOL_1 = "TCP";
private static final String PROTOCOL_2 = "UDP";
private LinkInfo info1;
private LinkInfo sameAsInfo1;
private LinkInfo info2;
/**
* Initial setup for this unit test.
*/
@Before
public void setUp() {
LinkInfo.Builder builder1 = DefaultLinkInfo.builder();
LinkInfo.Builder builder2 = DefaultLinkInfo.builder();
LinkInfo.Builder builder3 = DefaultLinkInfo.builder();
info1 = builder1
.withLinkId(LINK_ID_1)
.withSrcIp(SRC_IP_1)
.withSrcPort(SRC_PORT_1)
.withDstIp(DST_IP_1)
.withDstPort(DST_PORT_1)
.withLinkStats(LINK_STATS_1)
.withProtocol(PROTOCOL_1)
.build();
sameAsInfo1 = builder2
.withLinkId(LINK_ID_1)
.withSrcIp(SRC_IP_1)
.withSrcPort(SRC_PORT_1)
.withDstIp(DST_IP_1)
.withDstPort(DST_PORT_1)
.withLinkStats(LINK_STATS_1)
.withProtocol(PROTOCOL_1)
.build();
info2 = builder3
.withLinkId(LINK_ID_2)
.withSrcIp(SRC_IP_2)
.withSrcPort(SRC_PORT_2)
.withDstIp(DST_IP_2)
.withDstPort(DST_PORT_2)
.withLinkStats(LINK_STATS_2)
.withProtocol(PROTOCOL_2)
.build();
}
/**
* Tests class immutability.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(DefaultLinkInfo.class);
}
/**
* Tests object equality.
*/
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(info1, sameAsInfo1)
.addEqualityGroup(info2).testEquals();
}
/**
* Tests object construction.
*/
@Test
public void testConstruction() {
LinkInfo info = info1;
assertEquals(LINK_ID_1, info.linkId());
assertEquals(SRC_IP_1, info.srcIp());
assertEquals(SRC_PORT_1, info.srcPort());
assertEquals(DST_IP_1, info.dstIp());
assertEquals(DST_PORT_1, info.dstPort());
assertEquals(LINK_STATS_1, info.linkStats());
assertEquals(PROTOCOL_1, info.protocol());
}
}

View File

@ -0,0 +1,122 @@
/*
* 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.api;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Unit tests for DefaultLinkStatsInfo class.
*/
public final class DefaultLinkStatsInfoTest {
private static final long TX_PACKET_1 = 1;
private static final long TX_PACKET_2 = 2;
private static final long RX_PACKET_1 = 1;
private static final long RX_PACKET_2 = 2;
private static final long TX_BYTE_1 = 1;
private static final long TX_BYTE_2 = 2;
private static final long RX_BYTE_1 = 1;
private static final long RX_BYTE_2 = 2;
private static final long TX_DROP_1 = 1;
private static final long TX_DROP_2 = 2;
private static final long RX_DROP_1 = 1;
private static final long RX_DROP_2 = 2;
private static final long TIMESTAMP_1 = 10;
private static final long TIMESTAMP_2 = 20;
private LinkStatsInfo info1;
private LinkStatsInfo sameAsInfo1;
private LinkStatsInfo info2;
static LinkStatsInfo createLinkStatsInfo1() {
return DefaultLinkStatsInfo.builder()
.withTxPacket(TX_PACKET_1)
.withRxPacket(RX_PACKET_1)
.withTxByte(TX_BYTE_1)
.withRxByte(RX_BYTE_1)
.withTxDrop(TX_DROP_1)
.withRxDrop(RX_DROP_1)
.withTimestamp(TIMESTAMP_1)
.build();
}
static LinkStatsInfo createLinkStatsInfo2() {
return DefaultLinkStatsInfo.builder()
.withTxPacket(TX_PACKET_2)
.withRxPacket(RX_PACKET_2)
.withTxByte(TX_BYTE_2)
.withRxByte(RX_BYTE_2)
.withTxDrop(TX_DROP_2)
.withRxDrop(RX_DROP_2)
.withTimestamp(TIMESTAMP_2)
.build();
}
/**
* Initial setup for this unit test.
*/
@Before
public void setUp() {
info1 = createLinkStatsInfo1();
sameAsInfo1 = createLinkStatsInfo1();
info2 = createLinkStatsInfo2();
}
/**
* Tests class immutability.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(DefaultLinkStatsInfo.class);
}
/**
* Tests object equality.
*/
@Test
public void testEquality() {
new EqualsTester()
.addEqualityGroup(info1, sameAsInfo1)
.addEqualityGroup(info2).testEquals();
}
/**
* Tests object construction.
*/
@Test
public void testConstruction() {
LinkStatsInfo info = info1;
assertEquals(TX_PACKET_1, info.getTxPacket());
assertEquals(RX_PACKET_1, info.getRxPacket());
assertEquals(TX_BYTE_1, info.getTxByte());
assertEquals(RX_BYTE_1, info.getRxByte());
assertEquals(TX_DROP_1, info.getTxDrop());
assertEquals(RX_DROP_1, info.getRxDrop());
assertEquals(TIMESTAMP_1, info.getTimestamp());
}
}