2 * Copyright 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.driver.handshaker;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet;
20 import org.onosproject.net.Device;
21 import org.onosproject.openflow.controller.OpenFlowOpticalSwitch;
22 import org.onosproject.openflow.controller.PortDescPropertyType;
23 import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
24 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted;
25 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted;
26 import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted;
27 import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsRequest;
28 import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry;
29 import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsReply;
30 import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsRequest;
31 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
32 import org.projectfloodlight.openflow.protocol.OFMessage;
33 import org.projectfloodlight.openflow.protocol.OFObject;
34 import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
35 import org.projectfloodlight.openflow.protocol.OFStatsRequest;
36 import org.projectfloodlight.openflow.protocol.OFType;
37 import org.projectfloodlight.openflow.types.OFPort;
38 import org.projectfloodlight.openflow.types.TableId;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
44 import java.util.concurrent.atomic.AtomicBoolean;
46 public class CalientFiberSwitchHandshaker extends AbstractOpenFlowSwitch implements OpenFlowOpticalSwitch {
48 private final AtomicBoolean driverHandshakeComplete = new AtomicBoolean(false);
49 private List<OFCalientPortDescStatsEntry> fiberPorts = new ArrayList<>();
53 public Boolean supportNxRole() {
58 public void startDriverHandshake() {
59 log.warn("Starting driver handshake for sw {}", getStringId());
60 if (startDriverHandshakeCalled) {
61 throw new SwitchDriverSubHandshakeAlreadyStarted();
63 startDriverHandshakeCalled = true;
65 sendHandshakeOFExperimenterPortDescRequest();
66 } catch (IOException e) {
67 log.error("Exception while sending experimenter port desc:", e.getMessage());
73 private void sendHandshakeOFExperimenterPortDescRequest() throws IOException {
74 // send multi part message for port description for optical switches
75 OFCalientPortDescStatsRequest portsRequest = factory()
76 .buildCalientPortDescStatsRequest()
78 log.warn("Sending experimenter port description message {}",
79 portsRequest.toString());
80 this.sendHandshakeMessage(portsRequest);
84 public boolean isDriverHandshakeComplete() {
85 return driverHandshakeComplete.get();
89 public void processDriverHandshakeMessage(OFMessage m) {
90 if (!startDriverHandshakeCalled) {
91 throw new SwitchDriverSubHandshakeNotStarted();
93 if (driverHandshakeComplete.get()) {
94 throw new SwitchDriverSubHandshakeCompleted(m);
97 switch (m.getType()) {
101 log.error("Switch Error {} {}", getStringId(), m);
107 case GET_ASYNC_REPLY:
113 case QUEUE_GET_CONFIG_REPLY:
118 log.warn("Received port desc reply");
119 OFCalientPortDescStatsReply descStatsReply = (OFCalientPortDescStatsReply) m;
120 fiberPorts.addAll(descStatsReply.getPortDesc());
121 // Multi-part message
122 if (!descStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
123 driverHandshakeComplete.set(true);
127 log.warn("Received message {} during switch-driver " +
128 "subhandshake " + "from switch {} ... " +
129 "Ignoring message", m,
136 public Device.Type deviceType() {
137 return Device.Type.FIBER_SWITCH;
141 public List<? extends OFObject> getPortsOf(PortDescPropertyType type) {
142 return ImmutableList.copyOf(fiberPorts);
146 public Set<PortDescPropertyType> getPortTypes() {
147 return ImmutableSet.of(PortDescPropertyType.OPTICAL_TRANSPORT);
151 public final void sendMsg(OFMessage m) {
152 OFMessage newMsg = m;
154 if (m.getType() == OFType.STATS_REQUEST) {
155 OFStatsRequest sr = (OFStatsRequest) m;
156 log.debug("Rebuilding stats request type {}", sr.getStatsType());
157 switch (sr.getStatsType()) {
159 OFCalientFlowStatsRequest request = this.factory().buildCalientFlowStatsRequest()
160 .setCookie(((OFFlowStatsRequest) sr).getCookie())
161 .setCookieMask(((OFFlowStatsRequest) sr).getCookieMask())
162 .setMatch(this.factory().matchWildcardAll())
163 .setOutGroup(((OFFlowStatsRequest) sr).getOutGroup().getGroupNumber())
164 .setOutPort(OFPort.ANY)
165 .setTableId(TableId.ALL)
167 .setFlags(sr.getFlags())
179 super.sendMsg(newMsg);