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