2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.openflow.controller.impl;
19 import java.io.IOException;
20 import java.util.Dictionary;
21 import java.util.Hashtable;
23 import java.util.stream.IntStream;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onlab.junit.TestTools;
28 import org.onlab.util.ItemNotFoundException;
29 import org.onosproject.net.DeviceId;
30 import org.onosproject.net.driver.Driver;
31 import org.onosproject.openflow.DriverAdapter;
32 import org.onosproject.openflow.DriverServiceAdapter;
33 import org.onosproject.openflow.OFDescStatsReplyAdapter;
34 import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
35 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.io.Files;
41 import static com.google.common.io.ByteStreams.toByteArray;
42 import static com.google.common.io.Files.write;
43 import static org.hamcrest.MatcherAssert.assertThat;
44 import static org.hamcrest.Matchers.hasItem;
45 import static org.hamcrest.Matchers.is;
46 import static org.hamcrest.Matchers.lessThan;
47 import static org.hamcrest.Matchers.not;
48 import static org.hamcrest.Matchers.notNullValue;
49 import static org.hamcrest.Matchers.nullValue;
52 * Unit tests for the OpenFlow controller class.
54 public class ControllerTest {
56 Controller controller;
57 protected static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
59 static final File TEST_DIR = Files.createTempDir();
62 * Writes the necessary file for the tests in the temporary directory
64 static File stageTestResource(String name) throws IOException {
65 File file = new File(TEST_DIR, name);
66 byte[] bytes = toByteArray(ControllerTest.class.getResourceAsStream(name));
71 class MockDriverService extends DriverServiceAdapter {
72 static final int NO_SUCH_DRIVER_ID = 1;
73 static final int ITEM_NOT_FOUND_DRIVER_ID = 2;
74 static final int DRIVER_EXISTS_ID = 3;
76 static final String BASE_DRIVER_NAME = "of:000000000000000";
78 static final String NO_SUCH_DRIVER = BASE_DRIVER_NAME
80 static final String ITEM_NOT_FOUND_DRIVER = BASE_DRIVER_NAME
81 + ITEM_NOT_FOUND_DRIVER_ID;
82 static final String DRIVER_EXISTS = BASE_DRIVER_NAME
86 public Driver getDriver(DeviceId deviceId) {
87 switch (deviceId.toString()) {
90 case ITEM_NOT_FOUND_DRIVER:
91 throw new ItemNotFoundException();
93 return new DriverAdapter();
95 throw new AssertionError();
101 * Creates and initializes a new controller.
104 public void setUp() {
105 controller = new Controller();
106 Dictionary<String, String> properties = new Hashtable<>();
107 properties.put("openflowPorts",
108 Integer.toString(TestTools.findAvailablePort(0)));
109 controller.setConfigParams(properties);
113 * Tests fetching a driver that does not exist.
116 public void switchInstanceNotFoundTest() {
117 controller.start(null, new MockDriverService());
118 OpenFlowSwitchDriver driver =
119 controller.getOFSwitchInstance(MockDriverService.NO_SUCH_DRIVER_ID,
122 assertThat(driver, nullValue());
127 * Tests fetching a driver that throws an ItemNotFoundException.
130 public void switchItemNotFoundTest() {
131 controller.start(null, new MockDriverService());
132 OFDescStatsReply stats =
133 new OFDescStatsReplyAdapter();
134 OpenFlowSwitchDriver driver =
135 controller.getOFSwitchInstance(MockDriverService.ITEM_NOT_FOUND_DRIVER_ID,
138 assertThat(driver, nullValue());
143 * Tests fetching a driver that throws an ItemNotFoundException.
146 public void driverExistsTest() {
147 controller.start(null, new MockDriverService());
148 OFDescStatsReply stats =
149 new OFDescStatsReplyAdapter();
150 OpenFlowSwitchDriver driver =
151 controller.getOFSwitchInstance(MockDriverService.DRIVER_EXISTS_ID,
154 assertThat(driver, notNullValue());
159 * Tests configuring the controller.
162 public void testConfiguration() {
163 Dictionary<String, String> properties = new Hashtable<>();
164 properties.put("openflowPorts", "1,2,3,4,5");
165 properties.put("workerThreads", "5");
167 controller.setConfigParams(properties);
168 IntStream.rangeClosed(1, 5)
169 .forEach(i -> assertThat(controller.openFlowPorts, hasItem(i)));
170 assertThat(controller.workerThreads, is(5));
174 * Tests the SSL/TLS methods in the controller.
177 public void testSsl() throws IOException {
178 File keystore = stageTestResource("ControllerTestKeystore.jks");
179 String keystoreName = keystore.getAbsolutePath();
181 System.setProperty("enableOFTLS", Boolean.toString(Boolean.TRUE));
182 System.setProperty("javax.net.ssl.keyStore", keystoreName);
183 System.setProperty("javax.net.ssl.trustStore", keystoreName);
184 System.setProperty("javax.net.ssl.keyStorePassword", "password");
185 System.setProperty("javax.net.ssl.trustStorePassword", "password");
186 Dictionary<String, String> properties = new Hashtable<>();
187 properties.put("openflowPorts",
188 Integer.toString(TestTools.findAvailablePort(0)));
189 properties.put("workerThreads", "0");
191 controller.setConfigParams(properties);
192 controller.start(null, new MockDriverService());
194 assertThat(controller.serverSSLEngine, notNullValue());
197 boolean removed = keystore.delete();
199 log.warn("Could not remove temporary file");
204 * Tests controll utility health methods.
207 public void testHealth() {
208 Map<String, Long> memory = controller.getMemory();
209 assertThat(memory.size(), is(2));
210 assertThat(memory.get("total"), is(not(0)));
211 assertThat(memory.get("free"), is(not(0)));
213 long startTime = controller.getSystemStartTime();
214 assertThat(startTime, lessThan(System.currentTimeMillis()));
216 long upTime = controller.getSystemUptime();
217 assertThat(upTime, lessThan(30L * 1000));