Committed by
Bob zhou
[ONOS-2815] add RouterId for Router api
Change-Id: I597dfac4f88a67f1b1f91f469af5950b751554ec
Showing
2 changed files
with
140 additions
and
0 deletions
| 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 | + * Immutable representation of a router identifier. | ||
| 25 | + */ | ||
| 26 | +public final class RouterId { | ||
| 27 | + | ||
| 28 | + private final String routerId; | ||
| 29 | + | ||
| 30 | + // Public construction is prohibited | ||
| 31 | + private RouterId(String routerId) { | ||
| 32 | + checkNotNull(routerId, "routerId cannot be null"); | ||
| 33 | + this.routerId = routerId; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Creates a router identifier. | ||
| 38 | + * | ||
| 39 | + * @param routerId the router identifier | ||
| 40 | + * @return the router identifier | ||
| 41 | + */ | ||
| 42 | + public static RouterId valueOf(String routerId) { | ||
| 43 | + return new RouterId(routerId); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Returns the router identifier. | ||
| 48 | + * | ||
| 49 | + * @return the router identifier | ||
| 50 | + */ | ||
| 51 | + public String routerId() { | ||
| 52 | + return routerId; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public int hashCode() { | ||
| 57 | + return routerId.hashCode(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Override | ||
| 61 | + public boolean equals(Object obj) { | ||
| 62 | + if (this == obj) { | ||
| 63 | + return true; | ||
| 64 | + } | ||
| 65 | + if (obj instanceof RouterId) { | ||
| 66 | + final RouterId that = (RouterId) obj; | ||
| 67 | + return Objects.equals(this.routerId, that.routerId); | ||
| 68 | + } | ||
| 69 | + return false; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Override | ||
| 73 | + public String toString() { | ||
| 74 | + return toStringHelper(this).add("routerId", routerId).toString(); | ||
| 75 | + } | ||
| 76 | +} | ||
| 77 | + |
| 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 | + | ||
| 26 | +import com.google.common.testing.EqualsTester; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * Unit tests for RouterId class. | ||
| 30 | + */ | ||
| 31 | +public class RouterIdTest { | ||
| 32 | + final RouterId routerId1 = RouterId.valueOf("1"); | ||
| 33 | + final RouterId sameAsRouterId1 = RouterId.valueOf("1"); | ||
| 34 | + final RouterId routerId2 = RouterId.valueOf("2"); | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Checks that the RouterId class is immutable. | ||
| 38 | + */ | ||
| 39 | + @Test | ||
| 40 | + public void testImmutability() { | ||
| 41 | + assertThatClassIsImmutable(RouterId.class); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * Checks the operation of equals() methods. | ||
| 46 | + */ | ||
| 47 | + @Test | ||
| 48 | + public void testEquals() { | ||
| 49 | + new EqualsTester().addEqualityGroup(routerId1, sameAsRouterId1).addEqualityGroup(routerId2) | ||
| 50 | + .testEquals(); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * Checks the construction of a RouterId object. | ||
| 55 | + */ | ||
| 56 | + @Test | ||
| 57 | + public void testConstruction() { | ||
| 58 | + final String routerIdValue = "s"; | ||
| 59 | + final RouterId routerId = RouterId.valueOf(routerIdValue); | ||
| 60 | + assertThat(routerId, is(notNullValue())); | ||
| 61 | + assertThat(routerId.routerId(), is(routerIdValue)); | ||
| 62 | + } | ||
| 63 | +} |
-
Please register or login to post a comment