alshabib | 3b554cf | 2014-08-18 11:19:50 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
tom | 9ccd781 | 2014-08-25 22:43:19 -0700 | [diff] [blame^] | 18 | package org.onlab.onos.of.controller.impl.internal; |
alshabib | 3b554cf | 2014-08-18 11:19:50 -0500 | [diff] [blame] | 19 | |
| 20 | import junit.framework.TestCase; |
tom | 9ccd781 | 2014-08-25 22:43:19 -0700 | [diff] [blame^] | 21 | import org.onlab.onos.of.controller.impl.IOFSwitch; |
alshabib | 3b554cf | 2014-08-18 11:19:50 -0500 | [diff] [blame] | 22 | |
| 23 | import org.easymock.EasyMock; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | |
| 28 | |
| 29 | public class ControllerTest extends TestCase { |
| 30 | |
| 31 | private Controller controller; |
| 32 | private IOFSwitch sw; |
| 33 | private OFChannelHandler h; |
| 34 | |
| 35 | @Override |
| 36 | @Before |
| 37 | public void setUp() throws Exception { |
| 38 | super.setUp(); |
| 39 | sw = EasyMock.createMock(IOFSwitch.class); |
| 40 | h = EasyMock.createMock(OFChannelHandler.class); |
| 41 | controller = new Controller(); |
| 42 | ControllerRunThread t = new ControllerRunThread(); |
| 43 | t.start(); |
| 44 | /* |
| 45 | * Making sure the thread is properly started before making calls |
| 46 | * to controller class. |
| 47 | */ |
| 48 | Thread.sleep(200); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Starts the base mocks used in these tests. |
| 53 | */ |
| 54 | private void startMocks() { |
| 55 | EasyMock.replay(sw, h); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Reset the mocks to a known state. |
| 60 | * Automatically called after tests. |
| 61 | */ |
| 62 | @After |
| 63 | private void resetMocks() { |
| 64 | EasyMock.reset(sw); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Fetches the controller instance. |
| 69 | * @return the controller |
| 70 | */ |
| 71 | public Controller getController() { |
| 72 | return controller; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Run the controller's main loop so that updates are processed. |
| 77 | */ |
| 78 | protected class ControllerRunThread extends Thread { |
| 79 | @Override |
| 80 | public void run() { |
| 81 | controller.openFlowPort = 0; // Don't listen |
| 82 | controller.activate(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Verify that we are able to add a switch that just connected. |
| 88 | * If it already exists then this should fail |
| 89 | * |
| 90 | * @throws Exception error |
| 91 | */ |
| 92 | @Test |
| 93 | public void testAddConnectedSwitches() throws Exception { |
| 94 | startMocks(); |
| 95 | assertTrue(controller.addConnectedSwitch(0, h)); |
| 96 | assertFalse(controller.addConnectedSwitch(0, h)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add active master but cannot re-add active master. |
| 101 | * @throws Exception an error occurred. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testAddActivatedMasterSwitch() throws Exception { |
| 105 | startMocks(); |
| 106 | controller.addConnectedSwitch(0, h); |
| 107 | assertTrue(controller.addActivatedMasterSwitch(0, sw)); |
| 108 | assertFalse(controller.addActivatedMasterSwitch(0, sw)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests that an activated switch can be added but cannot be re-added. |
| 113 | * |
| 114 | * @throws Exception an error occurred |
| 115 | */ |
| 116 | @Test |
| 117 | public void testAddActivatedEqualSwitch() throws Exception { |
| 118 | startMocks(); |
| 119 | controller.addConnectedSwitch(0, h); |
| 120 | assertTrue(controller.addActivatedEqualSwitch(0, sw)); |
| 121 | assertFalse(controller.addActivatedEqualSwitch(0, sw)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Move an equal switch to master. |
| 126 | * @throws Exception an error occurred |
| 127 | */ |
| 128 | @Test |
| 129 | public void testTranstitionToMaster() throws Exception { |
| 130 | startMocks(); |
| 131 | controller.addConnectedSwitch(0, h); |
| 132 | controller.addActivatedEqualSwitch(0, sw); |
| 133 | controller.transitionToMasterSwitch(0); |
| 134 | assertNotNull(controller.getMasterSwitch(0)); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Transition a master switch to equal state. |
| 139 | * @throws Exception an error occurred |
| 140 | */ |
| 141 | @Test |
| 142 | public void testTranstitionToEqual() throws Exception { |
| 143 | startMocks(); |
| 144 | controller.addConnectedSwitch(0, h); |
| 145 | controller.addActivatedMasterSwitch(0, sw); |
| 146 | controller.transitionToEqualSwitch(0); |
| 147 | assertNotNull(controller.getEqualSwitch(0)); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Remove the switch from the controller instance. |
| 152 | * @throws Exception an error occurred |
| 153 | */ |
| 154 | @Test |
| 155 | public void testRemoveSwitch() throws Exception { |
| 156 | sw.cancelAllStatisticsReplies(); |
| 157 | EasyMock.expectLastCall().once(); |
| 158 | sw.setConnected(false); |
| 159 | EasyMock.expectLastCall().once(); |
| 160 | startMocks(); |
| 161 | controller.addConnectedSwitch(0, h); |
| 162 | controller.addActivatedMasterSwitch(0, sw); |
| 163 | controller.removeConnectedSwitch(0); |
| 164 | assertNull(controller.getSwitch(0)); |
| 165 | EasyMock.verify(sw, h); |
| 166 | } |
| 167 | } |