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