blob: b3bb8b1eacb2bda1b738de6665cdcdf390251698 [file] [log] [blame]
Ray Milkey7f9e0202015-11-04 17:14:09 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey7f9e0202015-11-04 17:14:09 -08003 *
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;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080026import org.junit.ClassRule;
Ray Milkey7f9e0202015-11-04 17:14:09 -080027import org.junit.Test;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080028import org.junit.rules.TemporaryFolder;
Ray Milkey7f9e0202015-11-04 17:14:09 -080029import org.onlab.junit.TestTools;
30import org.onlab.util.ItemNotFoundException;
31import org.onosproject.net.DeviceId;
Ray Milkeyd931a9b2016-03-02 17:01:30 -080032import org.onosproject.net.driver.Behaviour;
Ray Milkey7f9e0202015-11-04 17:14:09 -080033import org.onosproject.net.driver.Driver;
Ray Milkeyd931a9b2016-03-02 17:01:30 -080034import org.onosproject.net.driver.DriverAdapter;
35import org.onosproject.net.driver.DriverHandler;
36import org.onosproject.net.driver.DriverServiceAdapter;
Ray Milkey7f9e0202015-11-04 17:14:09 -080037import org.onosproject.openflow.OFDescStatsReplyAdapter;
Ray Milkeyd931a9b2016-03-02 17:01:30 -080038import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
Ray Milkey7f9e0202015-11-04 17:14:09 -080039import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
40import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
41import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Ray Milkey7f9e0202015-11-04 17:14:09 -080044import static com.google.common.io.ByteStreams.toByteArray;
45import static com.google.common.io.Files.write;
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.hamcrest.Matchers.hasItem;
48import static org.hamcrest.Matchers.is;
49import static org.hamcrest.Matchers.lessThan;
50import static org.hamcrest.Matchers.not;
51import static org.hamcrest.Matchers.notNullValue;
52import static org.hamcrest.Matchers.nullValue;
53
54/**
55 * Unit tests for the OpenFlow controller class.
56 */
57public class ControllerTest {
58
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080059 @ClassRule
60 public static TemporaryFolder testFolder = new TemporaryFolder();
61
Ray Milkey7f9e0202015-11-04 17:14:09 -080062 Controller controller;
63 protected static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
64
Ray Milkeyd931a9b2016-03-02 17:01:30 -080065 private class TestDriver extends DriverAdapter {
66 @SuppressWarnings("unchecked")
67 @Override
68 public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) {
69 if (behaviourClass == OpenFlowSwitchDriver.class) {
70 return (T) new OpenflowSwitchDriverAdapter();
71 }
72 return null;
73 }
74 }
75
Ray Milkey7f9e0202015-11-04 17:14:09 -080076 /*
77 * Writes the necessary file for the tests in the temporary directory
78 */
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080079 private static File stageTestResource(String name) throws IOException {
80 File file = new File(testFolder.newFolder(), name);
Ray Milkey7f9e0202015-11-04 17:14:09 -080081 byte[] bytes = toByteArray(ControllerTest.class.getResourceAsStream(name));
82 write(bytes, file);
83 return file;
84 }
85
86 class MockDriverService extends DriverServiceAdapter {
87 static final int NO_SUCH_DRIVER_ID = 1;
88 static final int ITEM_NOT_FOUND_DRIVER_ID = 2;
89 static final int DRIVER_EXISTS_ID = 3;
90
91 static final String BASE_DRIVER_NAME = "of:000000000000000";
92
93 static final String NO_SUCH_DRIVER = BASE_DRIVER_NAME
94 + NO_SUCH_DRIVER_ID;
95 static final String ITEM_NOT_FOUND_DRIVER = BASE_DRIVER_NAME
96 + ITEM_NOT_FOUND_DRIVER_ID;
97 static final String DRIVER_EXISTS = BASE_DRIVER_NAME
98 + DRIVER_EXISTS_ID;
99
100 @Override
101 public Driver getDriver(DeviceId deviceId) {
102 switch (deviceId.toString()) {
103 case NO_SUCH_DRIVER:
104 return null;
105 case ITEM_NOT_FOUND_DRIVER:
106 throw new ItemNotFoundException();
107 case DRIVER_EXISTS:
Ray Milkeyd931a9b2016-03-02 17:01:30 -0800108 return new TestDriver();
Ray Milkey7f9e0202015-11-04 17:14:09 -0800109 default:
110 throw new AssertionError();
111 }
112 }
113 }
114
115 /**
116 * Creates and initializes a new controller.
117 */
118 @Before
119 public void setUp() {
120 controller = new Controller();
121 Dictionary<String, String> properties = new Hashtable<>();
122 properties.put("openflowPorts",
123 Integer.toString(TestTools.findAvailablePort(0)));
124 controller.setConfigParams(properties);
125 }
126
127 /**
128 * Tests fetching a driver that does not exist.
129 */
130 @Test
131 public void switchInstanceNotFoundTest() {
132 controller.start(null, new MockDriverService());
133 OpenFlowSwitchDriver driver =
134 controller.getOFSwitchInstance(MockDriverService.NO_SUCH_DRIVER_ID,
135 null,
136 null);
137 assertThat(driver, nullValue());
138 controller.stop();
139 }
140
141 /**
142 * Tests fetching a driver that throws an ItemNotFoundException.
143 */
144 @Test
145 public void switchItemNotFoundTest() {
146 controller.start(null, new MockDriverService());
147 OFDescStatsReply stats =
148 new OFDescStatsReplyAdapter();
149 OpenFlowSwitchDriver driver =
150 controller.getOFSwitchInstance(MockDriverService.ITEM_NOT_FOUND_DRIVER_ID,
151 stats,
152 null);
153 assertThat(driver, nullValue());
154 controller.stop();
155 }
156
157 /**
158 * Tests fetching a driver that throws an ItemNotFoundException.
159 */
160 @Test
161 public void driverExistsTest() {
162 controller.start(null, new MockDriverService());
163 OFDescStatsReply stats =
164 new OFDescStatsReplyAdapter();
165 OpenFlowSwitchDriver driver =
166 controller.getOFSwitchInstance(MockDriverService.DRIVER_EXISTS_ID,
167 stats,
168 null);
169 assertThat(driver, notNullValue());
170 controller.stop();
171 }
172
173 /**
174 * Tests configuring the controller.
175 */
176 @Test
177 public void testConfiguration() {
178 Dictionary<String, String> properties = new Hashtable<>();
179 properties.put("openflowPorts", "1,2,3,4,5");
180 properties.put("workerThreads", "5");
181
182 controller.setConfigParams(properties);
183 IntStream.rangeClosed(1, 5)
184 .forEach(i -> assertThat(controller.openFlowPorts, hasItem(i)));
185 assertThat(controller.workerThreads, is(5));
186 }
187
188 /**
189 * Tests the SSL/TLS methods in the controller.
190 */
191 @Test
192 public void testSsl() throws IOException {
193 File keystore = stageTestResource("ControllerTestKeystore.jks");
194 String keystoreName = keystore.getAbsolutePath();
195
196 System.setProperty("enableOFTLS", Boolean.toString(Boolean.TRUE));
197 System.setProperty("javax.net.ssl.keyStore", keystoreName);
198 System.setProperty("javax.net.ssl.trustStore", keystoreName);
199 System.setProperty("javax.net.ssl.keyStorePassword", "password");
200 System.setProperty("javax.net.ssl.trustStorePassword", "password");
201 Dictionary<String, String> properties = new Hashtable<>();
202 properties.put("openflowPorts",
203 Integer.toString(TestTools.findAvailablePort(0)));
204 properties.put("workerThreads", "0");
205
206 controller.setConfigParams(properties);
207 controller.start(null, new MockDriverService());
208
alshabib5162a9d2015-12-07 17:49:37 -0800209 assertThat(controller.sslContext, notNullValue());
Ray Milkey7f9e0202015-11-04 17:14:09 -0800210
211 controller.stop();
212 boolean removed = keystore.delete();
213 if (!removed) {
214 log.warn("Could not remove temporary file");
215 }
216 }
217
218 /**
Frank Wang3a98e0a2017-08-11 11:09:30 +0800219 * Tests control utility health methods.
Ray Milkey7f9e0202015-11-04 17:14:09 -0800220 */
221 @Test
222 public void testHealth() {
223 Map<String, Long> memory = controller.getMemory();
224 assertThat(memory.size(), is(2));
225 assertThat(memory.get("total"), is(not(0)));
226 assertThat(memory.get("free"), is(not(0)));
227
228 long startTime = controller.getSystemStartTime();
229 assertThat(startTime, lessThan(System.currentTimeMillis()));
230
231 long upTime = controller.getSystemUptime();
232 assertThat(upTime, lessThan(30L * 1000));
233 }
234}