blob: 4e7f61d5c59854276483bf722ea1dc3b42d57e4d [file] [log] [blame]
sangho11c30ac2015-01-22 14:30:55 -08001/*
2 * Copyright 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 */
16package org.onosproject.openflow.drivers;
17
sanghob35a6192015-04-01 13:05:26 -070018import com.google.common.collect.Lists;
sangho11c30ac2015-01-22 14:30:55 -080019import org.onosproject.openflow.controller.Dpid;
20import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch;
sangho87af8112015-01-29 12:53:08 -080021import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted;
22import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted;
23import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted;
sangho87af8112015-01-29 12:53:08 -080024import org.projectfloodlight.openflow.protocol.OFFlowMod;
25import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
26import org.projectfloodlight.openflow.protocol.OFFactory;
sangho11c30ac2015-01-22 14:30:55 -080027import org.projectfloodlight.openflow.protocol.OFMessage;
sangho87af8112015-01-29 12:53:08 -080028import org.projectfloodlight.openflow.protocol.OFType;
sangho87af8112015-01-29 12:53:08 -080029import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
sanghob35a6192015-04-01 13:05:26 -070030import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
sangho87af8112015-01-29 12:53:08 -080031import org.projectfloodlight.openflow.types.TableId;
sangho11c30ac2015-01-22 14:30:55 -080032
sangho87af8112015-01-29 12:53:08 -080033import java.util.Collections;
sangho11c30ac2015-01-22 14:30:55 -080034import java.util.List;
sangho87af8112015-01-29 12:53:08 -080035import java.util.concurrent.atomic.AtomicBoolean;
sangho11c30ac2015-01-22 14:30:55 -080036
sangho11c30ac2015-01-22 14:30:55 -080037public class OFSwitchImplSpringOpenTTP extends AbstractOpenFlowSwitch {
38
sangho87af8112015-01-29 12:53:08 -080039 private OFFactory factory;
40
41 private final AtomicBoolean driverHandshakeComplete;
42 private AtomicBoolean haltStateMachine;
43
sangho87af8112015-01-29 12:53:08 -080044 /* Default table ID - compatible with CpqD switch */
45 private static final int TABLE_VLAN = 0;
46 private static final int TABLE_TMAC = 1;
47 private static final int TABLE_IPV4_UNICAST = 2;
48 private static final int TABLE_MPLS = 3;
49 private static final int TABLE_ACL = 5;
50
sangho87af8112015-01-29 12:53:08 -080051 /* Set the default values. These variables will get
52 * overwritten based on the switch vendor type
53 */
54 protected int vlanTableId = TABLE_VLAN;
55 protected int tmacTableId = TABLE_TMAC;
56 protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST;
57 protected int mplsTableId = TABLE_MPLS;
58 protected int aclTableId = TABLE_ACL;
59
sangho87af8112015-01-29 12:53:08 -080060 protected OFSwitchImplSpringOpenTTP(Dpid dpid, OFDescStatsReply desc) {
61 super(dpid);
62 driverHandshakeComplete = new AtomicBoolean(false);
63 haltStateMachine = new AtomicBoolean(false);
sangho87af8112015-01-29 12:53:08 -080064 setSwitchDescription(desc);
sangho11c30ac2015-01-22 14:30:55 -080065 }
66
sangho11c30ac2015-01-22 14:30:55 -080067
68 @Override
sangho87af8112015-01-29 12:53:08 -080069 public String toString() {
70 return "OFSwitchImplSpringOpenTTP [" + ((channel != null)
71 ? channel.getRemoteAddress() : "?")
72 + " DPID[" + ((this.getStringId() != null) ?
73 this.getStringId() : "?") + "]]";
sangho11c30ac2015-01-22 14:30:55 -080074 }
75
76 @Override
77 public Boolean supportNxRole() {
78 return null;
79 }
80
sanghob35a6192015-04-01 13:05:26 -070081
sangho11c30ac2015-01-22 14:30:55 -080082 @Override
83 public void startDriverHandshake() {
sangho87af8112015-01-29 12:53:08 -080084 log.debug("Starting driver handshake for sw {}", getStringId());
85 if (startDriverHandshakeCalled) {
86 throw new SwitchDriverSubHandshakeAlreadyStarted();
87 }
88 startDriverHandshakeCalled = true;
89 factory = this.factory();
sangho11c30ac2015-01-22 14:30:55 -080090
sanghob35a6192015-04-01 13:05:26 -070091 driverHandshakeComplete.set(true);
92 log.debug("Driver handshake is complete");
93
sangho11c30ac2015-01-22 14:30:55 -080094 }
95
96 @Override
97 public boolean isDriverHandshakeComplete() {
sangho87af8112015-01-29 12:53:08 -080098 if (!startDriverHandshakeCalled) {
99 throw new SwitchDriverSubHandshakeNotStarted();
100 }
101 return driverHandshakeComplete.get();
sangho11c30ac2015-01-22 14:30:55 -0800102 }
103
sanghob35a6192015-04-01 13:05:26 -0700104
105
sangho11c30ac2015-01-22 14:30:55 -0800106 @Override
107 public void processDriverHandshakeMessage(OFMessage m) {
sangho87af8112015-01-29 12:53:08 -0800108 if (!startDriverHandshakeCalled) {
109 throw new SwitchDriverSubHandshakeNotStarted();
110 }
111 if (driverHandshakeComplete.get()) {
112 throw new SwitchDriverSubHandshakeCompleted(m);
113 }
sangho11c30ac2015-01-22 14:30:55 -0800114 }
115
sanghob35a6192015-04-01 13:05:26 -0700116
sangho87af8112015-01-29 12:53:08 -0800117 @Override
118 public void write(OFMessage msg) {
119 this.channel.write(Collections.singletonList(msg));
120 }
121
122 @Override
123 public void write(List<OFMessage> msgs) {
124 this.channel.write(msgs);
125 }
126
127 @Override
sanghob35a6192015-04-01 13:05:26 -0700128 public void transformAndSendMsg(OFMessage msg, TableType type) {
129 if (msg.getType() == OFType.FLOW_MOD) {
130 OFFlowMod flowMod = (OFFlowMod) msg;
sangho87af8112015-01-29 12:53:08 -0800131 OFFlowMod.Builder builder = flowMod.createBuilder();
sanghob35a6192015-04-01 13:05:26 -0700132 List<OFInstruction> instructions = flowMod.getInstructions();
133 List<OFInstruction> newInstructions = Lists.newArrayList();
134 for (OFInstruction i : instructions) {
135 if (i instanceof OFInstructionGotoTable) {
136 OFInstructionGotoTable gotoTable = (OFInstructionGotoTable) i;
137 TableType tid = TableType.values()[gotoTable.getTableId().getValue()];
138 newInstructions.add(
139 gotoTable.createBuilder()
140 .setTableId(getTableId(tid)).build());
141 } else {
142 newInstructions.add(i);
sangho87af8112015-01-29 12:53:08 -0800143 }
sangho87af8112015-01-29 12:53:08 -0800144 }
sanghob35a6192015-04-01 13:05:26 -0700145 builder.setTableId(getTableId(type));
146 builder.setInstructions(newInstructions);
147 OFMessage msgnew = builder.build();
148 channel.write(Collections.singletonList(msgnew));
149 log.trace("Installed {}", msgnew);
150
151 } else {
152 channel.write(Collections.singletonList(msg));
sangho87af8112015-01-29 12:53:08 -0800153 }
sangho87af8112015-01-29 12:53:08 -0800154 }
155
sanghob35a6192015-04-01 13:05:26 -0700156 @Override
157 public TableType getTableType(TableId tid) {
158 switch (tid.getValue()) {
159 case TABLE_IPV4_UNICAST:
160 return TableType.IP;
161 case TABLE_MPLS:
162 return TableType.MPLS;
163 case TABLE_ACL:
164 return TableType.ACL;
165 case TABLE_VLAN:
166 return TableType.VLAN;
167 case TABLE_TMAC:
168 return TableType.ETHER;
169 default:
170 log.error("Table type for Table id {} is not supported in the driver", tid);
171 return TableType.NONE;
172 }
sangho87af8112015-01-29 12:53:08 -0800173 }
174
175 private TableId getTableId(TableType tableType) {
176 switch (tableType) {
177 case IP:
178 return TableId.of(ipv4UnicastTableId);
179 case MPLS:
180 return TableId.of(mplsTableId);
181 case ACL:
182 return TableId.of(aclTableId);
sanghob35a6192015-04-01 13:05:26 -0700183 case VLAN:
184 return TableId.of(vlanTableId);
185 case ETHER:
186 return TableId.of(tmacTableId);
sangho87af8112015-01-29 12:53:08 -0800187 default: {
188 log.error("Table type {} is not supported in the driver", tableType);
189 return TableId.NONE;
190 }
191 }
192 }
193
Ray Milkey51365f32015-02-04 11:24:22 -0800194}