Sbhat35 | d975bdf | 2017-07-11 11:22:16 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 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 |
| 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 | } |