2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.incubator.net.intf.impl;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Maps;
21 import org.apache.felix.scr.annotations.Activate;
22 import org.apache.felix.scr.annotations.Component;
23 import org.apache.felix.scr.annotations.Deactivate;
24 import org.apache.felix.scr.annotations.Reference;
25 import org.apache.felix.scr.annotations.ReferenceCardinality;
26 import org.apache.felix.scr.annotations.Service;
27 import org.onlab.packet.IpAddress;
28 import org.onlab.packet.VlanId;
29 import org.onosproject.incubator.net.config.basics.ConfigException;
30 import org.onosproject.incubator.net.config.basics.InterfaceConfig;
31 import org.onosproject.incubator.net.intf.Interface;
32 import org.onosproject.incubator.net.intf.InterfaceAdminService;
33 import org.onosproject.incubator.net.intf.InterfaceService;
34 import org.onosproject.net.ConnectPoint;
35 import org.onosproject.net.config.NetworkConfigEvent;
36 import org.onosproject.net.config.NetworkConfigListener;
37 import org.onosproject.net.config.NetworkConfigService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.util.Collections;
43 import java.util.Optional;
46 import static java.util.stream.Collectors.collectingAndThen;
47 import static java.util.stream.Collectors.toSet;
50 * Manages the inventory of interfaces in the system.
53 @Component(immediate = true)
54 public class InterfaceManager implements InterfaceService,
55 InterfaceAdminService {
57 private final Logger log = LoggerFactory.getLogger(getClass());
59 private static final Class<ConnectPoint> SUBJECT_CLASS = ConnectPoint.class;
60 private static final Class<InterfaceConfig> CONFIG_CLASS = InterfaceConfig.class;
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected NetworkConfigService configService;
65 private final InternalConfigListener listener = new InternalConfigListener();
67 private final Map<ConnectPoint, Set<Interface>> interfaces = Maps.newConcurrentMap();
70 public void activate() {
71 configService.addListener(listener);
73 // TODO address concurrency issues here
74 for (ConnectPoint subject : configService.getSubjects(SUBJECT_CLASS, CONFIG_CLASS)) {
75 InterfaceConfig config = configService.getConfig(subject, CONFIG_CLASS);
78 updateInterfaces(config);
86 public void deactivate() {
87 configService.removeListener(listener);
93 public Set<Interface> getInterfaces() {
94 return interfaces.values()
96 .flatMap(set -> set.stream())
97 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
101 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
102 Set<Interface> intfs = interfaces.get(port);
104 return Collections.emptySet();
106 return ImmutableSet.copyOf(intfs);
110 public Set<Interface> getInterfacesByIp(IpAddress ip) {
111 return interfaces.values()
113 .flatMap(set -> set.stream())
114 .filter(intf -> intf.ipAddresses()
116 .anyMatch(ia -> ia.ipAddress().equals(ip)))
117 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
121 public Interface getMatchingInterface(IpAddress ip) {
122 Optional<Interface> match = interfaces.values()
124 .flatMap(set -> set.stream())
125 .filter(intf -> intf.ipAddresses()
127 .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
130 if (match.isPresent()) {
138 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
139 return interfaces.values()
141 .flatMap(set -> set.stream())
142 .filter(intf -> intf.vlan().equals(vlan))
143 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
146 private void updateInterfaces(InterfaceConfig intfConfig) {
148 interfaces.put(intfConfig.subject(), intfConfig.getInterfaces());
149 } catch (ConfigException e) {
150 log.error("Error in interface config", e);
154 private void removeInterfaces(ConnectPoint port) {
155 interfaces.remove(port);
159 public void add(Interface intf) {
160 if (interfaces.containsKey(intf.connectPoint())) {
161 boolean conflict = interfaces.get(intf.connectPoint()).stream()
162 .filter(i -> i.connectPoint().equals(intf.connectPoint()))
163 .filter(i -> i.mac().equals(intf.mac()))
164 .filter(i -> i.vlan().equals(intf.vlan()))
165 .findAny().isPresent();
168 log.error("Can't add interface because it conflicts with existing config");
173 InterfaceConfig config =
174 configService.addConfig(intf.connectPoint(), CONFIG_CLASS);
176 config.addInterface(intf);
178 configService.applyConfig(intf.connectPoint(), CONFIG_CLASS, config.node());
182 public void remove(ConnectPoint connectPoint, VlanId vlanId) {
183 Optional<Interface> intf = interfaces.get(connectPoint).stream()
184 .filter(i -> i.vlan().equals(vlanId))
187 if (!intf.isPresent()) {
188 log.error("Can't find interface {}/{} to remove", connectPoint, vlanId);
192 InterfaceConfig config = configService.addConfig(intf.get().connectPoint(), CONFIG_CLASS);
193 config.removeInterface(intf.get());
196 if (config.getInterfaces().isEmpty()) {
197 configService.removeConfig(connectPoint, CONFIG_CLASS);
199 configService.applyConfig(intf.get().connectPoint(), CONFIG_CLASS, config.node());
201 } catch (ConfigException e) {
202 log.error("Error reading interfaces JSON", e);
207 * Listener for network config events.
209 private class InternalConfigListener implements NetworkConfigListener {
212 public void event(NetworkConfigEvent event) {
213 switch (event.type()) {
216 if (event.configClass() == InterfaceConfig.class) {
217 InterfaceConfig config =
218 configService.getConfig((ConnectPoint) event.subject(), InterfaceConfig.class);
219 updateInterfaces(config);
223 if (event.configClass() == InterfaceConfig.class) {
224 removeInterfaces((ConnectPoint) event.subject());
227 case CONFIG_REGISTERED:
228 case CONFIG_UNREGISTERED: