Jonathan Hart
Committed by Gerrit Code Review

Created InterfaceService which maintains an inventory of interfaces

based on configuration data.

Change-Id: I98a360fd1b833885fcb41bf9bb8a6e81dc436fd1
...@@ -15,11 +15,10 @@ ...@@ -15,11 +15,10 @@
15 */ 15 */
16 package org.onosproject.cli; 16 package org.onosproject.cli;
17 17
18 -import java.util.Comparator;
19 -
20 import org.onosproject.cluster.ControllerNode; 18 import org.onosproject.cluster.ControllerNode;
21 import org.onosproject.core.Application; 19 import org.onosproject.core.Application;
22 import org.onosproject.core.ApplicationId; 20 import org.onosproject.core.ApplicationId;
21 +import org.onosproject.incubator.net.intf.Interface;
23 import org.onosproject.net.ConnectPoint; 22 import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.Element; 23 import org.onosproject.net.Element;
25 import org.onosproject.net.ElementId; 24 import org.onosproject.net.ElementId;
...@@ -29,6 +28,8 @@ import org.onosproject.net.group.Group; ...@@ -29,6 +28,8 @@ import org.onosproject.net.group.Group;
29 import org.onosproject.net.host.PortAddresses; 28 import org.onosproject.net.host.PortAddresses;
30 import org.onosproject.net.topology.TopologyCluster; 29 import org.onosproject.net.topology.TopologyCluster;
31 30
31 +import java.util.Comparator;
32 +
32 /** 33 /**
33 * Various comparators. 34 * Various comparators.
34 */ 35 */
...@@ -119,4 +120,7 @@ public final class Comparators { ...@@ -119,4 +120,7 @@ public final class Comparators {
119 } 120 }
120 }; 121 };
121 122
123 + public static final Comparator<Interface> INTERFACES_COMPARATOR = (intf1, intf2) ->
124 + CONNECT_POINT_COMPARATOR.compare(intf1.connectPoint(), intf2.connectPoint());
125 +
122 } 126 }
......
1 +/*
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 +package org.onosproject.cli.net;
17 +
18 +import com.google.common.collect.Lists;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.cli.Comparators;
22 +import org.onosproject.incubator.net.intf.Interface;
23 +import org.onosproject.incubator.net.intf.InterfaceService;
24 +
25 +import java.util.Collections;
26 +import java.util.List;
27 +
28 +/**
29 + * Lists all configured interfaces.
30 + */
31 +@Command(scope = "onos", name = "interfaces",
32 + description = "Lists all configured interfaces.")
33 +public class InterfacesListCommand extends AbstractShellCommand {
34 +
35 + private static final String FORMAT =
36 + "port=%s/%s, ips=%s, mac=%s, vlan=%s";
37 +
38 + @Override
39 + protected void execute() {
40 + InterfaceService interfaceService = get(InterfaceService.class);
41 +
42 + List<Interface> interfaces = Lists.newArrayList(interfaceService.getInterfaces());
43 +
44 + Collections.sort(interfaces, Comparators.INTERFACES_COMPARATOR);
45 +
46 + for (Interface intf : interfaces) {
47 + print(FORMAT, intf.connectPoint().deviceId(), intf.connectPoint().port(),
48 + intf.ipAddresses(), intf.mac(), intf.vlan());
49 + }
50 + }
51 +
52 +}
...@@ -323,6 +323,9 @@ ...@@ -323,6 +323,9 @@
323 <command> 323 <command>
324 <action class="org.onosproject.cli.net.AddressBindingsListCommand"/> 324 <action class="org.onosproject.cli.net.AddressBindingsListCommand"/>
325 </command> 325 </command>
326 + <command>
327 + <action class="org.onosproject.cli.net.InterfacesListCommand"/>
328 + </command>
326 329
327 <command> 330 <command>
328 <action class="org.onosproject.cli.net.GroupsListCommand"/> 331 <action class="org.onosproject.cli.net.GroupsListCommand"/>
...@@ -365,7 +368,7 @@ ...@@ -365,7 +368,7 @@
365 <entry key="-a" value-ref="allAppNameCompleter"/> 368 <entry key="-a" value-ref="allAppNameCompleter"/>
366 </optional-completers> 369 </optional-completers>
367 </command> 370 </command>
368 - 371 +
369 <command> 372 <command>
370 <action class="org.onosproject.cli.net.GlobalLabelCommand"/> 373 <action class="org.onosproject.cli.net.GlobalLabelCommand"/>
371 </command> 374 </command>
......
...@@ -15,16 +15,16 @@ ...@@ -15,16 +15,16 @@
15 */ 15 */
16 package org.onosproject.net.host; 16 package org.onosproject.net.host;
17 17
18 -import java.util.Set; 18 +import org.onlab.packet.IpAddress;
19 - 19 +import org.onlab.packet.MacAddress;
20 +import org.onlab.packet.VlanId;
20 import org.onosproject.event.ListenerService; 21 import org.onosproject.event.ListenerService;
21 import org.onosproject.net.ConnectPoint; 22 import org.onosproject.net.ConnectPoint;
22 import org.onosproject.net.DeviceId; 23 import org.onosproject.net.DeviceId;
23 import org.onosproject.net.Host; 24 import org.onosproject.net.Host;
24 import org.onosproject.net.HostId; 25 import org.onosproject.net.HostId;
25 -import org.onlab.packet.IpAddress; 26 +
26 -import org.onlab.packet.MacAddress; 27 +import java.util.Set;
27 -import org.onlab.packet.VlanId;
28 28
29 /** 29 /**
30 * Service for interacting with the inventory of end-station hosts. 30 * Service for interacting with the inventory of end-station hosts.
...@@ -127,7 +127,9 @@ public interface HostService ...@@ -127,7 +127,9 @@ public interface HostService
127 * Returns the addresses information for all connection points. 127 * Returns the addresses information for all connection points.
128 * 128 *
129 * @return the set of address bindings for all connection points 129 * @return the set of address bindings for all connection points
130 + * @deprecated in Drake release: use InterfaceService instead
130 */ 131 */
132 + @Deprecated
131 Set<PortAddresses> getAddressBindings(); 133 Set<PortAddresses> getAddressBindings();
132 134
133 /** 135 /**
...@@ -136,7 +138,9 @@ public interface HostService ...@@ -136,7 +138,9 @@ public interface HostService
136 * 138 *
137 * @param connectPoint the connection point to retrieve address bindings for 139 * @param connectPoint the connection point to retrieve address bindings for
138 * @return addresses bound to the port 140 * @return addresses bound to the port
141 + * @deprecated in Drake release: use InterfaceService instead
139 */ 142 */
143 + @Deprecated
140 Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint); 144 Set<PortAddresses> getAddressBindingsForPort(ConnectPoint connectPoint);
141 145
142 } 146 }
......
1 -/*
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 -
17 -package org.onosproject.incubator.net.config.basics;
18 -
19 -import com.fasterxml.jackson.databind.JsonNode;
20 -import com.fasterxml.jackson.databind.node.ArrayNode;
21 -import com.google.common.collect.Sets;
22 -import org.onlab.packet.MacAddress;
23 -import org.onlab.packet.VlanId;
24 -import org.onosproject.incubator.net.config.Config;
25 -import org.onosproject.net.ConnectPoint;
26 -import org.onosproject.net.host.InterfaceIpAddress;
27 -
28 -import java.util.Iterator;
29 -import java.util.Set;
30 -
31 -/**
32 - * Basic configuration for a port on a device.
33 - */
34 -public class BasicPortConfig extends Config<ConnectPoint> {
35 - public static final String IPS = "ips";
36 - public static final String MAC = "mac";
37 - public static final String VLAN = "vlan";
38 -
39 - /**
40 - * Returns the set of IP addresses assigned to the port.
41 - *
42 - * @return set ip IP addresses
43 - */
44 - public Set<InterfaceIpAddress> ips() {
45 - Set<InterfaceIpAddress> ips = Sets.newHashSet();
46 -
47 - JsonNode ipsNode = node.get(IPS);
48 - ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
49 -
50 - return ips;
51 - }
52 -
53 - /**
54 - * Adds an IP address to configuration of the port.
55 - *
56 - * @param ip ip address to add
57 - * @return this
58 - */
59 - public BasicPortConfig addIp(InterfaceIpAddress ip) {
60 - ArrayNode ipsNode = (ArrayNode) node.get(IPS);
61 - if (ipsNode == null) {
62 - ipsNode = node.putArray(IPS);
63 - }
64 -
65 - // Check if the value is already there
66 - if (ipsNode.findValue(ip.toString()) != null) {
67 - ipsNode.add(ip.toString());
68 - }
69 -
70 - return this;
71 - }
72 -
73 - /**
74 - * Removes an IP address from the configuration of the port.
75 - *
76 - * @param ip ip address to remove
77 - * @return this
78 - */
79 - public BasicPortConfig removeIp(InterfaceIpAddress ip) {
80 - ArrayNode ipsNode = (ArrayNode) node.get(IPS);
81 -
82 - if (ipsNode != null) {
83 - if (ipsNode.size() == 1) {
84 - node.remove(IPS);
85 - } else {
86 - Iterator<JsonNode> it = ipsNode.iterator();
87 - while (it.hasNext()) {
88 - if (it.next().asText().equals(ip.toString())) {
89 - it.remove();
90 - break;
91 - }
92 - }
93 - }
94 - }
95 -
96 - return this;
97 - }
98 -
99 - /**
100 - * Clear all IP addresses from the configuration.
101 - *
102 - * @return this
103 - */
104 - public BasicPortConfig clearIps() {
105 - node.remove(IPS);
106 - return this;
107 - }
108 -
109 - /**
110 - * Returns the MAC address configured on the port.
111 - *
112 - * @return MAC address
113 - */
114 - public MacAddress mac() {
115 - JsonNode macNode = node.get(MAC);
116 - if (macNode == null) {
117 - return null;
118 - }
119 -
120 - return MacAddress.valueOf(macNode.asText());
121 - }
122 -
123 - /**
124 - * Sets the MAC address configured on the port.
125 - *
126 - * @param mac MAC address
127 - * @return this
128 - */
129 - public BasicPortConfig mac(MacAddress mac) {
130 - String macString = (mac == null) ? null : mac.toString();
131 - return (BasicPortConfig) setOrClear(MAC, macString);
132 - }
133 -
134 - /**
135 - * Returns the VLAN configured on the port.
136 - *
137 - * @return VLAN ID
138 - */
139 - public VlanId vlan() {
140 - JsonNode macNode = node.get(VLAN);
141 - if (macNode == null) {
142 - return null;
143 - }
144 -
145 - return VlanId.vlanId(Short.parseShort(macNode.asText()));
146 - }
147 -
148 - /**
149 - * Sets the VLAN configured on the port.
150 - *
151 - * @param vlan VLAN ID
152 - * @return this
153 - */
154 - public BasicPortConfig vlan(VlanId vlan) {
155 - Integer vlanId = (vlan == null) ? null : Integer.valueOf(vlan.toShort());
156 - return (BasicPortConfig) setOrClear(VLAN, vlanId);
157 - }
158 -}
1 +/*
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 +
17 +package org.onosproject.incubator.net.config.basics;
18 +
19 +import com.fasterxml.jackson.databind.JsonNode;
20 +import com.google.common.collect.Sets;
21 +import org.onlab.packet.MacAddress;
22 +import org.onlab.packet.VlanId;
23 +import org.onosproject.incubator.net.config.Config;
24 +import org.onosproject.incubator.net.intf.Interface;
25 +import org.onosproject.net.ConnectPoint;
26 +import org.onosproject.net.host.InterfaceIpAddress;
27 +
28 +import java.util.Set;
29 +
30 +/**
31 + * Configuration for interfaces.
32 + */
33 +public class InterfaceConfig extends Config<ConnectPoint> {
34 + public static final String INTERFACES = "interfaces";
35 + public static final String IPS = "ips";
36 + public static final String MAC = "mac";
37 + public static final String VLAN = "vlan";
38 +
39 + /**
40 + * Retrieves all interfaces configured on this port.
41 + *
42 + * @return set of interfaces
43 + */
44 + public Set<Interface> getInterfaces() {
45 + Set<Interface> interfaces = Sets.newHashSet();
46 +
47 + for (JsonNode intfNode : node.path(INTERFACES)) {
48 + interfaces.add(new Interface(subject,
49 + getIps(intfNode),
50 + MacAddress.valueOf(intfNode.path(MAC).asText()),
51 + VlanId.vlanId(Short.parseShort(intfNode.path(VLAN).asText()))));
52 + }
53 +
54 + return interfaces;
55 + }
56 +
57 + private Set<InterfaceIpAddress> getIps(JsonNode node) {
58 + Set<InterfaceIpAddress> ips = Sets.newHashSet();
59 +
60 + JsonNode ipsNode = node.get(IPS);
61 + ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
62 +
63 + return ips;
64 + }
65 +
66 +}
1 +/*
2 + * Copyright 2014-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 +package org.onosproject.incubator.net.intf;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.collect.Sets;
20 +import org.onlab.packet.MacAddress;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.net.ConnectPoint;
23 +import org.onosproject.net.host.InterfaceIpAddress;
24 +
25 +import java.util.Objects;
26 +import java.util.Set;
27 +
28 +/**
29 + * An Interface maps network configuration information (such as addresses and
30 + * vlans) to a port in the network.
31 + */
32 +public class Interface {
33 + private final ConnectPoint connectPoint;
34 + private final Set<InterfaceIpAddress> ipAddresses;
35 + private final MacAddress macAddress;
36 + private final VlanId vlan;
37 +
38 + /**
39 + * Creates new Interface with the provided configuration.
40 + *
41 + * @param connectPoint the connect point this interface maps to
42 + * @param ipAddresses Set of IP addresses
43 + * @param macAddress MAC address
44 + * @param vlan VLAN ID
45 + */
46 + public Interface(ConnectPoint connectPoint,
47 + Set<InterfaceIpAddress> ipAddresses,
48 + MacAddress macAddress, VlanId vlan) {
49 + this.connectPoint = connectPoint;
50 + this.ipAddresses = Sets.newHashSet(ipAddresses);
51 + this.macAddress = macAddress;
52 + this.vlan = vlan;
53 + }
54 +
55 + /**
56 + * Retrieves the connection point that this interface maps to.
57 + *
58 + * @return the connection point
59 + */
60 + public ConnectPoint connectPoint() {
61 + return connectPoint;
62 + }
63 +
64 + /**
65 + * Retrieves the set of IP addresses that are assigned to the interface.
66 + *
67 + * @return the set of interface IP addresses
68 + */
69 + public Set<InterfaceIpAddress> ipAddresses() {
70 + return ipAddresses;
71 + }
72 +
73 + /**
74 + * Retrieves the MAC address that is assigned to the interface.
75 + *
76 + * @return the MAC address
77 + */
78 + public MacAddress mac() {
79 + return macAddress;
80 + }
81 +
82 + /**
83 + * Retrieves the VLAN ID that is assigned to the interface.
84 + *
85 + * @return the VLAN ID
86 + */
87 + public VlanId vlan() {
88 + return vlan;
89 + }
90 +
91 + @Override
92 + public boolean equals(Object other) {
93 + if (!(other instanceof Interface)) {
94 + return false;
95 + }
96 +
97 + Interface otherInterface = (Interface) other;
98 +
99 + return Objects.equals(connectPoint, otherInterface.connectPoint) &&
100 + Objects.equals(ipAddresses, otherInterface.ipAddresses) &&
101 + Objects.equals(macAddress, otherInterface.macAddress) &&
102 + Objects.equals(vlan, otherInterface.vlan);
103 + }
104 +
105 + @Override
106 + public int hashCode() {
107 + return Objects.hash(connectPoint, ipAddresses, macAddress, vlan);
108 + }
109 +
110 + @Override
111 + public String toString() {
112 + return MoreObjects.toStringHelper(getClass())
113 + .add("connectPoint", connectPoint)
114 + .add("ipAddresses", ipAddresses)
115 + .add("macAddress", macAddress)
116 + .add("vlan", vlan)
117 + .toString();
118 + }
119 +}
1 +/*
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 +
17 +package org.onosproject.incubator.net.intf;
18 +
19 +import org.onlab.packet.IpAddress;
20 +import org.onlab.packet.VlanId;
21 +import org.onosproject.net.ConnectPoint;
22 +
23 +import java.util.Set;
24 +
25 +/**
26 + * Service for interacting with interfaces.
27 + */
28 +public interface InterfaceService {
29 +
30 + /**
31 + * Returns the set of all interfaces in the system.
32 + *
33 + * @return set of interfaces
34 + */
35 + Set<Interface> getInterfaces();
36 +
37 + /**
38 + * Returns the set of interfaces configured on the given port.
39 + *
40 + * @param port connect point
41 + * @return set of interfaces
42 + */
43 + Set<Interface> getInterfacesByPort(ConnectPoint port);
44 +
45 + /**
46 + * Returns the set of interfaces with the given IP address.
47 + *
48 + * @param ip IP address
49 + * @return set of interfaces
50 + */
51 + Set<Interface> getInterfacesByIp(IpAddress ip);
52 +
53 + /**
54 + * Returns the set of interfaces in the given VLAN.
55 + *
56 + * @param vlan VLAN ID of the interfaces
57 + * @return set of interfaces
58 + */
59 + Set<Interface> getInterfacesByVlan(VlanId vlan);
60 +
61 + /**
62 + * Returns an interface that has an address that is in the same subnet as
63 + * the given IP address.
64 + *
65 + * @param ip IP address to find matching subnet interface for
66 + * @return interface
67 + */
68 + Interface getMatchingInterface(IpAddress ip);
69 +}
...@@ -28,7 +28,7 @@ import org.onosproject.incubator.net.config.NetworkConfigRegistry; ...@@ -28,7 +28,7 @@ import org.onosproject.incubator.net.config.NetworkConfigRegistry;
28 import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; 28 import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
29 import org.onosproject.incubator.net.config.basics.BasicHostConfig; 29 import org.onosproject.incubator.net.config.basics.BasicHostConfig;
30 import org.onosproject.incubator.net.config.basics.BasicLinkConfig; 30 import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
31 -import org.onosproject.incubator.net.config.basics.BasicPortConfig; 31 +import org.onosproject.incubator.net.config.basics.InterfaceConfig;
32 import org.onosproject.incubator.net.config.basics.OpticalPortConfig; 32 import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
33 import org.onosproject.incubator.net.config.basics.SubjectFactories; 33 import org.onosproject.incubator.net.config.basics.SubjectFactories;
34 import org.onosproject.incubator.net.domain.IntentDomainConfig; 34 import org.onosproject.incubator.net.domain.IntentDomainConfig;
...@@ -61,12 +61,12 @@ public class BasicNetworkConfigs { ...@@ -61,12 +61,12 @@ public class BasicNetworkConfigs {
61 return new BasicDeviceConfig(); 61 return new BasicDeviceConfig();
62 } 62 }
63 }, 63 },
64 - new ConfigFactory<ConnectPoint, BasicPortConfig>(CONNECT_POINT_SUBJECT_FACTORY, 64 + new ConfigFactory<ConnectPoint, InterfaceConfig>(CONNECT_POINT_SUBJECT_FACTORY,
65 - BasicPortConfig.class, 65 + InterfaceConfig.class,
66 - "basic") { 66 + "interfaces") {
67 @Override 67 @Override
68 - public BasicPortConfig createConfig() { 68 + public InterfaceConfig createConfig() {
69 - return new BasicPortConfig(); 69 + return new InterfaceConfig();
70 } 70 }
71 }, 71 },
72 new ConfigFactory<HostId, BasicHostConfig>(HOST_SUBJECT_FACTORY, 72 new ConfigFactory<HostId, BasicHostConfig>(HOST_SUBJECT_FACTORY,
......
1 +/*
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 +
17 +package org.onosproject.incubator.net.intf.impl;
18 +
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.NetworkConfigEvent;
30 +import org.onosproject.incubator.net.config.NetworkConfigListener;
31 +import org.onosproject.incubator.net.config.NetworkConfigService;
32 +import org.onosproject.incubator.net.config.basics.InterfaceConfig;
33 +import org.onosproject.incubator.net.intf.Interface;
34 +import org.onosproject.incubator.net.intf.InterfaceService;
35 +import org.onosproject.net.ConnectPoint;
36 +import org.onosproject.net.Device;
37 +import org.onosproject.net.Port;
38 +import org.onosproject.net.device.DeviceService;
39 +import org.slf4j.Logger;
40 +import org.slf4j.LoggerFactory;
41 +
42 +import java.util.Map;
43 +import java.util.Optional;
44 +import java.util.Set;
45 +
46 +import static java.util.stream.Collectors.collectingAndThen;
47 +import static java.util.stream.Collectors.toSet;
48 +
49 +/**
50 + * Manages the inventory of interfaces in the system.
51 + */
52 +@Service
53 +@Component(immediate = true)
54 +public 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 +}