blob: 80fc95f3166a15bea4a64b3cdc847935d2c59699 [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
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037//TODO: Knock-off this class as we don't need any switch/app specific
38//drivers in the south bound layers.
sangho11c30ac2015-01-22 14:30:55 -080039public class OFSwitchImplSpringOpenTTP extends AbstractOpenFlowSwitch {
40
sangho87af8112015-01-29 12:53:08 -080041 private OFFactory factory;
42
43 private final AtomicBoolean driverHandshakeComplete;
44 private AtomicBoolean haltStateMachine;
45
sangho87af8112015-01-29 12:53:08 -080046 /* Default table ID - compatible with CpqD switch */
47 private static final int TABLE_VLAN = 0;
48 private static final int TABLE_TMAC = 1;
49 private static final int TABLE_IPV4_UNICAST = 2;
50 private static final int TABLE_MPLS = 3;
51 private static final int TABLE_ACL = 5;
52
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070053 /*
54 * Set the default values. These variables will get overwritten based on the
55 * switch vendor type
sangho87af8112015-01-29 12:53:08 -080056 */
57 protected int vlanTableId = TABLE_VLAN;
58 protected int tmacTableId = TABLE_TMAC;
59 protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST;
60 protected int mplsTableId = TABLE_MPLS;
61 protected int aclTableId = TABLE_ACL;
62
sangho87af8112015-01-29 12:53:08 -080063 protected OFSwitchImplSpringOpenTTP(Dpid dpid, OFDescStatsReply desc) {
64 super(dpid);
65 driverHandshakeComplete = new AtomicBoolean(false);
66 haltStateMachine = new AtomicBoolean(false);
sangho87af8112015-01-29 12:53:08 -080067 setSwitchDescription(desc);
sangho11c30ac2015-01-22 14:30:55 -080068 }
69
sangho11c30ac2015-01-22 14:30:55 -080070 @Override
sangho87af8112015-01-29 12:53:08 -080071 public String toString() {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070072 return "OFSwitchImplSpringOpenTTP ["
73 + ((channel != null) ? channel.getRemoteAddress() : "?")
74 + " DPID["
75 + ((this.getStringId() != null) ? this.getStringId() : "?")
76 + "]]";
sangho11c30ac2015-01-22 14:30:55 -080077 }
78
79 @Override
80 public Boolean supportNxRole() {
81 return null;
82 }
83
84 @Override
85 public void startDriverHandshake() {
sangho87af8112015-01-29 12:53:08 -080086 log.debug("Starting driver handshake for sw {}", getStringId());
87 if (startDriverHandshakeCalled) {
88 throw new SwitchDriverSubHandshakeAlreadyStarted();
89 }
90 startDriverHandshakeCalled = true;
91 factory = this.factory();
sangho11c30ac2015-01-22 14:30:55 -080092
sanghob35a6192015-04-01 13:05:26 -070093 driverHandshakeComplete.set(true);
94 log.debug("Driver handshake is complete");
95
sangho11c30ac2015-01-22 14:30:55 -080096 }
97
98 @Override
99 public boolean isDriverHandshakeComplete() {
sangho87af8112015-01-29 12:53:08 -0800100 if (!startDriverHandshakeCalled) {
101 throw new SwitchDriverSubHandshakeNotStarted();
102 }
103 return driverHandshakeComplete.get();
sangho11c30ac2015-01-22 14:30:55 -0800104 }
105
106 @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
sangho87af8112015-01-29 12:53:08 -0800116 @Override
117 public void write(OFMessage msg) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700118 channel.write(Collections.singletonList(msg));
sangho87af8112015-01-29 12:53:08 -0800119 }
120
121 @Override
122 public void write(List<OFMessage> msgs) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700123 channel.write(msgs);
sangho87af8112015-01-29 12:53:08 -0800124 }
125
126 @Override
sanghob35a6192015-04-01 13:05:26 -0700127 public void transformAndSendMsg(OFMessage msg, TableType type) {
128 if (msg.getType() == OFType.FLOW_MOD) {
129 OFFlowMod flowMod = (OFFlowMod) msg;
sangho87af8112015-01-29 12:53:08 -0800130 OFFlowMod.Builder builder = flowMod.createBuilder();
sanghob35a6192015-04-01 13:05:26 -0700131 List<OFInstruction> instructions = flowMod.getInstructions();
132 List<OFInstruction> newInstructions = Lists.newArrayList();
133 for (OFInstruction i : instructions) {
134 if (i instanceof OFInstructionGotoTable) {
135 OFInstructionGotoTable gotoTable = (OFInstructionGotoTable) i;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700136 TableType tid = TableType.values()[gotoTable.getTableId()
137 .getValue()];
138 newInstructions.add(gotoTable.createBuilder()
139 .setTableId(getTableId(tid)).build());
sanghob35a6192015-04-01 13:05:26 -0700140 } else {
141 newInstructions.add(i);
sangho87af8112015-01-29 12:53:08 -0800142 }
sangho87af8112015-01-29 12:53:08 -0800143 }
sanghob35a6192015-04-01 13:05:26 -0700144 builder.setTableId(getTableId(type));
145 builder.setInstructions(newInstructions);
146 OFMessage msgnew = builder.build();
147 channel.write(Collections.singletonList(msgnew));
148 log.trace("Installed {}", msgnew);
149
150 } else {
151 channel.write(Collections.singletonList(msg));
sangho87af8112015-01-29 12:53:08 -0800152 }
sangho87af8112015-01-29 12:53:08 -0800153 }
154
sanghob35a6192015-04-01 13:05:26 -0700155 @Override
156 public TableType getTableType(TableId tid) {
157 switch (tid.getValue()) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700158 case TABLE_IPV4_UNICAST:
159 return TableType.IP;
160 case TABLE_MPLS:
161 return TableType.MPLS;
162 case TABLE_ACL:
163 return TableType.ACL;
164 case TABLE_VLAN:
165 return TableType.VLAN;
166 case TABLE_TMAC:
167 return TableType.ETHER;
168 default:
169 log.error("Table type for Table id {} is not supported in the driver",
170 tid);
171 return TableType.NONE;
sanghob35a6192015-04-01 13:05:26 -0700172 }
sangho87af8112015-01-29 12:53:08 -0800173 }
174
175 private TableId getTableId(TableType tableType) {
176 switch (tableType) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700177 case IP:
178 return TableId.of(ipv4UnicastTableId);
179 case MPLS:
180 return TableId.of(mplsTableId);
181 case ACL:
182 return TableId.of(aclTableId);
183 case VLAN:
184 return TableId.of(vlanTableId);
185 case ETHER:
186 return TableId.of(tmacTableId);
187 default: {
188 log.error("Table type {} is not supported in the driver", tableType);
189 return TableId.NONE;
190 }
sangho87af8112015-01-29 12:53:08 -0800191 }
192 }
193
Ray Milkey51365f32015-02-04 11:24:22 -0800194}