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