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 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.Collection; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 21 | import java.util.Dictionary; |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 22 | import java.util.List; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 27 | import java.util.stream.Stream; |
| 28 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableList; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 30 | import com.google.common.collect.ImmutableSet; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 31 | import com.google.common.collect.Streams; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Modified; |
| 36 | import org.apache.felix.scr.annotations.Property; |
| 37 | import org.apache.felix.scr.annotations.Reference; |
| 38 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 39 | import org.apache.felix.scr.annotations.Service; |
| 40 | import org.onlab.packet.ARP; |
| 41 | import org.onlab.packet.DHCP; |
| 42 | import org.onlab.packet.DHCP6; |
| 43 | import org.onlab.packet.IPacket; |
| 44 | import org.onlab.packet.IPv6; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 45 | import org.onlab.packet.Ethernet; |
| 46 | import org.onlab.packet.IPv4; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 47 | import org.onlab.packet.Ip4Address; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 48 | import org.onlab.packet.MacAddress; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 49 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 50 | import org.onlab.packet.UDP; |
| 51 | import org.onlab.packet.VlanId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 52 | import org.onlab.util.Tools; |
| 53 | import org.onosproject.cfg.ComponentConfigService; |
| 54 | import org.onosproject.core.ApplicationId; |
| 55 | import org.onosproject.core.CoreService; |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 56 | import org.onosproject.dhcprelay.api.DhcpHandler; |
| 57 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
Yi Tseng | 919b2df | 2017-09-07 16:22:51 -0700 | [diff] [blame] | 58 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 59 | import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig; |
Taras Lemkin | 371ac5e | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 60 | import org.onosproject.dhcprelay.config.DhcpServerConfig; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 61 | import org.onosproject.dhcprelay.config.EnableDhcpFpmConfig; |
Taras Lemkin | 371ac5e | 2018-03-26 14:52:58 +0000 | [diff] [blame] | 62 | import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig; |
| 63 | import org.onosproject.dhcprelay.config.IgnoreDhcpConfig; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 64 | import org.onosproject.dhcprelay.store.DhcpRecord; |
| 65 | import org.onosproject.dhcprelay.store.DhcpRelayStore; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 66 | import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore; |
| 67 | import org.onosproject.routing.fpm.api.FpmRecord; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 68 | import org.onosproject.net.Device; |
Yi Tseng | 4f2a046 | 2017-08-31 11:21:00 -0700 | [diff] [blame] | 69 | import org.onosproject.net.Host; |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 70 | import org.onosproject.net.config.Config; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 71 | import org.onosproject.net.device.DeviceEvent; |
| 72 | import org.onosproject.net.device.DeviceListener; |
| 73 | import org.onosproject.net.device.DeviceService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 74 | import org.onosproject.net.intf.Interface; |
| 75 | import org.onosproject.net.intf.InterfaceService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 76 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 77 | import org.onosproject.net.HostId; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 78 | import org.onosproject.net.config.ConfigFactory; |
| 79 | import org.onosproject.net.config.NetworkConfigEvent; |
| 80 | import org.onosproject.net.config.NetworkConfigListener; |
| 81 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 82 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 83 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 84 | import org.onosproject.net.flow.TrafficSelector; |
| 85 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 86 | import org.onosproject.net.host.HostService; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 87 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 88 | import org.onosproject.net.packet.OutboundPacket; |
| 89 | import org.onosproject.net.packet.PacketContext; |
| 90 | import org.onosproject.net.packet.PacketPriority; |
| 91 | import org.onosproject.net.packet.PacketProcessor; |
| 92 | import org.onosproject.net.packet.PacketService; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 93 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 94 | import org.osgi.service.component.ComponentContext; |
| 95 | import org.slf4j.Logger; |
| 96 | import org.slf4j.LoggerFactory; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 97 | import java.util.concurrent.Executors; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 98 | import java.util.concurrent.ExecutorService; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 99 | import java.util.concurrent.ScheduledExecutorService; |
| 100 | import java.util.concurrent.TimeUnit; |
| 101 | import static org.onlab.util.Tools.groupedThreads; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 102 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 103 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 104 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 105 | /** |
| 106 | * DHCP Relay Agent Application Component. |
| 107 | */ |
| 108 | @Component(immediate = true) |
| 109 | @Service |
| 110 | public class DhcpRelayManager implements DhcpRelayService { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 111 | public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay"; |
Yi Tseng | 06799d6 | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 112 | public static final String ROUTE_STORE_IMPL = |
| 113 | "org.onosproject.routeservice.store.RouteStoreImpl"; |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 114 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 115 | private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder() |
| 116 | .matchEthType(Ethernet.TYPE_ARP) |
| 117 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 118 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 119 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 120 | |
| 121 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 122 | new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 123 | DefaultDhcpRelayConfig.class, |
| 124 | DefaultDhcpRelayConfig.KEY, |
| 125 | true) { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 126 | @Override |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 127 | public DefaultDhcpRelayConfig createConfig() { |
| 128 | return new DefaultDhcpRelayConfig(); |
| 129 | } |
| 130 | }, |
| 131 | new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 132 | IndirectDhcpRelayConfig.class, |
| 133 | IndirectDhcpRelayConfig.KEY, |
| 134 | true) { |
Yi Tseng | 483ac6f | 2017-08-02 15:03:31 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public IndirectDhcpRelayConfig createConfig() { |
| 137 | return new IndirectDhcpRelayConfig(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 138 | } |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 139 | }, |
| 140 | new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 141 | IgnoreDhcpConfig.class, |
| 142 | IgnoreDhcpConfig.KEY, |
| 143 | true) { |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public IgnoreDhcpConfig createConfig() { |
| 146 | return new IgnoreDhcpConfig(); |
| 147 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 148 | }, |
| 149 | new ConfigFactory<ApplicationId, EnableDhcpFpmConfig>(APP_SUBJECT_FACTORY, |
| 150 | EnableDhcpFpmConfig.class, |
| 151 | EnableDhcpFpmConfig.KEY, |
| 152 | false) { |
| 153 | @Override |
| 154 | public EnableDhcpFpmConfig createConfig() { |
| 155 | return new EnableDhcpFpmConfig(); |
| 156 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 157 | } |
| 158 | ); |
| 159 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 160 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 161 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 162 | protected NetworkConfigRegistry cfgService; |
| 163 | |
| 164 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 165 | protected CoreService coreService; |
| 166 | |
| 167 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 168 | protected PacketService packetService; |
| 169 | |
| 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 171 | protected HostService hostService; |
| 172 | |
| 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 174 | protected InterfaceService interfaceService; |
| 175 | |
| 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 177 | protected DhcpRelayStore dhcpRelayStore; |
| 178 | |
| 179 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 180 | protected ComponentConfigService compCfgService; |
| 181 | |
Yi Tseng | 51f1be9 | 2017-09-01 17:24:57 -0700 | [diff] [blame] | 182 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 183 | protected DeviceService deviceService; |
| 184 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 185 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 186 | protected DhcpFpmPrefixStore dhcpFpmPrefixStore; |
| 187 | |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 188 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 189 | target = "(version=4)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 190 | protected DhcpHandler v4Handler; |
| 191 | |
| 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 193 | target = "(version=6)") |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 194 | protected DhcpHandler v6Handler; |
| 195 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 196 | @Property(name = "arpEnabled", boolValue = true, |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 197 | label = "Enable Address resolution protocol") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 198 | protected boolean arpEnabled = true; |
| 199 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 200 | @Property(name = "dhcpPollInterval", intValue = 24 * 3600, |
| 201 | label = "dhcp relay poll interval") |
| 202 | protected int dhcpPollInterval = 24 * 3600; |
| 203 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 204 | @Property(name = "dhcpFpmEnabled", boolValue = false, |
| 205 | label = "Enable DhcpRelay Fpm") |
| 206 | protected boolean dhcpFpmEnabled = false; |
| 207 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 208 | private ScheduledExecutorService timerExecutor; |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 209 | protected ExecutorService devEventExecutor; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 210 | |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 211 | protected DeviceListener deviceListener = new InternalDeviceListener(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 212 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 213 | private ApplicationId appId; |
| 214 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 215 | /** |
| 216 | * One second timer. |
| 217 | */ |
| 218 | class Dhcp6Timer implements Runnable { |
| 219 | @Override |
| 220 | public void run() { |
| 221 | v6Handler.timeTick(); |
| 222 | } |
| 223 | }; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 224 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 225 | @Activate |
| 226 | protected void activate(ComponentContext context) { |
| 227 | //start the dhcp relay agent |
| 228 | appId = coreService.registerApplication(DHCP_RELAY_APP); |
| 229 | |
| 230 | cfgService.addListener(cfgListener); |
| 231 | factories.forEach(cfgService::registerConfigFactory); |
| 232 | //update the dhcp server configuration. |
| 233 | updateConfig(); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 234 | |
| 235 | //add the packet processor |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 236 | packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0)); |
Yi Tseng | 5130129 | 2017-07-28 13:02:59 -0700 | [diff] [blame] | 237 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 238 | timerExecutor = Executors.newScheduledThreadPool(1, |
| 239 | groupedThreads("dhcpRelay", |
| 240 | "config-reloader-%d", log)); |
| 241 | timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(), |
| 242 | 0, |
| 243 | dhcpPollInterval, |
| 244 | TimeUnit.SECONDS); |
| 245 | |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 246 | devEventExecutor = newSingleThreadScheduledExecutor( |
| 247 | groupedThreads("onos/dhcprelay-dev-events", "events-%d", log)); |
| 248 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 249 | modified(context); |
| 250 | |
Yi Tseng | 06799d6 | 2017-09-01 16:02:56 -0700 | [diff] [blame] | 251 | // Enable distribute route store |
| 252 | compCfgService.preSetProperty(ROUTE_STORE_IMPL, |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 253 | "distributed", Boolean.TRUE.toString()); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 254 | compCfgService.registerProperties(getClass()); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 255 | |
| 256 | deviceService.addListener(deviceListener); |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 257 | |
| 258 | |
| 259 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 260 | log.info("DHCP-RELAY Started"); |
| 261 | } |
| 262 | |
| 263 | @Deactivate |
| 264 | protected void deactivate() { |
| 265 | cfgService.removeListener(cfgListener); |
| 266 | factories.forEach(cfgService::unregisterConfigFactory); |
| 267 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 268 | cancelArpPackets(); |
Yi Tseng | dfa8ee2 | 2017-08-07 12:45:01 -0700 | [diff] [blame] | 269 | compCfgService.unregisterProperties(getClass(), false); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 270 | deviceService.removeListener(deviceListener); |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 271 | timerExecutor.shutdown(); |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 272 | devEventExecutor.shutdownNow(); |
| 273 | devEventExecutor = null; |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 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 | |
| 283 | flag = Tools.isPropertyEnabled(properties, "arpEnabled"); |
| 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 | |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 296 | int intervalVal = Tools.getIntegerProperty(properties, "dhcpPollInterval"); |
| 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 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 311 | flag = Tools.isPropertyEnabled(properties, "dhcpFpmEnabled"); |
| 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 | e5bb709 | 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 | 6be77d8 | 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 | 285cb77 | 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 | 285cb77 | 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) { |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 627 | if (devEventExecutor != null) { |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 628 | Device device = event.subject(); |
| 629 | switch (event.type()) { |
| 630 | case DEVICE_ADDED: |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 631 | devEventExecutor.execute(this::updateIgnoreVlanConfigs); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 632 | break; |
| 633 | case DEVICE_AVAILABILITY_CHANGED: |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 634 | devEventExecutor.execute(() -> deviceAvailabilityChanged(device)); |
| 635 | break; |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 636 | default: |
| 637 | break; |
| 638 | } |
Ruchi Sahota | 196a9ca | 2019-03-01 16:56:07 +0000 | [diff] [blame^] | 639 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | private void deviceAvailabilityChanged(Device device) { |
| 643 | if (deviceService.isAvailable(device.id())) { |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 644 | updateIgnoreVlanConfigs(); |
Saurav Das | fa9004b | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 645 | } else { |
| 646 | removeIgnoreVlanState(); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 650 | private void updateIgnoreVlanConfigs() { |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 651 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
Yi Tseng | 525ff40 | 2017-10-23 19:39:39 -0700 | [diff] [blame] | 652 | v4Handler.updateIgnoreVlanConfig(config); |
| 653 | v6Handler.updateIgnoreVlanConfig(config); |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 654 | } |
Saurav Das | fa9004b | 2017-12-13 16:19:35 -0800 | [diff] [blame] | 655 | |
| 656 | private void removeIgnoreVlanState() { |
| 657 | IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class); |
| 658 | v4Handler.removeIgnoreVlanState(config); |
| 659 | v6Handler.removeIgnoreVlanState(config); |
| 660 | } |
Yi Tseng | 127ffe5 | 2017-09-12 15:55:17 -0700 | [diff] [blame] | 661 | } |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 662 | |
| 663 | |
| 664 | |
| 665 | public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) { |
| 666 | return dhcpFpmPrefixStore.getFpmRecord(prefix); |
| 667 | } |
| 668 | |
| 669 | public Collection<FpmRecord> getFpmRecords() { |
| 670 | return dhcpFpmPrefixStore.getFpmRecords(); |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) { |
| 675 | dhcpFpmPrefixStore.addFpmRecord(prefix, fpmRecord); |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) { |
| 680 | return dhcpFpmPrefixStore.removeFpmRecord(prefix); |
| 681 | } |
Kalhee Kim | e5bb709 | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 682 | |
| 683 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 684 | } |