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