Open Kilda Java Documentation
RecordHandlerTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.kafka;
17 
18 import static org.easymock.EasyMock.anyObject;
19 import static org.easymock.EasyMock.eq;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.expectLastCall;
22 import static org.easymock.EasyMock.verify;
23 
36 
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39 import net.floodlightcontroller.core.IOFSwitch;
40 import net.floodlightcontroller.core.internal.OFSwitch;
41 import net.floodlightcontroller.core.module.FloodlightModuleContext;
42 import org.easymock.EasyMockSupport;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.projectfloodlight.openflow.protocol.OFPortDesc;
46 import org.projectfloodlight.openflow.types.DatapathId;
47 import org.projectfloodlight.openflow.types.OFPort;
48 
49 import java.util.Map;
50 
51 public class RecordHandlerTest extends EasyMockSupport {
52  private static final FloodlightModuleContext context = new FloodlightModuleContext();
53 
54  private ISwitchManager switchManager;
55  private KafkaMessageProducer producer;
56  private ConsumerContext consumerContext;
57  private RecordHandlerMock handler;
58 
59  @Before
60  public void setUp() throws Exception {
61  switchManager = createStrictMock(SwitchManager.class);
62  producer = createMock(KafkaMessageProducer.class);
63 
64  context.addService(KafkaMessageProducer.class, producer);
65  context.addService(ISwitchManager.class, switchManager);
66 
67  KafkaMessageCollector collectorModule = new KafkaMessageCollector();
68  context.addConfigParam(collectorModule, "topic", "");
69  context.addConfigParam(collectorModule, "bootstrap-servers", "");
70  collectorModule.init(context);
71 
72  ConfigurationProvider provider = ConfigurationProvider.of(context, collectorModule);
73  KafkaTopicsConfig topicsConfig = provider.getConfiguration(KafkaTopicsConfig.class);
74 
75  consumerContext = new ConsumerContext(context, topicsConfig);
76 
77  handler = new RecordHandlerMock(consumerContext);
78  }
79 
85  @Test
86  public void networkDumpTest() {
87  // Cook mock data for ISwitchManager::getAllSwitchMap
88  // Two switches with two ports on each
89 
90  // switches for ISwitchManager::getAllSwitchMap
91  OFSwitch iofSwitch1 = mock(OFSwitch.class);
92  OFSwitch iofSwitch2 = mock(OFSwitch.class);
93 
94  Map<DatapathId, IOFSwitch> switches = ImmutableMap.of(
95  DatapathId.of(1), iofSwitch1,
96  DatapathId.of(2), iofSwitch2
97  );
98 
99  for (DatapathId swId : switches.keySet()) {
100  IOFSwitch sw = switches.get(swId);
101  expect(sw.isActive()).andReturn(true).anyTimes();
102  expect(sw.getId()).andReturn(swId).anyTimes();
103  }
104 
105  expect(switchManager.getAllSwitchMap()).andReturn(switches);
106 
107  // ports for OFSwitch::getEnabledPorts
108  OFPortDesc ofPortDesc1 = mock(OFPortDesc.class);
109  OFPortDesc ofPortDesc2 = mock(OFPortDesc.class);
110  OFPortDesc ofPortDesc3 = mock(OFPortDesc.class);
111  OFPortDesc ofPortDesc4 = mock(OFPortDesc.class);
112  OFPortDesc ofPortDesc5 = mock(OFPortDesc.class);
113 
114  expect(ofPortDesc1.getPortNo()).andReturn(OFPort.ofInt(1)).times(2);
115  expect(ofPortDesc2.getPortNo()).andReturn(OFPort.ofInt(2)).times(2);
116  expect(ofPortDesc3.getPortNo()).andReturn(OFPort.ofInt(3)).times(2);
117  expect(ofPortDesc4.getPortNo()).andReturn(OFPort.ofInt(4)).times(2);
118  // we don't want disco on -2 port
119  expect(ofPortDesc5.getPortNo()).andReturn(OFPort.ofInt(-2)).times(2);
120 
121 
122  expect(iofSwitch1.getEnabledPorts()).andReturn(ImmutableList.of(
123  ofPortDesc1,
124  ofPortDesc2
125  ));
126  expect(iofSwitch2.getEnabledPorts()).andReturn(ImmutableList.of(
127  ofPortDesc3,
128  ofPortDesc4,
129  ofPortDesc5
130  ));
131 
132  // Logic in SwitchEventCollector.buildSwitchInfoData is too complicated and requires a lot
133  // of mocking code so I replaced it with mock on kafkaMessageCollector.buildSwitchInfoData
134  handler.overrideNetworkDumpSwitchData(
135  DatapathId.of(1),
136  new NetworkDumpSwitchData(new SwitchId("ff:01")));
137  handler.overrideNetworkDumpSwitchData(
138  DatapathId.of(2),
139  new NetworkDumpSwitchData(new SwitchId("ff:02")));
140 
141  // setup hook for verify that we create new message for producer
142  producer.postMessage(eq(consumerContext.getKafkaTopoDiscoTopic()), anyObject(InfoMessage.class));
143  expectLastCall().times(8);
144 
145  Producer kafkaProducer = createMock(Producer.class);
146  expect(producer.getProducer()).andReturn(kafkaProducer).times(2);
147  kafkaProducer.enableGuaranteedOrder(eq(consumerContext.getKafkaTopoDiscoTopic()));
148  kafkaProducer.disableGuaranteedOrder(eq(consumerContext.getKafkaTopoDiscoTopic()));
149 
150  replayAll();
151 
152  // Create CommandMessage with NetworkCommandData for trigger network dump
154  System.currentTimeMillis(), Utils.SYSTEM_CORRELATION_ID,
156 
157 
158  // KafkaMessageCollector contains a complicated run logic with couple nested private
159  // classes, threading and that is very painful for writing clear looking test code so I
160  // created the simple method in KafkaMessageCollector for simplifying test logic.
161  handler.handleMessage(command);
162 
163  verify(producer);
164 
165  // TODO: verify content of InfoMessage in producer.postMessage
166  }
167 
168 }
static final String SYSTEM_CORRELATION_ID
Definition: Utils.java:73
Map< DatapathId, IOFSwitch > getAllSwitchMap()
synchronized void disableGuaranteedOrder(String topic)
Definition: Producer.java:57
def command(payload, fields)
Definition: share.py:102
static ConfigurationProvider of(FloodlightModuleContext moduleContext, IFloodlightModule module)
synchronized void enableGuaranteedOrder(String topic)
Definition: Producer.java:48
net
Definition: plan-b.py:46