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 | |
| 18 | import io.netty.channel.Channel; |
| 19 | import org.onosproject.net.Device; |
| 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; |
| 26 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | import java.util.concurrent.ConcurrentHashMap; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.projectfloodlight.openflow.protocol.OFControllerRole.*; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of OF switch. |
| 37 | */ |
| 38 | public final class DefaultOFSwitch implements OFSwitch { |
| 39 | |
| 40 | private static final String ERR_CH_DUPLICATE = "Channel already exists: "; |
| 41 | private static final String ERR_CH_NOT_FOUND = "Channel not found: "; |
| 42 | |
| 43 | private final Device device; |
| 44 | private final OFSwitchCapabilities capabilities; |
| 45 | |
| 46 | private final ConcurrentHashMap<Channel, OFControllerRole> controllerRoleMap |
| 47 | = new ConcurrentHashMap<>(); |
| 48 | |
| 49 | private DefaultOFSwitch(Device device, OFSwitchCapabilities capabilities) { |
| 50 | this.device = device; |
| 51 | this.capabilities = capabilities; |
| 52 | } |
| 53 | |
| 54 | // TODO add builder |
| 55 | |
| 56 | @Override |
| 57 | public Device device() { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public OFSwitchCapabilities capabilities() { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isConnected() { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void started() { |
| 73 | // TODO do some initial setups |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void stopped() { |
| 78 | // TODO implement |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void addControllerChannel(Channel channel) { |
| 83 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 84 | final String error = ERR_CH_DUPLICATE + channel.remoteAddress(); |
| 85 | checkArgument(existing == null, error); |
| 86 | return ROLE_EQUAL; |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void deleteControllerChannel(Channel channel) { |
| 92 | if (controllerRoleMap.remove(channel) == null) { |
| 93 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 94 | throw new IllegalStateException(error); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void setRole(Channel channel, OFControllerRole role) { |
| 100 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 101 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 102 | checkNotNull(existing, error); |
| 103 | return role; |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public OFControllerRole role(Channel channel) { |
| 109 | OFControllerRole role = controllerRoleMap.get(channel); |
| 110 | if (role == null) { |
| 111 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 112 | throw new IllegalStateException(error); |
| 113 | } |
| 114 | return role; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Set<Channel> controllerChannels() { |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void processPortAdded(Port port) { |
| 124 | // TODO generate FEATURES_REPLY message and send it to the controller |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void processPortDown(Port port) { |
| 129 | // TODO generate PORT_STATUS message and send it to the controller |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void processPortUp(Port port) { |
| 134 | // TODO generate PORT_STATUS message and send it to the controller |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void processFlowRemoved(FlowRule flowRule) { |
| 139 | // TODO generate FLOW_REMOVED message and send it to the controller |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void processPacketIn(InboundPacket packet) { |
| 144 | // TODO generate PACKET_IN message and send it to the controller |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void processControllerCommand(Channel channel, OFMessage msg) { |
| 149 | // TODO process controller command |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void processStatsRequest(Channel channel, OFMessage msg) { |
| 154 | // TODO process request and send reply |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void processRoleRequest(Channel channel, OFMessage msg) { |
| 159 | // TODO process role request and send reply |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void processFeaturesRequest(Channel channel, OFMessage msg) { |
| 164 | // TODO process features request and send reply |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void processLldp(Channel channel, OFMessage msg) { |
| 169 | // TODO process lldp |
| 170 | } |
| 171 | } |