Phaneendra Manda
Committed by Gerrit Code Review

[ONOS-3833] Load balance identifier class and UT

Change-Id: Ice90fbc56be8e214ed9e6657a32eff8839311749
1 +/*
2 + * Copyright 2016 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.Preconditions.checkArgument;
19 +
20 +import java.util.Objects;
21 +
22 +import com.google.common.base.MoreObjects;
23 +
24 +/*
25 + * Representation of 5 bit load balance identifier for a service function
26 + */
27 +public final class LoadBalanceId {
28 +
29 + private static final byte MAX_ID = 0x1F;
30 + private final byte loadBalanceId;
31 +
32 + /**
33 + * Default constructor.
34 + *
35 + * @param loadBalanceId service function chain path's load balance identifier
36 + */
37 + private LoadBalanceId(byte loadBalanceId) {
38 + checkArgument(loadBalanceId <= MAX_ID, "Load balance id should not be more than 5 bit identifier");
39 + this.loadBalanceId = (loadBalanceId);
40 + }
41 +
42 + /**
43 + * Returns the SfcLoadBalanceId by setting its value.
44 + *
45 + * @param loadBalanceId service function chain path's load balance identifier
46 + * @return LoadBalanceId
47 + */
48 + public static LoadBalanceId of(byte loadBalanceId) {
49 + return new LoadBalanceId(loadBalanceId);
50 + }
51 +
52 +
53 + /**
54 + * Returns load balance identifier for a service function.
55 + *
56 + * @return loadBalanceId
57 + */
58 + public byte loadBalanceId() {
59 + return loadBalanceId;
60 + }
61 +
62 +
63 + @Override
64 + public int hashCode() {
65 + return Objects.hash(loadBalanceId);
66 + }
67 +
68 + @Override
69 + public boolean equals(Object obj) {
70 + if (this == obj) {
71 + return true;
72 + }
73 + if (!(obj instanceof LoadBalanceId)) {
74 + return false;
75 + }
76 + final LoadBalanceId other = (LoadBalanceId) obj;
77 + return Objects.equals(this.loadBalanceId, other.loadBalanceId);
78 + }
79 +
80 + @Override
81 + public String toString() {
82 + return MoreObjects.toStringHelper(this)
83 + .add("loadBalanceId", loadBalanceId)
84 + .toString();
85 + }
86 +}
1 +/*
2 + * Copyright 2016 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 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 +
25 +import com.google.common.testing.EqualsTester;
26 +
27 +/**
28 + * Unit tests for LoadBalanceId class.
29 + */
30 +public class LoadBalanceIdTest {
31 +
32 + final LoadBalanceId id1 = LoadBalanceId.of((byte) 1);
33 + final LoadBalanceId sameAsId1 = LoadBalanceId.of((byte) 1);
34 + final LoadBalanceId id2 = LoadBalanceId.of((byte) 2);
35 +
36 + /**
37 + * Checks that the LoadBalanceId class is immutable.
38 + */
39 + @Test
40 + public void testImmutability() {
41 + assertThatClassIsImmutable(LoadBalanceId.class);
42 + }
43 +
44 + /**
45 + * Checks the operation of equals() methods.
46 + */
47 + @Test
48 + public void testEquals() {
49 + new EqualsTester().addEqualityGroup(id1, sameAsId1).addEqualityGroup(id2)
50 + .testEquals();
51 + }
52 +
53 + /**
54 + * Checks the construction of a LoadBalanceId object.
55 + */
56 + @Test
57 + public void testConstruction() {
58 + final LoadBalanceId id = LoadBalanceId.of((byte) 1);
59 + assertThat(id, is(notNullValue()));
60 + assertThat(id.loadBalanceId(), is((byte) 1));
61 + }
62 +}