Sbhat35 | d975bdf | 2017-07-11 11:22:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Sbhat35 | d975bdf | 2017-07-11 11:22:16 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.store.statistic.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Reference; |
| 20 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 21 | |
| 22 | import static org.hamcrest.Matchers.empty; |
| 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.ControllerNode; |
| 33 | import org.onosproject.cluster.NodeId; |
| 34 | import org.onosproject.mastership.MastershipService; |
| 35 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.MastershipRole; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 41 | import org.onosproject.net.flow.DefaultFlowRule; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.FlowEntry; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flow.instructions.Instruction; |
| 47 | import org.onosproject.net.flow.instructions.Instructions; |
| 48 | import org.onosproject.net.intent.IntentTestsMocks; |
| 49 | import org.onosproject.net.statistic.StatisticStore; |
| 50 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
| 51 | import org.osgi.service.component.ComponentContext; |
| 52 | |
| 53 | import java.util.Iterator; |
| 54 | import java.util.Set; |
| 55 | |
| 56 | import static org.easymock.EasyMock.createMock; |
| 57 | |
| 58 | import static org.easymock.EasyMock.expect; |
| 59 | import static org.easymock.EasyMock.replay; |
| 60 | import static org.hamcrest.Matchers.nullValue; |
| 61 | import static org.junit.Assert.assertEquals; |
| 62 | |
| 63 | import static org.junit.Assert.assertNotNull; |
| 64 | import static org.junit.Assert.assertThat; |
| 65 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 66 | import static org.onosproject.net.NetTestTools.did; |
| 67 | |
| 68 | |
| 69 | public 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 |
Jordan Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 138 | public String host() { |
| 139 | return "127.0.0.1"; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Ip4Address ip(boolean resolve) { |
Sbhat35 | d975bdf | 2017-07-11 11:22:16 -0700 | [diff] [blame] | 144 | 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 | } |