jiangrui
Committed by Gerrit Code Review

[ONOS2822]: add implementation of router api DefaultRouter and UT DefaultRouteTest

Change-Id: Ia13b56c5bb24c359b3a3ce6164cc9c84e52b5143
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.List;
22 +import java.util.Objects;
23 +
24 +/**
25 + * Default implementation of router interface.
26 + */
27 +public final class DefaultRouter implements Router {
28 + private final RouterId id;
29 + private final String name;
30 + private final boolean adminStateUp;
31 + private final Status status;
32 + private final boolean distributed;
33 + private final RouterGateway externalGatewayInfo;
34 + private final VirtualPortId gatewayPortId;
35 + private final TenantId tenantId;
36 + private final List<String> routes;
37 +
38 + /**
39 + * Creates router object.
40 + *
41 + * @param id router identifier
42 + * @param routerName the name of router
43 + * @param adminStateUp the status of admin state
44 + * @param status the status of router
45 + * @param distributed the status of router distributed
46 + * @param externalGatewayInfo the gateway info of router
47 + * @param gatewayPortId the port identifier of router gateway
48 + * @param tenantId the tenant identifier
49 + * @param routes the routes configure
50 + */
51 + public DefaultRouter(RouterId id, String routerName, boolean adminStateUp,
52 + Status status, boolean distributed,
53 + RouterGateway externalGatewayInfo,
54 + VirtualPortId gatewayPortId, TenantId tenantId,
55 + List<String> routes) {
56 + this.id = checkNotNull(id, "id cannot be null");
57 + this.name = routerName;
58 + this.adminStateUp = checkNotNull(adminStateUp, "adminStateUp cannot be null");
59 + this.status = checkNotNull(status, "status cannot be null");
60 + this.distributed = checkNotNull(distributed, "distributed cannot be null");
61 + this.externalGatewayInfo = externalGatewayInfo;
62 + this.gatewayPortId = gatewayPortId;
63 + this.tenantId = checkNotNull(tenantId, "tenantId cannot be null");
64 + this.routes = routes;
65 + }
66 +
67 + @Override
68 + public RouterId id() {
69 + return id;
70 + }
71 +
72 + @Override
73 + public String name() {
74 + return name;
75 + }
76 +
77 + @Override
78 + public boolean adminStateUp() {
79 + return adminStateUp;
80 + }
81 +
82 + @Override
83 + public Status status() {
84 + return status;
85 + }
86 +
87 + @Override
88 + public boolean distributed() {
89 + return distributed;
90 + }
91 +
92 + @Override
93 + public RouterGateway externalGatewayInfo() {
94 + return externalGatewayInfo;
95 + }
96 +
97 + @Override
98 + public VirtualPortId gatewayPortid() {
99 + return gatewayPortId;
100 + }
101 +
102 + @Override
103 + public TenantId tenantId() {
104 + return tenantId;
105 + }
106 +
107 + @Override
108 + public List<String> routes() {
109 + return routes;
110 + }
111 +
112 + @Override
113 + public int hashCode() {
114 + return Objects.hash(id, name, adminStateUp, status, distributed,
115 + externalGatewayInfo, gatewayPortId, routes);
116 + }
117 +
118 + @Override
119 + public boolean equals(Object obj) {
120 + if (this == obj) {
121 + return true;
122 + }
123 + if (obj instanceof DefaultRouter) {
124 + final DefaultRouter that = (DefaultRouter) obj;
125 + return Objects.equals(this.id, that.id)
126 + && Objects.equals(this.name, that.name)
127 + && Objects.equals(this.adminStateUp, that.adminStateUp)
128 + && Objects.equals(this.status, that.status)
129 + && Objects.equals(this.distributed, that.distributed)
130 + && Objects.equals(this.externalGatewayInfo,
131 + that.externalGatewayInfo)
132 + && Objects.equals(this.gatewayPortId, that.gatewayPortId)
133 + && Objects.equals(this.routes, that.routes);
134 + }
135 + return false;
136 + }
137 +
138 + @Override
139 + public String toString() {
140 + return toStringHelper(this).add("id", id).add("routerName", name)
141 + .add("adminStateUp", adminStateUp).add("status", status)
142 + .add("distributed", distributed)
143 + .add("externalGatewayInfo", externalGatewayInfo)
144 + .add("gatewayPortid", gatewayPortId).add("routes", routes).toString();
145 + }
146 +}
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 java.util.Collections;
24 +
25 +import org.junit.Test;
26 +import org.onosproject.vtnrsc.DefaultRouter;
27 +import org.onosproject.vtnrsc.Router;
28 +import org.onosproject.vtnrsc.RouterGateway;
29 +import org.onosproject.vtnrsc.RouterId;
30 +import org.onosproject.vtnrsc.TenantId;
31 +import org.onosproject.vtnrsc.TenantNetworkId;
32 +import org.onosproject.vtnrsc.VirtualPortId;
33 +
34 +import com.google.common.testing.EqualsTester;
35 +
36 +/**
37 + * Unit tests for DefaultRouter class.
38 + */
39 +public class DefaultRouterTest {
40 +
41 + private String tenantIdStr = "123";
42 + private String virtualPortId = "1212";
43 + private String routeIdStr1 = "1";
44 + private String routeIdStr2 = "2";
45 + private String routerName = "router";
46 + private String tenantNetworkId = "1234567";
47 +
48 + /**
49 + * Checks that the DefaultRouter class is immutable.
50 + */
51 + @Test
52 + public void testImmutability() {
53 + assertThatClassIsImmutable(DefaultRouter.class);
54 + }
55 +
56 + /**
57 + * Checks the operation of equals().
58 + */
59 + @Test
60 + public void testEquals() {
61 + final TenantId tenantId = TenantId.tenantId(tenantIdStr);
62 + final VirtualPortId portId = VirtualPortId.portId(virtualPortId);
63 + final RouterId routerId1 = RouterId.valueOf(routeIdStr1);
64 + final RouterId routerId2 = RouterId.valueOf(routeIdStr2);
65 + final TenantNetworkId networkId = TenantNetworkId
66 + .networkId(tenantNetworkId);
67 + final RouterGateway routerGateway = RouterGateway.routerGateway(
68 + networkId,
69 + true,
70 + Collections
71 + .emptySet());
72 +
73 + Router r1 = new DefaultRouter(routerId1, routerName, false,
74 + Router.Status.ACTIVE, false,
75 + routerGateway, portId, tenantId, null);
76 + Router r2 = new DefaultRouter(routerId1, routerName, false,
77 + Router.Status.ACTIVE, false,
78 + routerGateway, portId, tenantId, null);
79 + Router r3 = new DefaultRouter(routerId2, routerName, false,
80 + Router.Status.ACTIVE, false,
81 + routerGateway, portId, tenantId, null);
82 +
83 + new EqualsTester().addEqualityGroup(r1, r2).addEqualityGroup(r3)
84 + .testEquals();
85 + }
86 +
87 + /**
88 + * Checks the construction of a DefaultRouter object.
89 + */
90 + @Test
91 + public void testConstruction() {
92 + final TenantId tenantId = TenantId.tenantId(tenantIdStr);
93 + final VirtualPortId portId = VirtualPortId.portId(virtualPortId);
94 + final RouterId routerId = RouterId.valueOf(routeIdStr1);
95 + final TenantNetworkId networkId = TenantNetworkId
96 + .networkId(tenantNetworkId);
97 + final RouterGateway routerGateway = RouterGateway.routerGateway(
98 + networkId,
99 + true,
100 + Collections
101 + .emptySet());
102 +
103 + Router r1 = new DefaultRouter(routerId, routerName, false,
104 + Router.Status.ACTIVE, false,
105 + routerGateway, portId, tenantId, null);
106 + assertThat(routerId, is(notNullValue()));
107 + assertThat(routerId, is(r1.id()));
108 + assertThat(tenantId, is(notNullValue()));
109 + assertThat(tenantId, is(r1.tenantId()));
110 + assertThat(routerGateway, is(notNullValue()));
111 + assertThat(routerGateway, is(r1.externalGatewayInfo()));
112 + }
113 +
114 +}