hongtao
Committed by Gerrit Code Review

[ONOS-2815]: add RouterInterface and RouterInterfaceTest

Change-Id: Ia1711581059d217543556e3668633039574be309
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 + */
16 +package org.onosproject.vtnrsc;
17 +
18 +import static com.google.common.base.MoreObjects.toStringHelper;
19 +import static com.google.common.base.Preconditions.checkNotNull;
20 +
21 +import java.util.Objects;
22 +
23 +/**
24 + * Representation of a Router interface.
25 + */
26 +public final class RouterInterface {
27 + private final SubnetId subnetId;
28 + private final VirtualPortId portId;
29 + private final RouterId routerId;
30 + private final TenantId tenantId;
31 +
32 + // Public construction is prohibited
33 + private RouterInterface(SubnetId subnetId, VirtualPortId portId,
34 + RouterId routerId, TenantId tenantId) {
35 + this.subnetId = checkNotNull(subnetId, "subnetId cannot be null");
36 + this.portId = checkNotNull(portId, "portId cannot be null");
37 + this.routerId = checkNotNull(routerId, "routerId cannot be null");
38 + this.tenantId = checkNotNull(tenantId, "tenantId cannot be null");
39 + }
40 +
41 + /**
42 + * Creates router interface object.
43 + *
44 + * @param subnetId subnet identifier
45 + * @param portId port identifier
46 + * @param routerId router identifier
47 + * @param tenantId tenant identifier
48 + * @return RouterInterface
49 + */
50 + public static RouterInterface routerInterface(SubnetId subnetId,
51 + VirtualPortId portId,
52 + RouterId routerId,
53 + TenantId tenantId) {
54 + return new RouterInterface(subnetId, portId, routerId, tenantId);
55 + }
56 +
57 + /**
58 + * Returns subnet identifier.
59 + *
60 + * @return subnetId the subnet identifier
61 + */
62 + public SubnetId subnetId() {
63 + return subnetId;
64 + }
65 +
66 + /**
67 + * Returns port identifier.
68 + *
69 + * @return portId the port identifier
70 + */
71 + public VirtualPortId portId() {
72 + return portId;
73 + }
74 +
75 + /**
76 + * Returns router identifier.
77 + *
78 + * @return routerId the router identifier
79 + */
80 + public RouterId routerId() {
81 + return routerId;
82 + }
83 +
84 + /**
85 + * Returns tenant identifier.
86 + *
87 + * @return tenantId the tenant identifier
88 + */
89 + public TenantId tenantId() {
90 + return tenantId;
91 + }
92 +
93 + @Override
94 + public int hashCode() {
95 + return Objects.hash(subnetId, portId, routerId, tenantId);
96 + }
97 +
98 + @Override
99 + public boolean equals(Object obj) {
100 + if (this == obj) {
101 + return true;
102 + }
103 + if (obj instanceof RouterInterface) {
104 + final RouterInterface that = (RouterInterface) obj;
105 + return Objects.equals(this.subnetId, that.subnetId)
106 + && Objects.equals(this.portId, that.portId)
107 + && Objects.equals(this.routerId, that.routerId)
108 + && Objects.equals(this.tenantId, that.tenantId);
109 + }
110 + return false;
111 + }
112 +
113 + @Override
114 + public String toString() {
115 + return toStringHelper(this).add("subnetId", subnetId)
116 + .add("portId", portId).add("routerId", routerId)
117 + .add("tenantId", tenantId).toString();
118 + }
119 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 + */
16 +package org.onosproject.vtnrsc.router;
17 +
18 +import static org.hamcrest.MatcherAssert.assertThat;
19 +import static org.hamcrest.Matchers.is;
20 +import static org.hamcrest.Matchers.notNullValue;
21 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
22 +
23 +import org.junit.Test;
24 +import org.onosproject.vtnrsc.RouterId;
25 +import org.onosproject.vtnrsc.RouterInterface;
26 +import org.onosproject.vtnrsc.SubnetId;
27 +import org.onosproject.vtnrsc.TenantId;
28 +import org.onosproject.vtnrsc.VirtualPortId;
29 +
30 +import com.google.common.testing.EqualsTester;
31 +
32 +/**
33 + * Unit tests for RouterInterface class.
34 + */
35 +public class RouterInterfaceTest {
36 + private String tenantIdStr = "123";
37 + private String virtualPortId = "1212";
38 + private String routeIdStr1 = "1";
39 + private String routeIdStr2 = "2";
40 + private String subnetIdStr = "1234567";
41 +
42 + /**
43 + * Checks that the RouterInterface class is immutable.
44 + */
45 + @Test
46 + public void testImmutability() {
47 + assertThatClassIsImmutable(RouterInterface.class);
48 + }
49 +
50 + /**
51 + * Checks the operation of equals().
52 + */
53 + @Test
54 + public void testEquals() {
55 + final TenantId tenantId = TenantId.tenantId(tenantIdStr);
56 + final VirtualPortId portId = VirtualPortId.portId(virtualPortId);
57 + final RouterId routerId1 = RouterId.valueOf(routeIdStr1);
58 + final RouterId routerId2 = RouterId.valueOf(routeIdStr2);
59 + final SubnetId subnet = SubnetId.subnetId(subnetIdStr);
60 +
61 + RouterInterface ri1 = RouterInterface.routerInterface(subnet, portId,
62 + routerId1,
63 + tenantId);
64 + RouterInterface ri2 = RouterInterface.routerInterface(subnet, portId,
65 + routerId1,
66 + tenantId);
67 + RouterInterface ri3 = RouterInterface.routerInterface(subnet, portId,
68 + routerId2,
69 + tenantId);
70 +
71 + new EqualsTester().addEqualityGroup(ri1, ri2).addEqualityGroup(ri3)
72 + .testEquals();
73 + }
74 +
75 + /**
76 + * Checks the construction of a RouterInterface object.
77 + */
78 + @Test
79 + public void testConstruction() {
80 + final TenantId tenantId = TenantId.tenantId(tenantIdStr);
81 + final VirtualPortId portId = VirtualPortId.portId(virtualPortId);
82 + final RouterId routerId1 = RouterId.valueOf(routeIdStr1);
83 + final SubnetId subnet = SubnetId.subnetId(subnetIdStr);
84 +
85 + RouterInterface ri1 = RouterInterface.routerInterface(subnet, portId,
86 + routerId1,
87 + tenantId);
88 + assertThat(portId, is(notNullValue()));
89 + assertThat(portId, is(ri1.portId()));
90 + assertThat(tenantId, is(notNullValue()));
91 + assertThat(tenantId, is(ri1.tenantId()));
92 + assertThat(routerId1, is(notNullValue()));
93 + assertThat(routerId1, is(ri1.routerId()));
94 + assertThat(subnet, is(notNullValue()));
95 + assertThat(subnet, is(ri1.subnetId()));
96 + }
97 +}