Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 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.net.packet.impl; |
| 18 | |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.net.packet.OutboundPacket; |
| 23 | import org.onosproject.net.packet.PacketProgrammable; |
| 24 | import org.onosproject.net.packet.PacketProvider; |
| 25 | import org.onosproject.net.provider.AbstractProvider; |
| 26 | import org.onosproject.net.provider.ProviderId; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * Driver-based packet rule provider. |
| 32 | */ |
| 33 | public class PacketDriverProvider extends AbstractProvider implements PacketProvider { |
| 34 | |
| 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
| 37 | // To be extracted for reuse as we deal with other. |
| 38 | private static final String SCHEME = "default"; |
| 39 | private static final String PROVIDER_NAME = "org.onosproject.provider"; |
| 40 | protected DeviceService deviceService; |
| 41 | |
| 42 | public PacketDriverProvider() { |
| 43 | super(new ProviderId(SCHEME, PROVIDER_NAME)); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Initializes the provider with the necessary device service. |
| 48 | * |
| 49 | * @param deviceService device service |
| 50 | */ |
| 51 | void init(DeviceService deviceService) { |
| 52 | this.deviceService = deviceService; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void emit(OutboundPacket packet) { |
| 57 | PacketProgrammable programmable = getPacketProgrammable(packet.sendThrough()); |
| 58 | if (programmable != null) { |
| 59 | programmable.emit(packet); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private PacketProgrammable getPacketProgrammable(DeviceId deviceId) { |
Ray Milkey | 2c142c5 | 2017-11-16 16:46:04 -0800 | [diff] [blame] | 64 | if (deviceService == null) { |
| 65 | log.debug("Packet encountered but device service is not ready, dropping"); |
| 66 | return null; |
| 67 | } |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 68 | Device device = deviceService.getDevice(deviceId); |
| 69 | if (device.is(PacketProgrammable.class)) { |
| 70 | return device.as(PacketProgrammable.class); |
| 71 | } else { |
Andrea Campanella | fe1d473 | 2016-02-08 16:45:27 -0800 | [diff] [blame] | 72 | log.debug("Device {} is not packet programmable", deviceId); |
Andrea Campanella | e427390 | 2016-02-08 13:43:30 -0800 | [diff] [blame] | 73 | return null; |
Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 74 | } |
Andrea Campanella | 5b24053 | 2016-02-05 09:44:41 -0800 | [diff] [blame] | 75 | } |
| 76 | } |