Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -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 | |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 17 | package org.onosproject.net.intf.impl; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 18 | |
Deepa Vaddireddy | 2740a11 | 2017-10-26 06:23:26 +0000 | [diff] [blame] | 19 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Maps; |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.VlanId; |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 31 | import org.onosproject.event.ListenerRegistry; |
Ray Milkey | 6c01374 | 2017-08-15 10:16:43 -0700 | [diff] [blame] | 32 | import org.onosproject.net.config.ConfigException; |
| 33 | import org.onosproject.net.config.basics.InterfaceConfig; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 34 | import org.onosproject.net.intf.Interface; |
| 35 | import org.onosproject.net.intf.InterfaceAdminService; |
| 36 | import org.onosproject.net.intf.InterfaceEvent; |
| 37 | import org.onosproject.net.intf.InterfaceListener; |
| 38 | import org.onosproject.net.intf.InterfaceService; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 39 | import org.onosproject.net.ConnectPoint; |
Deepa Vaddireddy | 2740a11 | 2017-10-26 06:23:26 +0000 | [diff] [blame] | 40 | import org.onosproject.net.config.BasicNetworkConfigService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigEvent; |
| 42 | import org.onosproject.net.config.NetworkConfigListener; |
| 43 | import org.onosproject.net.config.NetworkConfigService; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 47 | import java.util.Collection; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 48 | import java.util.Collections; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 49 | import java.util.Map; |
| 50 | import java.util.Optional; |
| 51 | import java.util.Set; |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 52 | import java.util.stream.Stream; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 53 | |
| 54 | import static java.util.stream.Collectors.collectingAndThen; |
| 55 | import static java.util.stream.Collectors.toSet; |
| 56 | |
| 57 | /** |
| 58 | * Manages the inventory of interfaces in the system. |
| 59 | */ |
| 60 | @Service |
| 61 | @Component(immediate = true) |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 62 | public class InterfaceManager extends ListenerRegistry<InterfaceEvent, InterfaceListener> |
| 63 | implements InterfaceService, InterfaceAdminService { |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 64 | |
| 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 66 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 67 | private static final Class<ConnectPoint> SUBJECT_CLASS = ConnectPoint.class; |
| 68 | private static final Class<InterfaceConfig> CONFIG_CLASS = InterfaceConfig.class; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 71 | protected NetworkConfigService configService; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 72 | |
Deepa Vaddireddy | 2740a11 | 2017-10-26 06:23:26 +0000 | [diff] [blame] | 73 | //Dependency to ensure subject factories are properly initialized |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected BasicNetworkConfigService basicNetworkConfigService; |
| 76 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 77 | private final InternalConfigListener listener = new InternalConfigListener(); |
| 78 | |
| 79 | private final Map<ConnectPoint, Set<Interface>> interfaces = Maps.newConcurrentMap(); |
| 80 | |
| 81 | @Activate |
| 82 | public void activate() { |
| 83 | configService.addListener(listener); |
| 84 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 85 | // TODO address concurrency issues here |
| 86 | for (ConnectPoint subject : configService.getSubjects(SUBJECT_CLASS, CONFIG_CLASS)) { |
| 87 | InterfaceConfig config = configService.getConfig(subject, CONFIG_CLASS); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 88 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 89 | if (config != null) { |
| 90 | updateInterfaces(config); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
| 99 | configService.removeListener(listener); |
| 100 | |
| 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Set<Interface> getInterfaces() { |
| 106 | return interfaces.values() |
| 107 | .stream() |
| 108 | .flatMap(set -> set.stream()) |
| 109 | .collect(collectingAndThen(toSet(), ImmutableSet::copyOf)); |
| 110 | } |
| 111 | |
| 112 | @Override |
Jonathan Hart | 43d232e | 2016-02-03 15:34:10 -0800 | [diff] [blame] | 113 | public Interface getInterfaceByName(ConnectPoint connectPoint, String name) { |
| 114 | Optional<Interface> intf = |
| 115 | interfaces.getOrDefault(connectPoint, Collections.emptySet()) |
| 116 | .stream() |
| 117 | .filter(i -> i.name().equals(name)) |
| 118 | .findAny(); |
| 119 | |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 120 | return intf.orElse(null); |
Jonathan Hart | 43d232e | 2016-02-03 15:34:10 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 124 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 125 | Set<Interface> intfs = interfaces.get(port); |
| 126 | if (intfs == null) { |
| 127 | return Collections.emptySet(); |
| 128 | } |
| 129 | return ImmutableSet.copyOf(intfs); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<Interface> getInterfacesByIp(IpAddress ip) { |
| 134 | return interfaces.values() |
| 135 | .stream() |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 136 | .flatMap(Collection::stream) |
| 137 | .filter(intf -> intf.ipAddressesList() |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 138 | .stream() |
| 139 | .anyMatch(ia -> ia.ipAddress().equals(ip))) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 140 | .collect(collectingAndThen(toSet(), ImmutableSet::copyOf)); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Interface getMatchingInterface(IpAddress ip) { |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 145 | return getMatchingInterfacesStream(ip).findFirst().orElse(null); |
| 146 | } |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 147 | |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public Set<Interface> getMatchingInterfaces(IpAddress ip) { |
| 150 | return getMatchingInterfacesStream(ip).collect(toSet()); |
| 151 | } |
| 152 | |
| 153 | private Stream<Interface> getMatchingInterfacesStream(IpAddress ip) { |
| 154 | return interfaces.values() |
| 155 | .stream() |
| 156 | .flatMap(Collection::stream) |
| 157 | .filter(intf -> intf.ipAddressesList() |
| 158 | .stream() |
| 159 | .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip))); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Set<Interface> getInterfacesByVlan(VlanId vlan) { |
| 164 | return interfaces.values() |
| 165 | .stream() |
Charles Chan | b3b09cd | 2017-03-14 12:53:46 -0700 | [diff] [blame] | 166 | .flatMap(Collection::stream) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 167 | .filter(intf -> intf.vlan().equals(vlan)) |
| 168 | .collect(collectingAndThen(toSet(), ImmutableSet::copyOf)); |
| 169 | } |
| 170 | |
| 171 | private void updateInterfaces(InterfaceConfig intfConfig) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 172 | try { |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 173 | Set<Interface> old = interfaces.put(intfConfig.subject(), |
| 174 | Sets.newHashSet(intfConfig.getInterfaces())); |
| 175 | |
| 176 | if (old == null) { |
| 177 | old = Collections.emptySet(); |
| 178 | } |
| 179 | |
| 180 | for (Interface intf : intfConfig.getInterfaces()) { |
| 181 | if (intf.name().equals(Interface.NO_INTERFACE_NAME)) { |
| 182 | process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf)); |
| 183 | } else { |
| 184 | Optional<Interface> oldIntf = findInterface(intf, old); |
| 185 | if (oldIntf.isPresent()) { |
| 186 | old.remove(oldIntf.get()); |
| 187 | if (!oldIntf.get().equals(intf)) { |
gaurav | 2c626cc | 2016-04-26 04:18:33 +0530 | [diff] [blame] | 188 | process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_UPDATED, intf, oldIntf.get())); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 189 | } |
| 190 | } else { |
| 191 | process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf)); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | for (Interface intf : old) { |
| 197 | if (!intf.name().equals(Interface.NO_INTERFACE_NAME)) { |
| 198 | process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf)); |
| 199 | } |
| 200 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 201 | } catch (ConfigException e) { |
| 202 | log.error("Error in interface config", e); |
| 203 | } |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 206 | private Optional<Interface> findInterface(Interface intf, Set<Interface> set) { |
| 207 | return set.stream().filter(i -> i.name().equals(intf.name())).findAny(); |
| 208 | } |
| 209 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 210 | private void removeInterfaces(ConnectPoint port) { |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 211 | Set<Interface> old = interfaces.remove(port); |
| 212 | |
| 213 | old.stream() |
| 214 | .filter(i -> !i.name().equals(Interface.NO_INTERFACE_NAME)) |
| 215 | .forEach(i -> process(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, i))); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 218 | @Override |
| 219 | public void add(Interface intf) { |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 220 | InterfaceConfig config = |
| 221 | configService.addConfig(intf.connectPoint(), CONFIG_CLASS); |
| 222 | |
| 223 | config.addInterface(intf); |
| 224 | |
| 225 | configService.applyConfig(intf.connectPoint(), CONFIG_CLASS, config.node()); |
| 226 | } |
| 227 | |
| 228 | @Override |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 229 | public boolean remove(ConnectPoint connectPoint, String name) { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 230 | InterfaceConfig config = configService.addConfig(connectPoint, CONFIG_CLASS); |
| 231 | config.removeInterface(name); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 232 | |
| 233 | try { |
| 234 | if (config.getInterfaces().isEmpty()) { |
| 235 | configService.removeConfig(connectPoint, CONFIG_CLASS); |
| 236 | } else { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 237 | configService.applyConfig(connectPoint, CONFIG_CLASS, config.node()); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 238 | } |
| 239 | } catch (ConfigException e) { |
| 240 | log.error("Error reading interfaces JSON", e); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 241 | return false; |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 242 | } |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 243 | |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 244 | return true; |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Listener for network config events. |
| 249 | */ |
| 250 | private class InternalConfigListener implements NetworkConfigListener { |
| 251 | |
| 252 | @Override |
| 253 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 254 | if (event.configClass() == CONFIG_CLASS) { |
| 255 | switch (event.type()) { |
| 256 | case CONFIG_ADDED: |
| 257 | case CONFIG_UPDATED: |
Charles Chan | ee993b1 | 2017-08-14 13:32:49 -0700 | [diff] [blame] | 258 | event.config().ifPresent(config -> updateInterfaces((InterfaceConfig) config)); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 259 | break; |
| 260 | case CONFIG_REMOVED: |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 261 | removeInterfaces((ConnectPoint) event.subject()); |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 262 | break; |
| 263 | case CONFIG_REGISTERED: |
| 264 | case CONFIG_UNREGISTERED: |
| 265 | default: |
| 266 | break; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 267 | } |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |