blob: 87a8f5a1555f1af6844e8e59a9a1ea80fa1cd0ef [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.openflow.controller.impl;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070017
Ray Milkey7f9e0202015-11-04 17:14:09 -080018import java.io.IOException;
19
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Ray Milkey7f9e0202015-11-04 17:14:09 -080023import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.openflow.controller.RoleState;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.openflow.controller.driver.RoleRecvStatus;
27import org.onosproject.openflow.controller.driver.RoleReplyInfo;
28import org.onosproject.openflow.controller.driver.SwitchStateException;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070029import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070030import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070031import org.projectfloodlight.openflow.types.U64;
32
33import static org.junit.Assert.assertEquals;
alshabibb452fd72015-04-22 20:46:20 -070034import static org.onosproject.openflow.controller.RoleState.MASTER;
35import static org.onosproject.openflow.controller.RoleState.SLAVE;
36import static org.onosproject.openflow.controller.driver.RoleRecvStatus.MATCHED_CURRENT_ROLE;
37import static org.onosproject.openflow.controller.driver.RoleRecvStatus.OTHER_EXPECTATION;
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070038
39public 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 Milkey7f9e0202015-11-04 17:14:09 -080092 private class TestSwitchDriver extends OpenflowSwitchDriverAdapter {
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070093
94 RoleState failed = null;
95 RoleState current = null;
96
97 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -070098 public void setRole(RoleState role) {
99 current = role;
100 }
101
102 @Override
103 public RoleState getRole() {
104 return current;
105 }
106
107 @Override
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700108 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 Koshibe3ef2b0d2014-10-31 13:58:27 -0700121 public void returnRoleReply(RoleState requested, RoleState response) {
122 failed = requested;
123 }
124
Ray Milkeye53f1712015-01-16 09:17:16 -0800125 @Override
126 public String channelId() {
127 return "1.2.3.4:1";
128 }
Ayaka Koshibeee1c4672014-09-25 12:31:52 -0700129 }
130}