blob: 3e5dd28f83a999ec9e05895dbde908f0b023cbda [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
17package org.onosproject.incubator.net.intf.impl;
18
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Maps;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.incubator.net.config.NetworkConfigEvent;
30import org.onosproject.incubator.net.config.NetworkConfigListener;
31import org.onosproject.incubator.net.config.NetworkConfigService;
32import org.onosproject.incubator.net.config.basics.InterfaceConfig;
33import org.onosproject.incubator.net.intf.Interface;
34import org.onosproject.incubator.net.intf.InterfaceService;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.Device;
37import org.onosproject.net.Port;
38import org.onosproject.net.device.DeviceService;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import java.util.Map;
43import java.util.Optional;
44import java.util.Set;
45
46import static java.util.stream.Collectors.collectingAndThen;
47import static java.util.stream.Collectors.toSet;
48
49/**
50 * Manages the inventory of interfaces in the system.
51 */
52@Service
53@Component(immediate = true)
54public class InterfaceManager implements InterfaceService {
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected NetworkConfigService configService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected DeviceService deviceService;
63
64 private final InternalConfigListener listener = new InternalConfigListener();
65
66 private final Map<ConnectPoint, Set<Interface>> interfaces = Maps.newConcurrentMap();
67
68 @Activate
69 public void activate() {
70 configService.addListener(listener);
71
72 for (Device d : deviceService.getDevices()) {
73 for (Port p : deviceService.getPorts(d.id())) {
74 InterfaceConfig config =
75 configService.getConfig(new ConnectPoint(d.id(), p.number()), InterfaceConfig.class);
76
77 if (config != null) {
78 updateInterfaces(config);
79 }
80 }
81 }
82
83 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
88 configService.removeListener(listener);
89
90 log.info("Stopped");
91 }
92
93 @Override
94 public Set<Interface> getInterfaces() {
95 return interfaces.values()
96 .stream()
97 .flatMap(set -> set.stream())
98 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
99 }
100
101 @Override
102 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
103 return ImmutableSet.copyOf(interfaces.get(port));
104 }
105
106 @Override
107 public Set<Interface> getInterfacesByIp(IpAddress ip) {
108 return interfaces.values()
109 .stream()
110 .flatMap(set -> set.stream())
111 .filter(intf -> intf.ipAddresses().contains(ip))
112 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
113 }
114
115 @Override
116 public Interface getMatchingInterface(IpAddress ip) {
117 Optional<Interface> match = interfaces.values()
118 .stream()
119 .flatMap(set -> set.stream())
120 .filter(intf -> intf.ipAddresses()
121 .stream()
122 .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
123 .findFirst();
124
125 if (match.isPresent()) {
126 return match.get();
127 }
128
129 return null;
130 }
131
132 @Override
133 public Set<Interface> getInterfacesByVlan(VlanId vlan) {
134 return interfaces.values()
135 .stream()
136 .flatMap(set -> set.stream())
137 .filter(intf -> intf.vlan().equals(vlan))
138 .collect(collectingAndThen(toSet(), ImmutableSet::copyOf));
139 }
140
141 private void updateInterfaces(InterfaceConfig intfConfig) {
142 interfaces.put(intfConfig.subject(), intfConfig.getInterfaces());
143 }
144
145 private void removeInterfaces(ConnectPoint port) {
146 interfaces.remove(port);
147 }
148
149 /**
150 * Listener for network config events.
151 */
152 private class InternalConfigListener implements NetworkConfigListener {
153
154 @Override
155 public void event(NetworkConfigEvent event) {
156 switch (event.type()) {
157 case CONFIG_ADDED:
158 case CONFIG_UPDATED:
159 if (event.configClass() == InterfaceConfig.class) {
160 InterfaceConfig config =
161 configService.getConfig((ConnectPoint) event.subject(), InterfaceConfig.class);
162 updateInterfaces(config);
163 }
164 break;
165 case CONFIG_REMOVED:
166 if (event.configClass() == InterfaceConfig.class) {
167 removeInterfaces((ConnectPoint) event.subject());
168 }
169 break;
170 case CONFIG_REGISTERED:
171 case CONFIG_UNREGISTERED:
172 default:
173 break;
174 }
175 }
176 }
177}