Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 23 | import org.onosproject.openflow.OpenflowSwitchDriverAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.openflow.controller.RoleState; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.openflow.controller.driver.RoleRecvStatus; |
| 27 | import org.onosproject.openflow.controller.driver.RoleReplyInfo; |
| 28 | import org.onosproject.openflow.controller.driver.SwitchStateException; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.types.U64; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 34 | import static org.onosproject.openflow.controller.RoleState.MASTER; |
| 35 | import static org.onosproject.openflow.controller.RoleState.SLAVE; |
| 36 | import static org.onosproject.openflow.controller.driver.RoleRecvStatus.MATCHED_CURRENT_ROLE; |
| 37 | import static org.onosproject.openflow.controller.driver.RoleRecvStatus.OTHER_EXPECTATION; |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 38 | |
| 39 | public class RoleManagerTest { |
| 40 | |
| 41 | private static final U64 GID = U64.of(10L); |
| 42 | private static final long XID = 1L; |
| 43 | |
| 44 | private OpenFlowSwitchDriver sw; |
| 45 | private RoleManager manager; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() { |
| 49 | sw = new TestSwitchDriver(); |
| 50 | manager = new RoleManager(sw); |
| 51 | } |
| 52 | |
| 53 | @After |
| 54 | public void tearDown() { |
| 55 | manager = null; |
| 56 | sw = null; |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void deliverRoleReply() { |
| 61 | RoleRecvStatus status; |
| 62 | |
| 63 | RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID); |
| 64 | RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID); |
| 65 | |
| 66 | try { |
| 67 | //call without sendRoleReq() for requestPending = false |
| 68 | //first, sw.role == null |
| 69 | status = manager.deliverRoleReply(asserted); |
| 70 | assertEquals("expectation wrong", OTHER_EXPECTATION, status); |
| 71 | |
| 72 | sw.setRole(MASTER); |
| 73 | assertEquals("expectation wrong", OTHER_EXPECTATION, status); |
| 74 | sw.setRole(SLAVE); |
| 75 | |
| 76 | //match to pendingRole = MASTER, requestPending = true |
| 77 | manager.sendRoleRequest(MASTER, MATCHED_CURRENT_ROLE); |
| 78 | status = manager.deliverRoleReply(asserted); |
| 79 | assertEquals("expectation wrong", MATCHED_CURRENT_ROLE, status); |
| 80 | |
| 81 | //requestPending never gets reset -- this might be a bug. |
| 82 | status = manager.deliverRoleReply(unasserted); |
| 83 | assertEquals("expectation wrong", OTHER_EXPECTATION, status); |
| 84 | assertEquals("pending role mismatch", MASTER, ((TestSwitchDriver) sw).failed); |
| 85 | |
| 86 | } catch (IOException | SwitchStateException e) { |
| 87 | assertEquals("unexpected error thrown", |
| 88 | SwitchStateException.class, e.getClass()); |
| 89 | } |
| 90 | } |
| 91 | |
Ray Milkey | 7f9e020 | 2015-11-04 17:14:09 -0800 | [diff] [blame] | 92 | private class TestSwitchDriver extends OpenflowSwitchDriverAdapter { |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 93 | |
| 94 | RoleState failed = null; |
| 95 | RoleState current = null; |
| 96 | |
| 97 | @Override |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 98 | public void setRole(RoleState role) { |
| 99 | current = role; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public RoleState getRole() { |
| 104 | return current; |
| 105 | } |
| 106 | |
| 107 | @Override |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 108 | public void setFeaturesReply(OFFeaturesReply featuresReply) { |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void setSwitchDescription(OFDescStatsReply desc) { |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int getNextTransactionId() { |
| 117 | return (int) XID; |
| 118 | } |
| 119 | |
| 120 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 121 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 122 | failed = requested; |
| 123 | } |
| 124 | |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 125 | @Override |
| 126 | public String channelId() { |
| 127 | return "1.2.3.4:1"; |
| 128 | } |
Ayaka Koshibe | ee1c467 | 2014-09-25 12:31:52 -0700 | [diff] [blame] | 129 | } |
| 130 | } |