Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.proxyarp; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 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.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 31 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketProcessor; |
| 33 | import org.onosproject.net.packet.PacketService; |
| 34 | import org.onosproject.net.proxyarp.ProxyArpService; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | /** |
| 38 | * Sample reactive proxy arp application. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class ProxyArp { |
| 42 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected PacketService packetService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected ProxyArpService proxyArpService; |
| 50 | |
| 51 | private ProxyArpProcessor processor = new ProxyArpProcessor(); |
| 52 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected CoreService coreService; |
| 55 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 56 | private ApplicationId appId; |
| 57 | |
| 58 | @Activate |
| 59 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | appId = coreService.registerApplication("org.onosproject.proxyarp"); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 61 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 62 | |
| 63 | TrafficSelector.Builder selectorBuilder = |
| 64 | DefaultTrafficSelector.builder(); |
| 65 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
| 66 | packetService.requestPackets(selectorBuilder.build(), |
| 67 | PacketPriority.CONTROL, appId); |
| 68 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 69 | log.info("Started with Application ID {}", appId.id()); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | packetService.removeProcessor(processor); |
| 75 | processor = null; |
| 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Packet processor responsible for forwarding packets along their paths. |
| 82 | */ |
| 83 | private class ProxyArpProcessor implements PacketProcessor { |
| 84 | |
| 85 | @Override |
| 86 | public void process(PacketContext context) { |
| 87 | // Stop processing if the packet has been handled, since we |
| 88 | // can't do any more to it. |
| 89 | if (context.isHandled()) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | //handle the arp packet. |
| 94 | proxyArpService.handleArp(context); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |