blob: 82cdc31960b7d0b57d516102f186bf60b57acd6f [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
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080018import java.util.Dictionary;
19
alshabibc274c902014-10-03 14:58:27 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
alshabibc274c902014-10-03 14:58:27 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080027import org.onlab.packet.Ethernet;
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090028import org.onlab.packet.ICMP6;
alshabib78baaf22015-02-18 18:55:45 -080029import org.onlab.packet.IPv6;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080032import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080035import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.packet.PacketProcessor;
37import org.onosproject.net.packet.PacketService;
38import org.onosproject.net.proxyarp.ProxyArpService;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080039import org.osgi.service.component.ComponentContext;
alshabibc274c902014-10-03 14:58:27 -070040import org.slf4j.Logger;
41
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080042import static com.google.common.base.Strings.isNullOrEmpty;
alshabib78baaf22015-02-18 18:55:45 -080043import static org.slf4j.LoggerFactory.getLogger;
44
alshabibc274c902014-10-03 14:58:27 -070045/**
46 * Sample reactive proxy arp application.
47 */
48@Component(immediate = true)
49public class ProxyArp {
50
alshabibc274c902014-10-03 14:58:27 -070051 private final Logger log = getLogger(getClass());
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected PacketService packetService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected ProxyArpService proxyArpService;
58
59 private ProxyArpProcessor processor = new ProxyArpProcessor();
60
alshabib92c65ad2014-10-08 21:56:05 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected CoreService coreService;
63
alshabibc274c902014-10-03 14:58:27 -070064 private ApplicationId appId;
65
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080066 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
67 label = "Enable IPv6 Neighbor Discovery; default is false")
68 private boolean ipv6NeighborDiscovery = false;
69
alshabibc274c902014-10-03 14:58:27 -070070 @Activate
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080071 public void activate(ComponentContext context) {
Brian O'Connorabafb502014-12-02 22:26:20 -080072 appId = coreService.registerApplication("org.onosproject.proxyarp");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080073 readComponentConfiguration(context);
74
alshabibc274c902014-10-03 14:58:27 -070075 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080076
77 TrafficSelector.Builder selectorBuilder =
78 DefaultTrafficSelector.builder();
79 selectorBuilder.matchEthType(Ethernet.TYPE_ARP);
80 packetService.requestPackets(selectorBuilder.build(),
81 PacketPriority.CONTROL, appId);
82
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080083 if (ipv6NeighborDiscovery) {
84 // IPv6 Neighbor Solicitation packet.
85 selectorBuilder = DefaultTrafficSelector.builder();
86 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
87 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
88 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
89 packetService.requestPackets(selectorBuilder.build(),
90 PacketPriority.CONTROL, appId);
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090091
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080092 // IPv6 Neighbor Advertisement packet.
93 selectorBuilder = DefaultTrafficSelector.builder();
94 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
95 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
96 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
97 packetService.requestPackets(selectorBuilder.build(),
98 PacketPriority.CONTROL, appId);
99 }
Kunihiro Ishiguro93224452015-02-13 00:40:08 +0900100
alshabibc274c902014-10-03 14:58:27 -0700101 log.info("Started with Application ID {}", appId.id());
102 }
103
104 @Deactivate
105 public void deactivate() {
106 packetService.removeProcessor(processor);
107 processor = null;
108 log.info("Stopped");
109 }
110
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800111 @Modified
112 public void modified(ComponentContext context) {
113 readComponentConfiguration(context);
114 }
115
116 /**
117 * Extracts properties from the component configuration context.
118 *
119 * @param context the component context
120 */
121 private void readComponentConfiguration(ComponentContext context) {
122 Dictionary<?, ?> properties = context.getProperties();
123 Boolean flag;
124
125 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
126 if (flag == null) {
127 log.info("IPv6 Neighbor Discovery is not configured, " +
128 "using current value of {}", ipv6NeighborDiscovery);
129 } else {
130 ipv6NeighborDiscovery = flag;
131 log.info("Configured. IPv6 Neighbor Discovery is {}",
132 ipv6NeighborDiscovery ? "enabled" : "disabled");
133 }
134 }
135
136 /**
137 * Check property name is defined and set to true.
138 *
139 * @param properties properties to be looked up
140 * @param propertyName the name of the property to look up
141 * @return value when the propertyName is defined or return null
142 */
143 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
144 String propertyName) {
145 Boolean value = null;
146 try {
147 String s = (String) properties.get(propertyName);
148 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
149 } catch (ClassCastException e) {
150 // No propertyName defined.
151 value = null;
152 }
153 return value;
154 }
alshabibc274c902014-10-03 14:58:27 -0700155
156 /**
157 * Packet processor responsible for forwarding packets along their paths.
158 */
159 private class ProxyArpProcessor implements PacketProcessor {
160
161 @Override
162 public void process(PacketContext context) {
163 // Stop processing if the packet has been handled, since we
164 // can't do any more to it.
165 if (context.isHandled()) {
166 return;
167 }
168
169 //handle the arp packet.
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800170 proxyArpService.handlePacket(context);
alshabibc274c902014-10-03 14:58:27 -0700171 }
172 }
173}
174
175