Phaneendra Manda
Committed by Gerrit Code Review

[ONOS-4226]Service function group class added for sf statistics

Change-Id: I93fa832b8935c065a2384581daa55d304ffe2123
1 +/*
2 + * Copyright 2016-present 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 +
20 +import java.util.Map;
21 +import java.util.Objects;
22 +
23 +import com.google.common.collect.ImmutableMap;
24 +
25 +/**
26 + * Implementation of ServiceFunctionGroup class.
27 + */
28 +public final class ServiceFunctionGroup {
29 +
30 + private final String name;
31 + private final String description;
32 + private final Map<PortPairId, Integer> portPairLoadMap;
33 +
34 + /**
35 + * Creates an instance of service function group.
36 + *
37 + * @param name name of port pair
38 + * @param description description of port pair
39 + * @param portPairLoadMap map of port pair id and its load
40 + */
41 + public ServiceFunctionGroup(String name, String description,
42 + Map<PortPairId, Integer> portPairLoadMap) {
43 + this.name = name;
44 + this.description = description;
45 + this.portPairLoadMap = portPairLoadMap;
46 + }
47 +
48 + /**
49 + * Returns name of service function group.
50 + *
51 + * @return name of service function group
52 + */
53 + public String name() {
54 + return name;
55 + }
56 +
57 + /**
58 + * Returns description of service function group.
59 + *
60 + * @return description of service function group.
61 + */
62 + public String description() {
63 + return description;
64 + }
65 +
66 + /**
67 + * Returns port pair load map.
68 + *
69 + * @return port pair load map
70 + */
71 + public Map<PortPairId, Integer> portPairLoadMap() {
72 + return ImmutableMap.copyOf(portPairLoadMap);
73 + }
74 +
75 + @Override
76 + public int hashCode() {
77 + return Objects.hash(name, description, portPairLoadMap);
78 + }
79 +
80 + @Override
81 + public boolean equals(Object obj) {
82 + if (this == obj) {
83 + return true;
84 + }
85 + if (obj instanceof ServiceFunctionGroup) {
86 + ServiceFunctionGroup that = (ServiceFunctionGroup) obj;
87 + return Objects.equals(name, that.name()) &&
88 + Objects.equals(description, that.description()) &&
89 + Objects.equals(portPairLoadMap, that.portPairLoadMap());
90 + }
91 + return false;
92 + }
93 +
94 + @Override
95 + public String toString() {
96 + return toStringHelper(this)
97 + .add("name", name)
98 + .add("description", description)
99 + .add("portPairLoadMap", portPairLoadMap)
100 + .toString();
101 + }
102 +}
1 +/*
2 + * Copyright 2016-present 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.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
21 +
22 +import java.util.Map;
23 +import java.util.concurrent.ConcurrentHashMap;
24 +
25 +import org.junit.Test;
26 +
27 +import com.google.common.testing.EqualsTester;
28 +
29 +/**
30 + * Unit tests for ServiceFunctionGroup class.
31 + */
32 +public class ServiceFunctionGroupTest {
33 +
34 + /**
35 + * Checks that the ServiceFunctionGroup class is immutable.
36 + */
37 + @Test
38 + public void testImmutability() {
39 + assertThatClassIsImmutable(ServiceFunctionGroup.class);
40 + }
41 +
42 + /**
43 + * Checks the operation of equals() methods.
44 + */
45 + @Test
46 + public void testEquals() {
47 + String name1 = "Firewall";
48 + String description1 = "Firewall service function";
49 + Map<PortPairId, Integer> portPairLoadMap1 = new ConcurrentHashMap<>();
50 + PortPairId portPairId1 = PortPairId.of("a4444444-4a56-2a6e-cd3a-9dee4e2ec345");
51 + portPairLoadMap1.put(portPairId1, Integer.valueOf(2));
52 + ServiceFunctionGroup sfg1 = new ServiceFunctionGroup(name1, description1, portPairLoadMap1);
53 + ServiceFunctionGroup sameAsSfg1 = new ServiceFunctionGroup(name1, description1, portPairLoadMap1);
54 +
55 + String name2 = "Dpi";
56 + String description2 = "Dpi service function";
57 + Map<PortPairId, Integer> portPairLoadMap2 = new ConcurrentHashMap<>();
58 + PortPairId portPairId2 = PortPairId.of("b6666666-4a56-2a6e-cd3a-9dee4e2ec345");
59 + portPairLoadMap2.put(portPairId2, Integer.valueOf(3));
60 + ServiceFunctionGroup sfg2 = new ServiceFunctionGroup(name2, description2, portPairLoadMap2);
61 +
62 + new EqualsTester().addEqualityGroup(sfg1, sameAsSfg1).addEqualityGroup(sfg2).testEquals();
63 + }
64 +
65 + /**
66 + * Checks the construction of a ServiceFunctionGroup object.
67 + */
68 + @Test
69 + public void testConstruction() {
70 +
71 + String name = "Firewall";
72 + String description = "Firewall service function";
73 + Map<PortPairId, Integer> portPairLoadMap = new ConcurrentHashMap<>();
74 + PortPairId portPairId = PortPairId.of("a4444444-4a56-2a6e-cd3a-9dee4e2ec345");
75 + portPairLoadMap.put(portPairId, Integer.valueOf(2));
76 + ServiceFunctionGroup sfg = new ServiceFunctionGroup(name, description, portPairLoadMap);
77 +
78 + assertThat(name, is(sfg.name()));
79 + assertThat(description, is(sfg.description()));
80 + assertThat(2, is(sfg.portPairLoadMap().get(portPairId)));
81 + }
82 +}