bobzhou
Committed by Ray Milkey

[ONOS-2815] add RouterGateway for Router api

Change-Id: I65843e29173782dc53f82163b3ffe369d6be8223
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.Collection;
22 +import java.util.Objects;
23 +
24 +/**
25 + * Representation of a Router gateway.
26 + */
27 +public final class RouterGateway {
28 +
29 + private final TenantNetworkId networkId;
30 + private final boolean enableSnat;
31 + private final Collection<FixedIp> externalFixedIps;
32 +
33 + // Public construction is prohibited
34 + private RouterGateway(TenantNetworkId networkId, boolean enableSnat,
35 + Collection<FixedIp> externalFixedIps) {
36 + this.networkId = checkNotNull(networkId, "networkId cannot be null");
37 + this.enableSnat = checkNotNull(enableSnat, "enableSnat cannot be null");
38 + this.externalFixedIps = checkNotNull(externalFixedIps, "externalFixedIps cannot be null");
39 + }
40 +
41 + /**
42 + * Creates router gateway object.
43 + *
44 + * @param networkId network identifier
45 + * @param enableSnat SNAT enable or not
46 + * @param externalFixedIps external fixed IP
47 + * @return RouterGateway
48 + */
49 + public static RouterGateway routerGateway(TenantNetworkId networkId, boolean enableSnat,
50 + Collection<FixedIp> externalFixedIps) {
51 + return new RouterGateway(networkId, enableSnat, externalFixedIps);
52 + }
53 +
54 + /**
55 + * Returns network identifier.
56 + *
57 + * @return networkId
58 + */
59 + public TenantNetworkId networkId() {
60 + return networkId;
61 + }
62 +
63 + /**
64 + * Return SNAT enable or not.
65 + *
66 + * @return enableSnat
67 + */
68 + public boolean enableSnat() {
69 + return enableSnat;
70 + }
71 +
72 + /**
73 + * Return external fixed Ip.
74 + *
75 + * @return externalFixedIps
76 + */
77 + public Collection<FixedIp> externalFixedIps() {
78 + return externalFixedIps;
79 + }
80 +
81 + @Override
82 + public int hashCode() {
83 + return Objects.hash(networkId, enableSnat, externalFixedIps);
84 + }
85 +
86 + @Override
87 + public boolean equals(Object obj) {
88 + if (this == obj) {
89 + return true;
90 + }
91 + if (obj instanceof RouterGateway) {
92 + final RouterGateway that = (RouterGateway) obj;
93 + return Objects.equals(this.networkId, that.networkId)
94 + && Objects.equals(this.enableSnat, that.enableSnat)
95 + && Objects.equals(this.externalFixedIps, that.externalFixedIps);
96 + }
97 + return false;
98 + }
99 +
100 + @Override
101 + public String toString() {
102 + return toStringHelper(this)
103 + .add("networkId", networkId)
104 + .add("enableSnat", enableSnat)
105 + .add("externalFixedIps", externalFixedIps)
106 + .toString();
107 + }
108 +}
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.HashSet;
24 +import java.util.Set;
25 +
26 +import org.junit.Test;
27 +import org.onosproject.vtnrsc.RouterGateway;
28 +import org.onosproject.vtnrsc.TenantNetworkId;
29 +import org.onosproject.vtnrsc.FixedIp;
30 +
31 +import com.google.common.testing.EqualsTester;
32 +
33 +/**
34 + * Unit tests for RouterGateway class.
35 + */
36 +public class RouterGatewayTest {
37 + final TenantNetworkId networkId1 = TenantNetworkId.networkId("1");
38 + final TenantNetworkId networkId2 = TenantNetworkId.networkId("2");
39 + final Set<FixedIp> fixedIpSet1 = new HashSet<>();
40 + final Set<FixedIp> fixedIpSet2 = new HashSet<>();
41 +
42 + /**
43 + * Checks that the RouterGateway class is immutable.
44 + */
45 + @Test
46 + public void testImmutability() {
47 + assertThatClassIsImmutable(RouterGateway.class);
48 + }
49 +
50 + /**
51 + * Checks the operation of equals().
52 + */
53 + @Test
54 + public void testEquals() {
55 + RouterGateway routerGateway1 = RouterGateway.routerGateway(networkId1,
56 + true,
57 + fixedIpSet1);
58 + RouterGateway routerGateway2 = RouterGateway.routerGateway(networkId1,
59 + true,
60 + fixedIpSet1);
61 + RouterGateway routerGateway3 = RouterGateway.routerGateway(networkId2,
62 + true,
63 + fixedIpSet2);
64 + new EqualsTester().addEqualityGroup(routerGateway1, routerGateway2)
65 + .addEqualityGroup(routerGateway3).testEquals();
66 + }
67 +
68 + /**
69 + * Checks the construction of a RouterGateway object.
70 + */
71 + @Test
72 + public void testConstruction() {
73 + RouterGateway routerGateway = RouterGateway.routerGateway(networkId1,
74 + true,
75 + fixedIpSet1);
76 + assertThat(fixedIpSet1, is(notNullValue()));
77 + assertThat(fixedIpSet1, is(routerGateway.externalFixedIps()));
78 + assertThat(networkId1, is(notNullValue()));
79 + assertThat(networkId1, is(routerGateway.networkId()));
80 + assertThat(routerGateway.enableSnat(), is(true));
81 + }
82 +}