2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.openflow.controller.impl;
18 import java.io.IOException;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
24 import org.onosproject.openflow.controller.RoleState;
25 import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
26 import org.onosproject.openflow.controller.driver.RoleRecvStatus;
27 import org.onosproject.openflow.controller.driver.RoleReplyInfo;
28 import org.onosproject.openflow.controller.driver.SwitchStateException;
29 import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
30 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
31 import org.projectfloodlight.openflow.types.U64;
33 import static org.junit.Assert.assertEquals;
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;
39 public class RoleManagerTest {
41 private static final U64 GID = U64.of(10L);
42 private static final long XID = 1L;
44 private OpenFlowSwitchDriver sw;
45 private RoleManager manager;
49 sw = new TestSwitchDriver();
50 manager = new RoleManager(sw);
54 public void tearDown() {
60 public void deliverRoleReply() {
61 RoleRecvStatus status;
63 RoleReplyInfo asserted = new RoleReplyInfo(MASTER, GID, XID);
64 RoleReplyInfo unasserted = new RoleReplyInfo(SLAVE, GID, XID);
67 //call without sendRoleReq() for requestPending = false
68 //first, sw.role == null
69 status = manager.deliverRoleReply(asserted);
70 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
73 assertEquals("expectation wrong", OTHER_EXPECTATION, status);
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);
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);
86 } catch (IOException | SwitchStateException e) {
87 assertEquals("unexpected error thrown",
88 SwitchStateException.class, e.getClass());
92 private class TestSwitchDriver extends OpenflowSwitchDriverAdapter {
94 RoleState failed = null;
95 RoleState current = null;
98 public void setRole(RoleState role) {
103 public RoleState getRole() {
108 public void setFeaturesReply(OFFeaturesReply featuresReply) {
112 public void setSwitchDescription(OFDescStatsReply desc) {
116 public int getNextTransactionId() {
121 public void returnRoleReply(RoleState requested, RoleState response) {
126 public String channelId() {