blob: fc430f3941d3631aa133a7d2336d09d2953db306 [file] [log] [blame]
Sbhat35d975bdf2017-07-11 11:22:16 -07001/*
2 * Copyright 2015-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
17package org.onosproject.store.statistic.impl;
18
19import org.apache.felix.scr.annotations.Reference;
20import org.apache.felix.scr.annotations.ReferenceCardinality;
21
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 {
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 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
138 public Ip4Address ip() {
139 return Ip4Address.valueOf("127.0.0.1");
140 }
141
142 @Override
143 public int tcpPort() {
144 return 0;
145 }
146 }
147
148 @Before
149 public void setUp() throws Exception {
150 statStore = new DistributedStatisticStore();
151 statStore.cfgService = new ComponentConfigAdapter();
152 mockClusterService = createMock(ClusterService.class);
153 statStore.clusterService = mockClusterService;
154 statStore.clusterService = mockClusterService;
155 nodeId = new NodeId("1");
156 mockControllerNode = new SetMockControllerNode(nodeId);
157
158 expect(mockClusterService.getLocalNode())
159 .andReturn(mockControllerNode).anyTimes();
160 replay(mockClusterService);
161
162 statStore.clusterCommunicator = new ClusterCommunicationServiceAdapter();
163 statStore.mastershipService = new MasterOfAll();
164
165 statStore.activate(mockContext);
166 store = statStore;
167 }
168
169 @After
170 public void tearDown() throws Exception {
171 statStore.deactivate();
172 }
173
174 @Test
175 public void testEmpty() {
176 assertThat(store.getPreviousStatistic(testConnectPoint), is(nullValue()));
177 assertThat(store.getCurrentStatistic(testConnectPoint), is(nullValue()));
178 }
179
180 @Test
181 public void testAddStatistic() {
182
183 FlowEntry flowEntry2 = new DefaultFlowEntry(flowRule1);
184 assertEquals("PENDING_ADD", flowEntry2.state().toString());
185
186 FlowEntry flowTest1 = makeFlowEntry(1);
187 store.prepareForStatistics(flowTest1);
188 store.addOrUpdateStatistic(flowTest1);
189 cp1 = new ConnectPoint(flowTest1.deviceId(), PortNumber.portNumber(0));
190 ConnectPoint cp2 = new ConnectPoint(flowTest1.deviceId(), PortNumber.portNumber(1));
191 assertThat("Current map should be null", store.getCurrentStatistic(cp2), is(nullValue()));
192
193 Set<FlowEntry> currStatistic = store.getCurrentStatistic(cp1);
194 int currTotal = 0;
195 Iterator count = currStatistic.iterator();
196 while (count.hasNext()) {
197 count.next();
198 currTotal++;
199 }
200 assertThat(currTotal, is(1));
201 assertEquals(new NodeId("1"), nodeId);
202 assertEquals("ADDED", flowTest1.state().toString());
203 assertThat(store.getPreviousStatistic(cp1), is(empty()));
204
205 FlowEntry flowTest2 = makeFlowEntry(10);
206 ConnectPoint cp3 = new ConnectPoint(flowTest2.deviceId(), PortNumber.portNumber(0));
207 store.addOrUpdateStatistic(flowTest2);
208 Set<FlowEntry> prevStatistic = store.getPreviousStatistic(cp3);
209 int prevTotal = 0;
210 count = prevStatistic.iterator();
211 while (count.hasNext()) {
212 count.next();
213 prevTotal++;
214 }
215 assertThat(prevTotal, is(1));
216 }
217
218 @Test
219 public void testRemoveStatistic() {
220 FlowEntry flowEntry = makeFlowEntry(1);
221 store.prepareForStatistics(flowEntry);
222 store.addOrUpdateStatistic(flowEntry);
223 cp1 = new ConnectPoint(flowEntry.deviceId(), PortNumber.portNumber(0));
224 assertNotNull(store.getCurrentStatistic(cp1));
225 store.removeFromStatistics(flowEntry);
226 assertThat(store.getCurrentStatistic(cp1), is(empty()));
227 }
228
229}