blob: dddea32812c049599449206208e404f2e8e825be [file] [log] [blame]
Ray Milkey7f9e0202015-11-04 17:14:09 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.openflow.controller.impl;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.Dictionary;
21import java.util.Hashtable;
22import java.util.Map;
23import java.util.stream.IntStream;
24
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.junit.TestTools;
28import org.onlab.util.ItemNotFoundException;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.driver.Driver;
31import org.onosproject.openflow.DriverAdapter;
32import org.onosproject.openflow.DriverServiceAdapter;
33import org.onosproject.openflow.OFDescStatsReplyAdapter;
34import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
35import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import com.google.common.io.Files;
40
41import static com.google.common.io.ByteStreams.toByteArray;
42import static com.google.common.io.Files.write;
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.Matchers.hasItem;
45import static org.hamcrest.Matchers.is;
46import static org.hamcrest.Matchers.lessThan;
47import static org.hamcrest.Matchers.not;
48import static org.hamcrest.Matchers.notNullValue;
49import static org.hamcrest.Matchers.nullValue;
50
51/**
52 * Unit tests for the OpenFlow controller class.
53 */
54public class ControllerTest {
55
56 Controller controller;
57 protected static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
58
59 static final File TEST_DIR = Files.createTempDir();
60
61 /*
62 * Writes the necessary file for the tests in the temporary directory
63 */
64 static File stageTestResource(String name) throws IOException {
65 File file = new File(TEST_DIR, name);
66 byte[] bytes = toByteArray(ControllerTest.class.getResourceAsStream(name));
67 write(bytes, file);
68 return file;
69 }
70
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;
75
76 static final String BASE_DRIVER_NAME = "of:000000000000000";
77
78 static final String NO_SUCH_DRIVER = BASE_DRIVER_NAME
79 + NO_SUCH_DRIVER_ID;
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
83 + DRIVER_EXISTS_ID;
84
85 @Override
86 public Driver getDriver(DeviceId deviceId) {
87 switch (deviceId.toString()) {
88 case NO_SUCH_DRIVER:
89 return null;
90 case ITEM_NOT_FOUND_DRIVER:
91 throw new ItemNotFoundException();
92 case DRIVER_EXISTS:
93 return new DriverAdapter();
94 default:
95 throw new AssertionError();
96 }
97 }
98 }
99
100 /**
101 * Creates and initializes a new controller.
102 */
103 @Before
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);
110 }
111
112 /**
113 * Tests fetching a driver that does not exist.
114 */
115 @Test
116 public void switchInstanceNotFoundTest() {
117 controller.start(null, new MockDriverService());
118 OpenFlowSwitchDriver driver =
119 controller.getOFSwitchInstance(MockDriverService.NO_SUCH_DRIVER_ID,
120 null,
121 null);
122 assertThat(driver, nullValue());
123 controller.stop();
124 }
125
126 /**
127 * Tests fetching a driver that throws an ItemNotFoundException.
128 */
129 @Test
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,
136 stats,
137 null);
138 assertThat(driver, nullValue());
139 controller.stop();
140 }
141
142 /**
143 * Tests fetching a driver that throws an ItemNotFoundException.
144 */
145 @Test
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,
152 stats,
153 null);
154 assertThat(driver, notNullValue());
155 controller.stop();
156 }
157
158 /**
159 * Tests configuring the controller.
160 */
161 @Test
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");
166
167 controller.setConfigParams(properties);
168 IntStream.rangeClosed(1, 5)
169 .forEach(i -> assertThat(controller.openFlowPorts, hasItem(i)));
170 assertThat(controller.workerThreads, is(5));
171 }
172
173 /**
174 * Tests the SSL/TLS methods in the controller.
175 */
176 @Test
177 public void testSsl() throws IOException {
178 File keystore = stageTestResource("ControllerTestKeystore.jks");
179 String keystoreName = keystore.getAbsolutePath();
180
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");
190
191 controller.setConfigParams(properties);
192 controller.start(null, new MockDriverService());
193
Jonathan Hartb35540a2015-11-17 09:30:56 -0800194 assertThat(controller.serverSslEngine, notNullValue());
Ray Milkey7f9e0202015-11-04 17:14:09 -0800195
196 controller.stop();
197 boolean removed = keystore.delete();
198 if (!removed) {
199 log.warn("Could not remove temporary file");
200 }
201 }
202
203 /**
204 * Tests controll utility health methods.
205 */
206 @Test
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)));
212
213 long startTime = controller.getSystemStartTime();
214 assertThat(startTime, lessThan(System.currentTimeMillis()));
215
216 long upTime = controller.getSystemUptime();
217 assertThat(upTime, lessThan(30L * 1000));
218 }
219}