blob: a92345b64dbc9bb1546bc0fbdddddacfde254813 [file] [log] [blame]
Sbhat35d975bdf2017-07-11 11:22:16 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sbhat35d975bdf2017-07-11 11:22:16 -07003 *
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
17package org.onosproject.store.statistic.impl;
18
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Reference;
20import org.osgi.service.component.annotations.ReferenceCardinality;
Sbhat35d975bdf2017-07-11 11:22:16 -070021
22import static org.hamcrest.Matchers.empty;
23
24import static org.hamcrest.Matchers.is;
25
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.onlab.packet.Ip4Address;
30import org.onosproject.cfg.ComponentConfigAdapter;
31import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.ControllerNode;
33import org.onosproject.cluster.NodeId;
34import org.onosproject.mastership.MastershipService;
35import org.onosproject.mastership.MastershipServiceAdapter;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.MastershipRole;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.flow.DefaultFlowEntry;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowEntry;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.flow.instructions.Instruction;
47import org.onosproject.net.flow.instructions.Instructions;
48import org.onosproject.net.intent.IntentTestsMocks;
49import org.onosproject.net.statistic.StatisticStore;
50import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
51import org.osgi.service.component.ComponentContext;
52
53import java.util.Iterator;
54import java.util.Set;
55
56import static org.easymock.EasyMock.createMock;
57
58import static org.easymock.EasyMock.expect;
59import static org.easymock.EasyMock.replay;
60import static org.hamcrest.Matchers.nullValue;
61import static org.junit.Assert.assertEquals;
62
63import static org.junit.Assert.assertNotNull;
64import static org.junit.Assert.assertThat;
65import static org.onosproject.net.NetTestTools.APP_ID;
66import static org.onosproject.net.NetTestTools.did;
67
68
69public class DistributedStatisticStoreTest {
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sbhat35d975bdf2017-07-11 11:22:16 -070071 protected MastershipService mastershipService;
72
73 DistributedStatisticStore statStore;
74 StatisticStore store;
75 ComponentContext mockContext = null;
76 ControllerNode mockControllerNode;
77 NodeId nodeId;
78 ConnectPoint cp1;
79 DeviceId deviceId = did("1");
80
81 private ClusterService mockClusterService;
82 private final FlowRule fRule = new IntentTestsMocks.MockFlowRule(1);
83 Instruction output = Instructions.createOutput(PortNumber.portNumber(0));
84 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
85 TrafficTreatment treatment = tBuilder
86 .add(output)
87 .build();
88 private final DefaultFlowRule nullFlowRule = new DefaultFlowRule(fRule);
89 private final FlowRule flowRule1 =
90 DefaultFlowRule.builder()
91 .forDevice(deviceId)
92 .withSelector(new IntentTestsMocks.MockSelector())
93 .withTreatment(treatment)
94 .withPriority(22)
95 .makeTemporary(44)
96 .fromApp(APP_ID)
97 .build();
98 private final ConnectPoint testConnectPoint = new ConnectPoint(flowRule1.deviceId(), PortNumber.portNumber(1));
99 private final DefaultFlowEntry makeFlowEntry(int uniqueValue) {
100 FlowRule rule = DefaultFlowRule.builder()
101 .forDevice(deviceId)
102 .withSelector(new IntentTestsMocks.MockSelector())
103 .withTreatment(treatment)
104 .withPriority(uniqueValue)
105 .withCookie(uniqueValue)
106 .makeTemporary(uniqueValue)
107 .build();
108
109 return new DefaultFlowEntry(rule, FlowEntry.FlowEntryState.ADDED,
110 uniqueValue, uniqueValue, uniqueValue);
111 }
112
113 static class MasterOfAll extends MastershipServiceAdapter {
114 @Override
115 public MastershipRole getLocalRole(DeviceId deviceId) {
116 return MastershipRole.MASTER;
117 }
118
119 @Override
120 public NodeId getMasterFor(DeviceId deviceId) {
121 return new NodeId("1");
122 }
123 }
124
125 private static class SetMockControllerNode implements ControllerNode {
126 final NodeId id;
127
128 public SetMockControllerNode(NodeId id) {
129 this.id = id;
130 }
131
132 @Override
133 public NodeId id() {
134 return this.id;
135 }
136
137 @Override
Jordan Haltermane458f002018-09-18 17:42:05 -0700138 public String host() {
139 return "127.0.0.1";
140 }
141
142 @Override
143 public Ip4Address ip(boolean resolve) {
Sbhat35d975bdf2017-07-11 11:22:16 -0700144 return Ip4Address.valueOf("127.0.0.1");
145 }
146
147 @Override
148 public int tcpPort() {
149 return 0;
150 }
151 }
152
153 @Before
154 public void setUp() throws Exception {
155 statStore = new DistributedStatisticStore();
156 statStore.cfgService = new ComponentConfigAdapter();
157 mockClusterService = createMock(ClusterService.class);
158 statStore.clusterService = mockClusterService;
159 statStore.clusterService = mockClusterService;
160 nodeId = new NodeId("1");
161 mockControllerNode = new SetMockControllerNode(nodeId);
162
163 expect(mockClusterService.getLocalNode())
164 .andReturn(mockControllerNode).anyTimes();
165 replay(mockClusterService);
166
167 statStore.clusterCommunicator = new ClusterCommunicationServiceAdapter();
168 statStore.mastershipService = new MasterOfAll();
169
170 statStore.activate(mockContext);
171 store = statStore;
172 }
173
174 @After
175 public void tearDown() throws Exception {
176 statStore.deactivate();
177 }
178
179 @Test
180 public void testEmpty() {
181 assertThat(store.getPreviousStatistic(testConnectPoint), is(nullValue()));
182 assertThat(store.getCurrentStatistic(testConnectPoint), is(nullValue()));
183 }
184
185 @Test
186 public void testAddStatistic() {
187
188 FlowEntry flowEntry2 = new DefaultFlowEntry(flowRule1);
189 assertEquals("PENDING_ADD", flowEntry2.state().toString());
190
191 FlowEntry flowTest1 = makeFlowEntry(1);
192 store.prepareForStatistics(flowTest1);
193 store.addOrUpdateStatistic(flowTest1);
194 cp1 = new ConnectPoint(flowTest1.deviceId(), PortNumber.portNumber(0));
195 ConnectPoint cp2 = new ConnectPoint(flowTest1.deviceId(), PortNumber.portNumber(1));
196 assertThat("Current map should be null", store.getCurrentStatistic(cp2), is(nullValue()));
197
198 Set<FlowEntry> currStatistic = store.getCurrentStatistic(cp1);
199 int currTotal = 0;
200 Iterator count = currStatistic.iterator();
201 while (count.hasNext()) {
202 count.next();
203 currTotal++;
204 }
205 assertThat(currTotal, is(1));
206 assertEquals(new NodeId("1"), nodeId);
207 assertEquals("ADDED", flowTest1.state().toString());
208 assertThat(store.getPreviousStatistic(cp1), is(empty()));
209
210 FlowEntry flowTest2 = makeFlowEntry(10);
211 ConnectPoint cp3 = new ConnectPoint(flowTest2.deviceId(), PortNumber.portNumber(0));
212 store.addOrUpdateStatistic(flowTest2);
213 Set<FlowEntry> prevStatistic = store.getPreviousStatistic(cp3);
214 int prevTotal = 0;
215 count = prevStatistic.iterator();
216 while (count.hasNext()) {
217 count.next();
218 prevTotal++;
219 }
220 assertThat(prevTotal, is(1));
221 }
222
223 @Test
224 public void testRemoveStatistic() {
225 FlowEntry flowEntry = makeFlowEntry(1);
226 store.prepareForStatistics(flowEntry);
227 store.addOrUpdateStatistic(flowEntry);
228 cp1 = new ConnectPoint(flowEntry.deviceId(), PortNumber.portNumber(0));
229 assertNotNull(store.getCurrentStatistic(cp1));
230 store.removeFromStatistics(flowEntry);
231 assertThat(store.getCurrentStatistic(cp1), is(empty()));
232 }
233
234}