4b5943830ce1971e3daf49413bd75e8a4d4ed817
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
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.IOException;
19
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;
32
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;
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
92     private class TestSwitchDriver extends OpenflowSwitchDriverAdapter {
93
94         RoleState failed = null;
95         RoleState current = null;
96
97         @Override
98         public void setRole(RoleState role) {
99             current = role;
100         }
101
102         @Override
103         public RoleState getRole() {
104             return current;
105         }
106
107         @Override
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
121         public void returnRoleReply(RoleState requested, RoleState response) {
122             failed = requested;
123         }
124
125         @Override
126         public String channelId() {
127             return "1.2.3.4:1";
128         }
129     }
130 }