blob: 6be06a5840b5c83c9e046ecb45b48bc7cca1b16f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.proxyarp;
alshabibc274c902014-10-03 14:58:27 -070017
alshabibc274c902014-10-03 14:58:27 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabibc274c902014-10-03 14:58:27 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080025import org.onlab.packet.Ethernet;
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090026import org.onlab.packet.ICMP6;
alshabib78baaf22015-02-18 18:55:45 -080027import org.onlab.packet.IPv6;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070028import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080034import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.packet.PacketProcessor;
36import org.onosproject.net.packet.PacketService;
37import org.onosproject.net.proxyarp.ProxyArpService;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080038import org.osgi.service.component.ComponentContext;
alshabibc274c902014-10-03 14:58:27 -070039import org.slf4j.Logger;
40
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070041import java.util.Dictionary;
42
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080043import static com.google.common.base.Strings.isNullOrEmpty;
alshabib78baaf22015-02-18 18:55:45 -080044import static org.slf4j.LoggerFactory.getLogger;
45
alshabibc274c902014-10-03 14:58:27 -070046/**
47 * Sample reactive proxy arp application.
48 */
49@Component(immediate = true)
50public class ProxyArp {
51
alshabibc274c902014-10-03 14:58:27 -070052 private final Logger log = getLogger(getClass());
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected PacketService packetService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected ProxyArpService proxyArpService;
59
60 private ProxyArpProcessor processor = new ProxyArpProcessor();
61
alshabib92c65ad2014-10-08 21:56:05 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected CoreService coreService;
64
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected ComponentConfigService cfgService;
67
alshabibc274c902014-10-03 14:58:27 -070068 private ApplicationId appId;
69
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080070 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070071 label = "Enable IPv6 Neighbor Discovery; default is false")
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080072 private boolean ipv6NeighborDiscovery = false;
73
alshabibc274c902014-10-03 14:58:27 -070074 @Activate
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080075 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070076 cfgService.registerProperties(getClass());
Brian O'Connorabafb502014-12-02 22:26:20 -080077 appId = coreService.registerApplication("org.onosproject.proxyarp");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080078 readComponentConfiguration(context);
79
alshabibc274c902014-10-03 14:58:27 -070080 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080081
82 TrafficSelector.Builder selectorBuilder =
83 DefaultTrafficSelector.builder();
84 selectorBuilder.matchEthType(Ethernet.TYPE_ARP);
85 packetService.requestPackets(selectorBuilder.build(),
86 PacketPriority.CONTROL, appId);
87
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080088 if (ipv6NeighborDiscovery) {
89 // IPv6 Neighbor Solicitation packet.
90 selectorBuilder = DefaultTrafficSelector.builder();
91 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
92 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
93 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
94 packetService.requestPackets(selectorBuilder.build(),
95 PacketPriority.CONTROL, appId);
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090096
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080097 // IPv6 Neighbor Advertisement packet.
98 selectorBuilder = DefaultTrafficSelector.builder();
99 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
100 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
101 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
102 packetService.requestPackets(selectorBuilder.build(),
103 PacketPriority.CONTROL, appId);
104 }
Kunihiro Ishiguro93224452015-02-13 00:40:08 +0900105
alshabibc274c902014-10-03 14:58:27 -0700106 log.info("Started with Application ID {}", appId.id());
107 }
108
109 @Deactivate
110 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700111 cfgService.unregisterProperties(getClass(), false);
alshabibc274c902014-10-03 14:58:27 -0700112 packetService.removeProcessor(processor);
113 processor = null;
114 log.info("Stopped");
115 }
116
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800117 @Modified
118 public void modified(ComponentContext context) {
119 readComponentConfiguration(context);
120 }
121
122 /**
123 * Extracts properties from the component configuration context.
124 *
125 * @param context the component context
126 */
127 private void readComponentConfiguration(ComponentContext context) {
128 Dictionary<?, ?> properties = context.getProperties();
129 Boolean flag;
130
131 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
132 if (flag == null) {
133 log.info("IPv6 Neighbor Discovery is not configured, " +
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700134 "using current value of {}", ipv6NeighborDiscovery);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800135 } else {
136 ipv6NeighborDiscovery = flag;
137 log.info("Configured. IPv6 Neighbor Discovery is {}",
138 ipv6NeighborDiscovery ? "enabled" : "disabled");
139 }
140 }
141
142 /**
143 * Check property name is defined and set to true.
144 *
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700145 * @param properties properties to be looked up
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800146 * @param propertyName the name of the property to look up
147 * @return value when the propertyName is defined or return null
148 */
149 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
150 String propertyName) {
151 Boolean value = null;
152 try {
153 String s = (String) properties.get(propertyName);
154 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
155 } catch (ClassCastException e) {
156 // No propertyName defined.
157 value = null;
158 }
159 return value;
160 }
alshabibc274c902014-10-03 14:58:27 -0700161
162 /**
163 * Packet processor responsible for forwarding packets along their paths.
164 */
165 private class ProxyArpProcessor implements PacketProcessor {
166
167 @Override
168 public void process(PacketContext context) {
169 // Stop processing if the packet has been handled, since we
170 // can't do any more to it.
171 if (context.isHandled()) {
172 return;
173 }
174
175 //handle the arp packet.
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800176 proxyArpService.handlePacket(context);
alshabibc274c902014-10-03 14:58:27 -0700177 }
178 }
179}
180
181