yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl.provider; |
| 18 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 30 | import org.onosproject.incubator.net.virtual.TenantId; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 34 | import org.onosproject.incubator.net.virtual.VirtualPacketContext; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 36 | import org.onosproject.incubator.net.virtual.provider.AbstractVirtualProvider; |
| 37 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProvider; |
| 38 | import org.onosproject.incubator.net.virtual.provider.VirtualPacketProviderService; |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 39 | import org.onosproject.incubator.net.virtual.provider.VirtualProviderRegistryService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.flow.instructions.Instruction; |
| 46 | import org.onosproject.net.flow.instructions.Instructions; |
| 47 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 48 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 49 | import org.onosproject.net.packet.InboundPacket; |
| 50 | import org.onosproject.net.packet.OutboundPacket; |
| 51 | import org.onosproject.net.packet.PacketContext; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 52 | import org.onosproject.net.packet.PacketPriority; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 53 | import org.onosproject.net.packet.PacketProcessor; |
| 54 | import org.onosproject.net.packet.PacketService; |
| 55 | import org.onosproject.net.provider.ProviderId; |
| 56 | import org.osgi.service.component.ComponentContext; |
| 57 | import org.slf4j.Logger; |
| 58 | |
| 59 | import java.nio.ByteBuffer; |
| 60 | import java.util.Dictionary; |
| 61 | import java.util.HashSet; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 62 | import java.util.Optional; |
| 63 | import java.util.Set; |
| 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static org.slf4j.LoggerFactory.getLogger; |
| 67 | |
| 68 | @Component(immediate = true) |
| 69 | public class DefaultVirtualPacketProvider extends AbstractVirtualProvider |
| 70 | implements VirtualPacketProvider { |
| 71 | |
| 72 | private static final int PACKET_PROCESSOR_PRIORITY = 1; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 73 | private static final PacketPriority VIRTUAL_PACKET_PRIORITY = PacketPriority.REACTIVE; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 78 | protected PacketService packetService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected CoreService coreService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 84 | protected VirtualNetworkAdminService vnaService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 85 | |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected VirtualProviderRegistryService providerRegistryService; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 88 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 89 | private ApplicationId appId; |
| 90 | private InternalPacketProcessor processor; |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 91 | |
| 92 | private Set<NetworkId> requestsSet = Sets.newHashSet(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Creates a provider with the supplied identifier. |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 96 | */ |
| 97 | public DefaultVirtualPacketProvider() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 98 | super(new ProviderId("virtual-packet", "org.onosproject.virtual.virtual-packet")); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Activate |
| 102 | public void activate() { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 103 | appId = coreService.registerApplication("org.onosproject.virtual.virtual-packet"); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 104 | providerRegistryService.registerProvider(this); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 105 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 106 | log.info("Started"); |
| 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
Jonathan Hart | 6602867 | 2017-02-02 16:25:49 -0800 | [diff] [blame] | 111 | if (processor != null) { |
| 112 | packetService.removeProcessor(processor); |
| 113 | } |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 114 | providerRegistryService.unregisterProvider(this); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 115 | log.info("Stopped"); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Modified |
| 119 | protected void modified(ComponentContext context) { |
| 120 | Dictionary<?, ?> properties = context.getProperties(); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | @Override |
| 125 | public void emit(NetworkId networkId, OutboundPacket packet) { |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 126 | devirtualize(networkId, packet) |
| 127 | .forEach(outboundPacket -> packetService.emit(outboundPacket)); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 128 | } |
| 129 | |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 130 | @Override |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 131 | public void startPacketHandling(NetworkId networkId) { |
| 132 | requestsSet.add(networkId); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 133 | if (processor == null) { |
| 134 | processor = new InternalPacketProcessor(); |
| 135 | packetService.addProcessor(processor, PACKET_PROCESSOR_PRIORITY); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void stopPacketHandling(NetworkId networkId) { |
| 141 | requestsSet.remove(networkId); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 142 | if (requestsSet.isEmpty()) { |
| 143 | packetService.removeProcessor(processor); |
| 144 | processor = null; |
| 145 | } |
yoonseon | fb4a1db | 2017-01-31 11:38:30 -0800 | [diff] [blame] | 146 | } |
| 147 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 148 | /** |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 149 | * Send the outbound packet of a virtual context. |
| 150 | * This method is designed to support Context's send() method that invoked |
| 151 | * by applications. |
| 152 | * See {@link org.onosproject.net.packet.PacketContext} |
| 153 | * |
| 154 | * @param virtualPacketContext virtual packet context |
| 155 | */ |
| 156 | protected void send(VirtualPacketContext virtualPacketContext) { |
| 157 | devirtualizeContext(virtualPacketContext) |
| 158 | .forEach(outboundPacket -> packetService.emit(outboundPacket)); |
| 159 | } |
| 160 | |
| 161 | /** |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 162 | * Translate the requested physical PacketContext into a virtual PacketContext. |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 163 | * See {@link org.onosproject.net.packet.PacketContext} |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 164 | * |
| 165 | * @param context A physical PacketContext be translated |
| 166 | * @return A translated virtual PacketContext |
| 167 | */ |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 168 | private VirtualPacketContext virtualize(PacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 169 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 170 | VirtualPort vPort = getMappedVirtualPort(context.inPacket().receivedFrom()); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 171 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 172 | if (vPort != null) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 173 | ConnectPoint cp = new ConnectPoint(vPort.element().id(), |
| 174 | vPort.number()); |
| 175 | |
| 176 | Ethernet eth = context.inPacket().parsed(); |
| 177 | eth.setVlanID(Ethernet.VLAN_UNTAGGED); |
| 178 | |
| 179 | InboundPacket inPacket = |
| 180 | new DefaultInboundPacket(cp, eth, |
| 181 | ByteBuffer.wrap(eth.serialize())); |
| 182 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 183 | DefaultOutboundPacket outPkt = |
| 184 | new DefaultOutboundPacket(cp.deviceId(), |
| 185 | DefaultTrafficTreatment.builder().build(), |
| 186 | ByteBuffer.wrap(eth.serialize())); |
| 187 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 188 | VirtualPacketContext vContext = |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 189 | new DefaultVirtualPacketContext(context.time(), inPacket, outPkt, |
| 190 | context.isHandled(), vPort.networkId(), |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 191 | this); |
| 192 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 193 | return vContext; |
| 194 | } else { |
| 195 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Find the corresponding virtual port with the physical port. |
| 202 | * |
| 203 | * @param cp the connect point for the physical network |
| 204 | * @return a virtual port |
| 205 | */ |
| 206 | private VirtualPort getMappedVirtualPort(ConnectPoint cp) { |
| 207 | Set<TenantId> tIds = vnaService.getTenantIds(); |
| 208 | |
| 209 | Set<VirtualNetwork> vNetworks = new HashSet<>(); |
| 210 | tIds.forEach(tid -> vNetworks.addAll(vnaService.getVirtualNetworks(tid))); |
| 211 | |
| 212 | for (VirtualNetwork vNet : vNetworks) { |
| 213 | Set<VirtualDevice> vDevices = vnaService.getVirtualDevices(vNet.id()); |
| 214 | |
| 215 | Set<VirtualPort> vPorts = new HashSet<>(); |
| 216 | vDevices.forEach(dev -> vPorts |
| 217 | .addAll(vnaService.getVirtualPorts(dev.networkId(), dev.id()))); |
| 218 | |
| 219 | VirtualPort vPort = vPorts.stream() |
| 220 | .filter(vp -> vp.realizedBy().equals(cp)) |
| 221 | .findFirst().orElse(null); |
| 222 | |
| 223 | if (vPort != null) { |
| 224 | return vPort; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return null; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 232 | * Translate the requested virtual outbound packet into |
| 233 | * a set of physical OutboundPacket. |
| 234 | * See {@link org.onosproject.net.packet.OutboundPacket} |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 235 | * |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 236 | * @param packet an OutboundPacket to be translated |
| 237 | * @return a set of de-virtualized (physical) OutboundPacket |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 238 | */ |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 239 | private Set<OutboundPacket> devirtualize(NetworkId networkId, OutboundPacket packet) { |
| 240 | Set<OutboundPacket> outboundPackets = new HashSet<>(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 241 | Set<VirtualPort> vPorts = vnaService |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 242 | .getVirtualPorts(networkId, packet.sendThrough()); |
| 243 | |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 244 | TrafficTreatment.Builder commonTreatmentBuilder |
| 245 | = DefaultTrafficTreatment.builder(); |
| 246 | packet.treatment().allInstructions().stream() |
| 247 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 248 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 249 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 250 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 251 | PortNumber vOutPortNum = packet.treatment().allInstructions().stream() |
| 252 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 253 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 254 | .findFirst().get(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 255 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 256 | if (!vOutPortNum.isLogical()) { |
| 257 | Optional<ConnectPoint> optionalCpOut = vPorts.stream() |
| 258 | .filter(v -> v.number().equals(vOutPortNum)) |
| 259 | .map(v -> v.realizedBy()) |
| 260 | .findFirst(); |
| 261 | if (!optionalCpOut.isPresent()) { |
| 262 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
| 263 | vOutPortNum, networkId, packet.sendThrough()); |
| 264 | return outboundPackets; |
| 265 | } |
| 266 | ConnectPoint egressPoint = optionalCpOut.get(); |
| 267 | |
| 268 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 269 | .builder(commonTreatment) |
| 270 | .setOutput(egressPoint.port()).build(); |
| 271 | |
| 272 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 273 | egressPoint.deviceId(), treatment, packet.data()); |
| 274 | outboundPackets.add(outboundPacket); |
| 275 | } else { |
| 276 | if (vOutPortNum == PortNumber.FLOOD) { |
| 277 | for (VirtualPort outPort : vPorts) { |
| 278 | ConnectPoint cpOut = outPort.realizedBy(); |
| 279 | if (cpOut != null) { |
| 280 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 281 | .builder(commonTreatment) |
| 282 | .setOutput(cpOut.port()).build(); |
| 283 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 284 | cpOut.deviceId(), treatment, packet.data()); |
| 285 | outboundPackets.add(outboundPacket); |
| 286 | } else { |
| 287 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
| 288 | outPort.number(), networkId, packet.sendThrough()); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return outboundPackets; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 298 | * Translate the requested virtual packet context into |
| 299 | * a set of physical outbound packets. |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 300 | * |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 301 | * @param context A handled virtual packet context |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 302 | */ |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 303 | private Set<OutboundPacket> devirtualizeContext(VirtualPacketContext context) { |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 304 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 305 | Set<OutboundPacket> outboundPackets = new HashSet<>(); |
| 306 | |
| 307 | NetworkId networkId = context.networkId(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 308 | TrafficTreatment vTreatment = context.treatmentBuilder().build(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 309 | DeviceId sendThrough = context.outPacket().sendThrough(); |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 310 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 311 | Set<VirtualPort> vPorts = vnaService |
| 312 | .getVirtualPorts(networkId, sendThrough); |
| 313 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 314 | PortNumber vOutPortNum = vTreatment.allInstructions().stream() |
| 315 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 316 | .map(i -> ((Instructions.OutputInstruction) i).port()) |
| 317 | .findFirst().get(); |
| 318 | |
| 319 | TrafficTreatment.Builder commonTreatmentBuilder |
| 320 | = DefaultTrafficTreatment.builder(); |
| 321 | vTreatment.allInstructions().stream() |
| 322 | .filter(i -> i.type() != Instruction.Type.OUTPUT) |
| 323 | .forEach(i -> commonTreatmentBuilder.add(i)); |
| 324 | TrafficTreatment commonTreatment = commonTreatmentBuilder.build(); |
| 325 | |
| 326 | if (!vOutPortNum.isLogical()) { |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 327 | Optional<ConnectPoint> optionalCpOut = vPorts.stream() |
| 328 | .filter(v -> v.number().equals(vOutPortNum)) |
| 329 | .map(v -> v.realizedBy()) |
| 330 | .findFirst(); |
| 331 | if (!optionalCpOut.isPresent()) { |
| 332 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
| 333 | vOutPortNum, networkId, sendThrough); |
| 334 | return outboundPackets; |
| 335 | } |
| 336 | ConnectPoint egressPoint = optionalCpOut.get(); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 337 | |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 338 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 339 | .builder(commonTreatment) |
| 340 | .setOutput(egressPoint.port()).build(); |
| 341 | |
| 342 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 343 | egressPoint.deviceId(), treatment, context.outPacket().data()); |
| 344 | outboundPackets.add(outboundPacket); |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 345 | } else { |
| 346 | if (vOutPortNum == PortNumber.FLOOD) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 347 | Set<VirtualPort> outPorts = vPorts.stream() |
Yoonseon Han | 5cf483a | 2017-04-19 23:14:00 -0700 | [diff] [blame] | 348 | .filter(vp -> !vp.number().isLogical()) |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 349 | .filter(vp -> vp.number() != |
| 350 | context.inPacket().receivedFrom().port()) |
| 351 | .collect(Collectors.toSet()); |
| 352 | |
| 353 | for (VirtualPort outPort : outPorts) { |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 354 | ConnectPoint cpOut = outPort.realizedBy(); |
| 355 | if (cpOut != null) { |
| 356 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 357 | .builder(commonTreatment) |
| 358 | .setOutput(cpOut.port()).build(); |
| 359 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 360 | cpOut.deviceId(), treatment, context.outPacket().data()); |
| 361 | outboundPackets.add(outboundPacket); |
| 362 | } else { |
| 363 | log.warn("Port {} is not realized yet, in Network {}, Device {}", |
| 364 | outPort.number(), networkId, sendThrough); |
| 365 | } |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 366 | } |
| 367 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 368 | } |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 369 | return outboundPackets; |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private final class InternalPacketProcessor implements PacketProcessor { |
| 373 | |
| 374 | @Override |
| 375 | public void process(PacketContext context) { |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 376 | VirtualPacketContext vContexts = virtualize(context); |
yoonseon | 736a73b | 2017-01-26 14:27:48 -0800 | [diff] [blame] | 377 | |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 378 | if (vContexts == null) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | VirtualPacketProviderService service = |
| 383 | (VirtualPacketProviderService) providerRegistryService |
Harold Huang | bee92f6 | 2017-06-07 22:39:37 +0800 | [diff] [blame^] | 384 | .getProviderService(vContexts.networkId(), |
Yoonseon Han | c8089db | 2017-03-22 20:22:12 +0900 | [diff] [blame] | 385 | VirtualPacketProvider.class); |
| 386 | if (service != null) { |
| 387 | service.processPacket(vContexts); |
| 388 | } |
yoonseon | 5a51ef7 | 2016-10-26 14:50:09 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |