Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.ofagent.impl; |
| 17 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 19 | import io.netty.channel.Channel; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Port; |
| 21 | import org.onosproject.net.flow.FlowRule; |
| 22 | import org.onosproject.net.packet.InboundPacket; |
| 23 | import org.onosproject.ofagent.api.OFSwitch; |
| 24 | import org.onosproject.ofagent.api.OFSwitchCapabilities; |
| 25 | import org.projectfloodlight.openflow.protocol.OFControllerRole; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFEchoReply; |
| 27 | import org.projectfloodlight.openflow.protocol.OFEchoRequest; |
| 28 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 29 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 30 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
| 31 | import org.projectfloodlight.openflow.protocol.OFHello; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 34 | import org.projectfloodlight.openflow.types.DatapathId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 35 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 36 | import java.util.Collections; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | import java.util.concurrent.ConcurrentHashMap; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkArgument; |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | import static org.projectfloodlight.openflow.protocol.OFControllerRole.*; |
| 43 | |
| 44 | /** |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 45 | * Implementation of the default OpenFlow switch. |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 46 | */ |
| 47 | public final class DefaultOFSwitch implements OFSwitch { |
| 48 | |
| 49 | private static final String ERR_CH_DUPLICATE = "Channel already exists: "; |
| 50 | private static final String ERR_CH_NOT_FOUND = "Channel not found: "; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 51 | private static final long NUM_BUFFERS = 1024; |
| 52 | private static final short NUM_TABLES = 3; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 53 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 54 | private final DatapathId dpId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 55 | private final OFSwitchCapabilities capabilities; |
| 56 | |
| 57 | private final ConcurrentHashMap<Channel, OFControllerRole> controllerRoleMap |
| 58 | = new ConcurrentHashMap<>(); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 59 | private static final OFFactory FACTORY = OFFactories.getFactory(OFVersion.OF_13); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 60 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 61 | private int handshakeTransactionIds = -1; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 62 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 63 | private DefaultOFSwitch(DatapathId dpid, OFSwitchCapabilities capabilities) { |
| 64 | this.dpId = dpid; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 65 | this.capabilities = capabilities; |
| 66 | } |
| 67 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 68 | public static DefaultOFSwitch of(DatapathId dpid, OFSwitchCapabilities capabilities) { |
| 69 | checkNotNull(dpid, "DPID cannot be null"); |
| 70 | checkNotNull(capabilities, "OF capabilities cannot be null"); |
| 71 | return new DefaultOFSwitch(dpid, capabilities); |
| 72 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 73 | |
| 74 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 75 | public DatapathId dpid() { |
| 76 | return this.dpId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public OFSwitchCapabilities capabilities() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 81 | return this.capabilities; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void addControllerChannel(Channel channel) { |
| 86 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 87 | final String error = ERR_CH_DUPLICATE + channel.remoteAddress(); |
| 88 | checkArgument(existing == null, error); |
| 89 | return ROLE_EQUAL; |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void deleteControllerChannel(Channel channel) { |
| 95 | if (controllerRoleMap.remove(channel) == null) { |
| 96 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 97 | throw new IllegalStateException(error); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void setRole(Channel channel, OFControllerRole role) { |
| 103 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 104 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 105 | checkNotNull(existing, error); |
| 106 | return role; |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public OFControllerRole role(Channel channel) { |
| 112 | OFControllerRole role = controllerRoleMap.get(channel); |
| 113 | if (role == null) { |
| 114 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 115 | throw new IllegalStateException(error); |
| 116 | } |
| 117 | return role; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Set<Channel> controllerChannels() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 122 | return ImmutableSet.copyOf(controllerRoleMap.keySet()); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void processPortAdded(Port port) { |
| 127 | // TODO generate FEATURES_REPLY message and send it to the controller |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void processPortDown(Port port) { |
| 132 | // TODO generate PORT_STATUS message and send it to the controller |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void processPortUp(Port port) { |
| 137 | // TODO generate PORT_STATUS message and send it to the controller |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void processFlowRemoved(FlowRule flowRule) { |
| 142 | // TODO generate FLOW_REMOVED message and send it to the controller |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void processPacketIn(InboundPacket packet) { |
| 147 | // TODO generate PACKET_IN message and send it to the controller |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void processControllerCommand(Channel channel, OFMessage msg) { |
| 152 | // TODO process controller command |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void processStatsRequest(Channel channel, OFMessage msg) { |
| 157 | // TODO process request and send reply |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void processRoleRequest(Channel channel, OFMessage msg) { |
| 162 | // TODO process role request and send reply |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void processFeaturesRequest(Channel channel, OFMessage msg) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 167 | OFFeaturesReply ofFeaturesReply = FACTORY.buildFeaturesReply() |
| 168 | .setDatapathId(dpId) |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 169 | .setNBuffers(NUM_BUFFERS) |
| 170 | .setNTables(NUM_TABLES) |
| 171 | .setCapabilities(capabilities.ofSwitchCapabilities()) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 172 | .setXid(msg.getXid()) |
| 173 | .build(); |
| 174 | channel.writeAndFlush(Collections.singletonList(ofFeaturesReply)); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void processLldp(Channel channel, OFMessage msg) { |
| 179 | // TODO process lldp |
| 180 | } |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | public void sendOfHello(Channel channel) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 184 | OFHello ofHello = FACTORY.buildHello() |
| 185 | .setXid(this.handshakeTransactionIds--) |
| 186 | .build(); |
| 187 | channel.writeAndFlush(Collections.singletonList(ofHello)); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void processEchoRequest(Channel channel, OFMessage msg) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 192 | OFEchoReply ofEchoReply = FACTORY.buildEchoReply() |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 193 | .setXid(msg.getXid()) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 194 | .setData(((OFEchoRequest) msg).getData()) |
| 195 | .build(); |
| 196 | channel.writeAndFlush(Collections.singletonList(ofEchoReply)); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 197 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 198 | } |