Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 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 | package org.onosproject.openflow.controller.impl; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.util.Dictionary; |
| 21 | import java.util.Hashtable; |
| 22 | import java.util.Map; |
| 23 | import java.util.stream.IntStream; |
| 24 | |
| 25 | import org.junit.Before; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 26 | import org.junit.ClassRule; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 28 | import org.junit.rules.TemporaryFolder; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 29 | import org.onlab.junit.TestTools; |
| 30 | import org.onlab.util.ItemNotFoundException; |
| 31 | import org.onosproject.net.DeviceId; |
Ray Milkey | d931a9b | 2016-03-02 17:01:30 -0800 | [diff] [blame] | 32 | import org.onosproject.net.driver.Behaviour; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 33 | import org.onosproject.net.driver.Driver; |
Ray Milkey | d931a9b | 2016-03-02 17:01:30 -0800 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverAdapter; |
| 35 | import org.onosproject.net.driver.DriverHandler; |
| 36 | import org.onosproject.net.driver.DriverServiceAdapter; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 37 | import org.onosproject.openflow.OFDescStatsReplyAdapter; |
Ray Milkey | d931a9b | 2016-03-02 17:01:30 -0800 | [diff] [blame] | 38 | import org.onosproject.openflow.OpenflowSwitchDriverAdapter; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 39 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
| 40 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 44 | import static com.google.common.io.ByteStreams.toByteArray; |
| 45 | import static com.google.common.io.Files.write; |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.hamcrest.Matchers.hasItem; |
| 48 | import static org.hamcrest.Matchers.is; |
| 49 | import static org.hamcrest.Matchers.lessThan; |
| 50 | import static org.hamcrest.Matchers.not; |
| 51 | import static org.hamcrest.Matchers.notNullValue; |
| 52 | import static org.hamcrest.Matchers.nullValue; |
| 53 | |
| 54 | /** |
| 55 | * Unit tests for the OpenFlow controller class. |
| 56 | */ |
| 57 | public class ControllerTest { |
| 58 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 59 | @ClassRule |
| 60 | public static TemporaryFolder testFolder = new TemporaryFolder(); |
| 61 | |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 62 | Controller controller; |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory.getLogger(ControllerTest.class); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 64 | |
Ray Milkey | d931a9b | 2016-03-02 17:01:30 -0800 | [diff] [blame] | 65 | 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 Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 76 | /* |
| 77 | * Writes the necessary file for the tests in the temporary directory |
| 78 | */ |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 79 | private static File stageTestResource(String name) throws IOException { |
| 80 | File file = new File(testFolder.newFolder(), name); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 81 | 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 Milkey | d931a9b | 2016-03-02 17:01:30 -0800 | [diff] [blame] | 108 | return new TestDriver(); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 109 | 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() { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 132 | controller.start(null, new MockDriverService(), null); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 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() { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 146 | controller.start(null, new MockDriverService(), null); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 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() { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 162 | controller.start(null, new MockDriverService(), null); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 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); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 207 | controller.start(null, new MockDriverService(), null); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 208 | |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 209 | assertThat(controller.sslContext, notNullValue()); |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 210 | |
| 211 | controller.stop(); |
| 212 | boolean removed = keystore.delete(); |
| 213 | if (!removed) { |
| 214 | log.warn("Could not remove temporary file"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
Frank Wang | d8ab096 | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 219 | * Tests control utility health methods. |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 220 | */ |
| 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 | } |