Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [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 | package org.onosproject.dhcprelay; |
| 17 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Streams; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 21 | import org.onlab.packet.ARP; |
| 22 | import org.onlab.packet.DHCP; |
| 23 | import org.onlab.packet.DHCP6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IPacket; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IPv4; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IPv6; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 29 | import org.onlab.packet.IpPrefix; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 31 | import org.onlab.packet.UDP; |
| 32 | import org.onlab.packet.VlanId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 33 | import org.onlab.util.Tools; |
| 34 | import org.onosproject.cfg.ComponentConfigService; |
| 35 | import org.onosproject.core.ApplicationId; |
| 36 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 37 | import org.onosproject.dhcprelay.api.DhcpHandler; |
| 38 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 39 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 40 | import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 41 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 42 | import org.onosproject.dhcprelay.config.EnableDhcpFpmConfig; |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 43 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig; |
| 45 | import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 46 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 47 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 49 | import org.onosproject.net.Device; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 50 | import org.onosproject.net.Host; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 51 | import org.onosproject.net.HostId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | import org.onosproject.net.config.Config; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 53 | import org.onosproject.net.config.ConfigFactory; |
| 54 | import org.onosproject.net.config.NetworkConfigEvent; |
| 55 | import org.onosproject.net.config.NetworkConfigListener; |
| 56 | import org.onosproject.net.config.NetworkConfigRegistry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | import org.onosproject.net.device.DeviceEvent; |
| 58 | import org.onosproject.net.device.DeviceListener; |
| 59 | import org.onosproject.net.device.DeviceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 60 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 61 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 62 | import org.onosproject.net.flow.TrafficSelector; |
| 63 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 64 | import org.onosproject.net.host.HostService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | import org.onosproject.net.intf.Interface; |
| 66 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 67 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 68 | import org.onosproject.net.packet.OutboundPacket; |
| 69 | import org.onosproject.net.packet.PacketContext; |
| 70 | import org.onosproject.net.packet.PacketPriority; |
| 71 | import org.onosproject.net.packet.PacketProcessor; |
| 72 | import org.onosproject.net.packet.PacketService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | import org.onosproject.routing.fpm.api.FpmRecord; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 74 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | import org.osgi.service.component.annotations.Activate; |
| 76 | import org.osgi.service.component.annotations.Component; |
| 77 | import org.osgi.service.component.annotations.Deactivate; |
| 78 | import org.osgi.service.component.annotations.Modified; |
| 79 | import org.osgi.service.component.annotations.Reference; |
| 80 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 81 | import org.slf4j.Logger; |
| 82 | import org.slf4j.LoggerFactory; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | |
| 84 | import java.nio.ByteBuffer; |
| 85 | import java.util.Collection; |
| 86 | import java.util.Collections; |
| 87 | import java.util.Dictionary; |
| 88 | import java.util.List; |
| 89 | import java.util.Objects; |
| 90 | import java.util.Optional; |
| 91 | import java.util.Set; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 92 | import java.util.concurrent.Executors; |
| 93 | import java.util.concurrent.ScheduledExecutorService; |
| 94 | import java.util.concurrent.TimeUnit; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | import java.util.stream.Collectors; |
| 96 | import java.util.stream.Stream; |
| 97 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 98 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 99 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.ARP_ENABLED; |
| 100 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.ARP_ENABLED_DEFAULT; |
| 101 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_FPM_ENABLED; |
| 102 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_FPM_ENABLED_DEFAULT; |
| 103 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_POLL_INTERVAL; |
| 104 | import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_POLL_INTERVAL_DEFAULT; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 105 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 106 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 107 | /** |
| 108 | * DHCP Relay Agent Application Component. |
| 109 | */ |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 110 | @Component( |
| 111 | immediate = true, |
| 112 | service = DhcpRelayService.class, |
| 113 | property = { |
| 114 | ARP_ENABLED + ":Boolean=" + ARP_ENABLED_DEFAULT, |
| 115 | DHCP_POLL_INTERVAL + ":Integer=" + DHCP_POLL_INTERVAL_DEFAULT, |
| 116 | DHCP_FPM_ENABLED + ":Boolean=" + DHCP_FPM_ENABLED_DEFAULT |
| 117 | } |
| 118 | ) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 119 | public class DhcpRelayManager implements DhcpRelayService { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 120 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay"; |
Yi Tseng | 06799d6 | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 121 | public static final String ROUTE_STORE_IMPL = |
| 122 | "org.onosproject.routeservice.store.RouteStoreImpl"; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 123 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 124 | private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder() |
| 125 | .matchEthType(Ethernet.TYPE_ARP) |
| 126 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 127 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 128 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 129 | |
| 130 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 131 | new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 132 | DefaultDhcpRelayConfig.class, |
| 133 | DefaultDhcpRelayConfig.KEY, |
| 134 | true) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 135 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 136 | public DefaultDhcpRelayConfig createConfig() { |
| 137 | return new DefaultDhcpRelayConfig(); |
| 138 | } |
| 139 | }, |
| 140 | new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 141 | IndirectDhcpRelayConfig.class, |
| 142 | IndirectDhcpRelayConfig.KEY, |
| 143 | true) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public IndirectDhcpRelayConfig createConfig() { |
| 146 | return new IndirectDhcpRelayConfig(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 147 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 148 | }, |
| 149 | new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 150 | IgnoreDhcpConfig.class, |
| 151 | IgnoreDhcpConfig.KEY, |
| 152 | true) { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public IgnoreDhcpConfig createConfig() { |
| 155 | return new IgnoreDhcpConfig(); |
| 156 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 157 | }, |
| 158 | new ConfigFactory<ApplicationId, EnableDhcpFpmConfig>(APP_SUBJECT_FACTORY, |
| 159 | EnableDhcpFpmConfig.class, |
| 160 | EnableDhcpFpmConfig.KEY, |
| 161 | false) { |
| 162 | @Override |
| 163 | public EnableDhcpFpmConfig createConfig() { |
| 164 | return new EnableDhcpFpmConfig(); |
| 165 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 166 | } |
| 167 | ); |
| 168 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 169 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 171 | protected NetworkConfigRegistry cfgService; |
| 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 174 | protected CoreService coreService; |
| 175 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 177 | protected PacketService packetService; |
| 178 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 179 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 180 | protected HostService hostService; |
| 181 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 182 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 183 | protected InterfaceService interfaceService; |
| 184 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 185 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 186 | protected DhcpRelayStore dhcpRelayStore; |
| 187 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 188 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 189 | protected ComponentConfigService compCfgService; |
| 190 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 191 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 192 | protected DeviceService deviceService; |
| 193 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 194 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 195 | protected DhcpFpmPrefixStore dhcpFpmPrefixStore; |
| 196 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 197 | @Reference(cardinality = ReferenceCardinality.MANDATORY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 198 | target = "(version=4)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 199 | protected DhcpHandler v4Handler; |
| 200 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 201 | @Reference(cardinality = ReferenceCardinality.MANDATORY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 202 | target = "(version=6)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 203 | protected DhcpHandler v6Handler; |
| 204 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 205 | /** Enable Address resolution protocol. */ |
| 206 | protected boolean arpEnabled = ARP_ENABLED_DEFAULT; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 207 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 208 | /** dhcp relay poll interval. */ |
| 209 | protected int dhcpPollInterval = DHCP_POLL_INTERVAL_DEFAULT; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 210 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 211 | /** Enable DhcpRelay Fpm. */ |
| 212 | protected boolean dhcpFpmEnabled = DHCP_FPM_ENABLED_DEFAULT; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 213 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 214 | private ScheduledExecutorService timerExecutor; |
| 215 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 216 | protected DeviceListener deviceListener = new InternalDeviceListener(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 217 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 218 | private ApplicationId appId; |
| 219 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 220 | /** |
| 221 | * One second timer. |
| 222 | */ |
| 223 | class Dhcp6Timer implements Runnable { |
| 224 | @Override |
| 225 | public void run() { |
| 226 | v6Handler.timeTick(); |
| 227 | } |
| 228 | }; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 229 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 230 | @Activate |
| 231 | protected void activate(ComponentContext context) { |
| 232 | //start the dhcp relay agent |
| 233 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 234 | |
| 235 | cfgService.addListener(cfgListener); |
| 236 | factories.forEach(cfgService::registerConfigFactory); |
| 237 | //update the dhcp server configuration. |
| 238 | updateConfig(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 239 | |
| 240 | //add the packet processor |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 241 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 242 | |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 243 | timerExecutor = Executors.newScheduledThreadPool(1, |
| 244 | groupedThreads("dhcpRelay", |
| 245 | "config-reloader-%d", log)); |
| 246 | timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(), |
| 247 | 0, |
| 248 | dhcpPollInterval, |
| 249 | TimeUnit.SECONDS); |
| 250 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 251 | modified(context); |
| 252 | |
Yi Tseng | 06799d6 | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 253 | // Enable distribute route store |
| 254 | compCfgService.preSetProperty(ROUTE_STORE_IMPL, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 255 | "distributed", Boolean.TRUE.toString()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 256 | compCfgService.registerProperties(getClass()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 257 | |
| 258 | deviceService.addListener(deviceListener); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 262 | log.info("DHCP-RELAY Started"); |
| 263 | } |
| 264 | |
| 265 | @Deactivate |
| 266 | protected void deactivate() { |
| 267 | cfgService.removeListener(cfgListener); |
| 268 | factories.forEach(cfgService::unregisterConfigFactory); |
| 269 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 270 | cancelArpPackets(); |
Yi Tseng | dfa8ee2 | 2017-08-07 12:45:01 -0700 | [diff] [blame] | 271 | compCfgService.unregisterProperties(getClass(), false); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 272 | deviceService.removeListener(deviceListener); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 273 | timerExecutor.shutdown(); |
| 274 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 275 | log.info("DHCP-RELAY Stopped"); |
| 276 | } |
| 277 | |
| 278 | @Modified |
| 279 | protected void modified(ComponentContext context) { |
| 280 | Dictionary<?, ?> properties = context.getProperties(); |
| 281 | Boolean flag; |
| 282 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 283 | flag = Tools.isPropertyEnabled(properties, ARP_ENABLED); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 284 | if (flag != null) { |
| 285 | arpEnabled = flag; |
| 286 | log.info("Address resolution protocol is {}", |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 287 | arpEnabled ? "enabled" : "disabled"); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if (arpEnabled) { |
| 291 | requestArpPackets(); |
| 292 | } else { |
| 293 | cancelArpPackets(); |
| 294 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 295 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 296 | int intervalVal = Tools.getIntegerProperty(properties, DHCP_POLL_INTERVAL); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 297 | log.info("DhcpRelay poll interval new {} old {}", intervalVal, dhcpPollInterval); |
| 298 | if (intervalVal != dhcpPollInterval) { |
| 299 | timerExecutor.shutdown(); |
| 300 | dhcpPollInterval = intervalVal; |
| 301 | timerExecutor = Executors.newScheduledThreadPool(1, |
| 302 | groupedThreads("dhcpRelay", |
| 303 | "config-reloader-%d", log)); |
| 304 | timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(), |
| 305 | 0, |
| 306 | dhcpPollInterval > 1 ? dhcpPollInterval : 1, |
| 307 | TimeUnit.SECONDS); |
| 308 | v6Handler.setDhcp6PollInterval(dhcpPollInterval); |
| 309 | } |
| 310 | |
Ray Milkey | 687c00c | 2018-10-31 10:18:41 -0700 | [diff] [blame] | 311 | flag = Tools.isPropertyEnabled(properties, DHCP_FPM_ENABLED); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 312 | if (flag != null) { |
| 313 | boolean oldValue = dhcpFpmEnabled; |
| 314 | dhcpFpmEnabled = flag; |
| 315 | log.info("DhcpRelay FPM is {}", |
| 316 | dhcpFpmEnabled ? "enabled" : "disabled"); |
| 317 | |
| 318 | if (dhcpFpmEnabled && !oldValue) { |
| 319 | log.info("Dhcp Fpm is enabled."); |
| 320 | processDhcpFpmRoutes(true); |
| 321 | } |
| 322 | if (!dhcpFpmEnabled && oldValue) { |
| 323 | log.info("Dhcp Fpm is disabled."); |
| 324 | processDhcpFpmRoutes(false); |
| 325 | } |
| 326 | v6Handler.setDhcpFpmEnabled(dhcpFpmEnabled); |
| 327 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 330 | private static List<TrafficSelector> buildClientDhcpSelectors() { |
| 331 | return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(), |
| 332 | Dhcp6HandlerImpl.DHCP_SELECTORS.stream()) |
| 333 | .collect(Collectors.toList()); |
| 334 | } |
| 335 | |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Updates DHCP relay app configuration. |
| 338 | */ |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 339 | private void updateConfig() { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 340 | DefaultDhcpRelayConfig defaultConfig = |
| 341 | cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 342 | IndirectDhcpRelayConfig indirectConfig = |
| 343 | cfgService.getConfig(appId, IndirectDhcpRelayConfig.class); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 344 | IgnoreDhcpConfig ignoreDhcpConfig = |
| 345 | cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 346 | |
| 347 | if (defaultConfig != null) { |
| 348 | updateConfig(defaultConfig); |
| 349 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 350 | if (indirectConfig != null) { |
| 351 | updateConfig(indirectConfig); |
| 352 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 353 | if (ignoreDhcpConfig != null) { |
| 354 | updateConfig(ignoreDhcpConfig); |
| 355 | } |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Updates DHCP relay app configuration with given configuration. |
| 360 | * |
| 361 | * @param config the configuration ot update |
| 362 | */ |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 363 | protected void updateConfig(Config config) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 364 | if (config instanceof IndirectDhcpRelayConfig) { |
| 365 | IndirectDhcpRelayConfig indirectConfig = (IndirectDhcpRelayConfig) config; |
| 366 | v4Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
| 367 | v6Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs()); |
Yi Tseng | 3df7f9d | 2017-08-17 13:08:31 -0700 | [diff] [blame] | 368 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 369 | DefaultDhcpRelayConfig defaultConfig = (DefaultDhcpRelayConfig) config; |
| 370 | v4Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
| 371 | v6Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs()); |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 372 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 373 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 374 | v4Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
| 375 | v6Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | protected void removeConfig(Config config) { |
| 380 | if (config instanceof IndirectDhcpRelayConfig) { |
| 381 | v4Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 382 | v6Handler.setIndirectDhcpServerConfigs(Collections.emptyList()); |
| 383 | } else if (config instanceof DefaultDhcpRelayConfig) { |
| 384 | v4Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 385 | v6Handler.setDefaultDhcpServerConfigs(Collections.emptyList()); |
| 386 | } |
| 387 | if (config instanceof IgnoreDhcpConfig) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 388 | v4Handler.updateIgnoreVlanConfig(null); |
| 389 | v6Handler.updateIgnoreVlanConfig(null); |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 393 | private void processDhcpFpmRoutes(Boolean add) { |
| 394 | // needs to restore/remove fpm |
| 395 | } |
| 396 | |
| 397 | public boolean isDhcpFpmEnabled() { |
| 398 | return dhcpFpmEnabled; |
| 399 | } |
| 400 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 401 | /** |
| 402 | * Request ARP packet in via PacketService. |
| 403 | */ |
| 404 | private void requestArpPackets() { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 405 | packetService.requestPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Cancel requested ARP packets in via packet service. |
| 410 | */ |
| 411 | private void cancelArpPackets() { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 412 | packetService.cancelPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public Optional<DhcpRecord> getDhcpRecord(HostId hostId) { |
| 417 | return dhcpRelayStore.getDhcpRecord(hostId); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public Collection<DhcpRecord> getDhcpRecords() { |
| 422 | return dhcpRelayStore.getDhcpRecords(); |
| 423 | } |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 424 | @Override |
| 425 | public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) { |
| 426 | dhcpRelayStore.updateDhcpRecord(hostId, dhcpRecord); |
| 427 | } |
Yi Tseng | 13a41a1 | 2017-07-26 13:45:01 -0700 | [diff] [blame] | 428 | @Override |
| 429 | public Optional<MacAddress> getDhcpServerMacAddress() { |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 430 | // TODO: depreated it |
| 431 | DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class); |
| 432 | DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0); |
| 433 | Ip4Address serverip = serverConfig.getDhcpServerIp4().get(); |
| 434 | return hostService.getHostsByIp(serverip) |
| 435 | .stream() |
| 436 | .map(Host::mac) |
| 437 | .findFirst(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 440 | @Override |
| 441 | public List<DhcpServerInfo> getDefaultDhcpServerInfoList() { |
| 442 | return ImmutableList.<DhcpServerInfo>builder() |
| 443 | .addAll(v4Handler.getDefaultDhcpServerInfoList()) |
| 444 | .addAll(v6Handler.getDefaultDhcpServerInfoList()) |
| 445 | .build(); |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public List<DhcpServerInfo> getIndirectDhcpServerInfoList() { |
| 450 | return ImmutableList.<DhcpServerInfo>builder() |
| 451 | .addAll(v4Handler.getIndirectDhcpServerInfoList()) |
| 452 | .addAll(v6Handler.getIndirectDhcpServerInfoList()) |
| 453 | .build(); |
| 454 | } |
| 455 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 456 | /** |
| 457 | * Gets DHCP data from a packet. |
| 458 | * |
| 459 | * @param packet the packet |
| 460 | * @return the DHCP data; empty if it is not a DHCP packet |
| 461 | */ |
| 462 | private Optional<DHCP> findDhcp(Ethernet packet) { |
| 463 | return Stream.of(packet) |
| 464 | .filter(Objects::nonNull) |
| 465 | .map(Ethernet::getPayload) |
| 466 | .filter(p -> p instanceof IPv4) |
| 467 | .map(IPacket::getPayload) |
| 468 | .filter(Objects::nonNull) |
| 469 | .filter(p -> p instanceof UDP) |
| 470 | .map(IPacket::getPayload) |
| 471 | .filter(Objects::nonNull) |
| 472 | .filter(p -> p instanceof DHCP) |
| 473 | .map(p -> (DHCP) p) |
| 474 | .findFirst(); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Gets DHCPv6 data from a packet. |
| 479 | * |
| 480 | * @param packet the packet |
| 481 | * @return the DHCPv6 data; empty if it is not a DHCPv6 packet |
| 482 | */ |
| 483 | private Optional<DHCP6> findDhcp6(Ethernet packet) { |
| 484 | return Stream.of(packet) |
| 485 | .filter(Objects::nonNull) |
| 486 | .map(Ethernet::getPayload) |
| 487 | .filter(p -> p instanceof IPv6) |
| 488 | .map(IPacket::getPayload) |
| 489 | .filter(Objects::nonNull) |
| 490 | .filter(p -> p instanceof UDP) |
| 491 | .map(IPacket::getPayload) |
| 492 | .filter(Objects::nonNull) |
| 493 | .filter(p -> p instanceof DHCP6) |
| 494 | .map(p -> (DHCP6) p) |
| 495 | .findFirst(); |
| 496 | } |
| 497 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 498 | |
| 499 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 500 | |
| 501 | @Override |
| 502 | public void process(PacketContext context) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 503 | // process the packet and get the payload |
| 504 | Ethernet packet = context.inPacket().parsed(); |
| 505 | if (packet == null) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | findDhcp(packet).ifPresent(dhcpPayload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 510 | v4Handler.processDhcpPacket(context, dhcpPayload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 511 | }); |
| 512 | |
| 513 | findDhcp6(packet).ifPresent(dhcp6Payload -> { |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 514 | v6Handler.processDhcpPacket(context, dhcp6Payload); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 515 | }); |
| 516 | |
| 517 | if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) { |
| 518 | ARP arpPacket = (ARP) packet.getPayload(); |
| 519 | VlanId vlanId = VlanId.vlanId(packet.getVlanID()); |
| 520 | Set<Interface> interfaces = interfaceService. |
| 521 | getInterfacesByPort(context.inPacket().receivedFrom()); |
| 522 | //ignore the packets if dhcp server interface is not configured on onos. |
| 523 | if (interfaces.isEmpty()) { |
| 524 | log.warn("server virtual interface not configured"); |
| 525 | return; |
| 526 | } |
| 527 | if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) { |
| 528 | // handle request only |
| 529 | return; |
| 530 | } |
| 531 | MacAddress interfaceMac = interfaces.stream() |
| 532 | .filter(iface -> iface.vlan().equals(vlanId)) |
| 533 | .map(Interface::mac) |
| 534 | .filter(mac -> !mac.equals(MacAddress.NONE)) |
| 535 | .findFirst() |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 536 | .orElse(MacAddress.ONOS); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 537 | if (interfaceMac == null) { |
| 538 | // can't find interface mac address |
| 539 | return; |
| 540 | } |
| 541 | processArpPacket(context, packet, interfaceMac); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Processes the ARP Payload and initiates a reply to the client. |
| 547 | * |
| 548 | * @param context the packet context |
| 549 | * @param packet the ethernet payload |
| 550 | * @param replyMac mac address to be replied |
| 551 | */ |
| 552 | private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) { |
| 553 | ARP arpPacket = (ARP) packet.getPayload(); |
Ray Milkey | f0c4761 | 2017-09-28 11:29:38 -0700 | [diff] [blame] | 554 | ARP arpReply = arpPacket.duplicate(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 555 | arpReply.setOpCode(ARP.OP_REPLY); |
| 556 | |
| 557 | arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress()); |
| 558 | arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress()); |
| 559 | arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress()); |
| 560 | arpReply.setSenderHardwareAddress(replyMac.toBytes()); |
| 561 | |
| 562 | // Ethernet Frame. |
| 563 | Ethernet ethReply = new Ethernet(); |
| 564 | ethReply.setSourceMACAddress(replyMac.toBytes()); |
| 565 | ethReply.setDestinationMACAddress(packet.getSourceMAC()); |
| 566 | ethReply.setEtherType(Ethernet.TYPE_ARP); |
| 567 | ethReply.setVlanID(packet.getVlanID()); |
| 568 | ethReply.setPayload(arpReply); |
| 569 | |
| 570 | ConnectPoint targetPort = context.inPacket().receivedFrom(); |
| 571 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 572 | .setOutput(targetPort.port()).build(); |
| 573 | OutboundPacket o = new DefaultOutboundPacket( |
| 574 | targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize())); |
| 575 | if (log.isTraceEnabled()) { |
| 576 | log.trace("Relaying ARP packet {} to {}", packet, targetPort); |
| 577 | } |
| 578 | packetService.emit(o); |
| 579 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Listener for network config events. |
| 584 | */ |
| 585 | private class InternalConfigListener implements NetworkConfigListener { |
| 586 | @Override |
| 587 | public void event(NetworkConfigEvent event) { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 588 | switch (event.type()) { |
| 589 | case CONFIG_UPDATED: |
| 590 | case CONFIG_ADDED: |
| 591 | event.config().ifPresent(config -> { |
| 592 | updateConfig(config); |
| 593 | log.info("{} updated", config.getClass().getSimpleName()); |
| 594 | }); |
| 595 | break; |
| 596 | case CONFIG_REMOVED: |
| 597 | event.prevConfig().ifPresent(config -> { |
| 598 | removeConfig(config); |
| 599 | log.info("{} removed", config.getClass().getSimpleName()); |
| 600 | }); |
| 601 | break; |
Charles Chan | 10b2fee | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 602 | case CONFIG_REGISTERED: |
| 603 | case CONFIG_UNREGISTERED: |
| 604 | break; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 605 | default: |
| 606 | log.warn("Unsupported event type {}", event.type()); |
| 607 | break; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 608 | } |
Charles Chan | 50443e8 | 2018-01-03 16:26:32 -0800 | [diff] [blame] | 609 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 610 | |
Charles Chan | 50443e8 | 2018-01-03 16:26:32 -0800 | [diff] [blame] | 611 | @Override |
| 612 | public boolean isRelevant(NetworkConfigEvent event) { |
| 613 | if (event.configClass().equals(DefaultDhcpRelayConfig.class) || |
| 614 | event.configClass().equals(IndirectDhcpRelayConfig.class) || |
| 615 | event.configClass().equals(IgnoreDhcpConfig.class)) { |
| 616 | return true; |
| 617 | } |
| 618 | log.debug("Ignore irrelevant event class {}", event.configClass().getName()); |
| 619 | return false; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 622 | |
| 623 | private class InternalDeviceListener implements DeviceListener { |
| 624 | |
| 625 | @Override |
| 626 | public void event(DeviceEvent event) { |
| 627 | Device device = event.subject(); |
| 628 | switch (event.type()) { |
| 629 | case DEVICE_ADDED: |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 630 | updateIgnoreVlanConfigs(); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 631 | break; |
| 632 | case DEVICE_AVAILABILITY_CHANGED: |
| 633 | deviceAvailabilityChanged(device); |
Ray Milkey | d6a67c3 | 2018-02-02 10:30:35 -0800 | [diff] [blame] | 634 | break; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 635 | default: |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | private void deviceAvailabilityChanged(Device device) { |
| 641 | if (deviceService.isAvailable(device.id())) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 642 | updateIgnoreVlanConfigs(); |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 643 | } else { |
| 644 | removeIgnoreVlanState(); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 648 | private void updateIgnoreVlanConfigs() { |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 649 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 650 | v4Handler.updateIgnoreVlanConfig(config); |
| 651 | v6Handler.updateIgnoreVlanConfig(config); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 652 | } |
Saurav Das | b805f1a | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 653 | |
| 654 | private void removeIgnoreVlanState() { |
| 655 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
| 656 | v4Handler.removeIgnoreVlanState(config); |
| 657 | v6Handler.removeIgnoreVlanState(config); |
| 658 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 659 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 660 | |
| 661 | |
| 662 | |
| 663 | public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) { |
| 664 | return dhcpFpmPrefixStore.getFpmRecord(prefix); |
| 665 | } |
| 666 | |
| 667 | public Collection<FpmRecord> getFpmRecords() { |
| 668 | return dhcpFpmPrefixStore.getFpmRecords(); |
| 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) { |
| 673 | dhcpFpmPrefixStore.addFpmRecord(prefix, fpmRecord); |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) { |
| 678 | return dhcpFpmPrefixStore.removeFpmRecord(prefix); |
| 679 | } |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 680 | |
| 681 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 682 | } |