2 * Copyright 2014-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.
16 package org.onosproject.routing.config.impl;
18 import com.google.common.collect.Sets;
19 import org.onlab.packet.IpAddress;
20 import org.onosproject.net.ConnectPoint;
21 import org.onosproject.net.host.HostService;
22 import org.onosproject.net.host.InterfaceIpAddress;
23 import org.onosproject.net.host.PortAddresses;
24 import org.onosproject.routing.config.Interface;
28 import static com.google.common.base.Preconditions.checkNotNull;
31 * Adapts PortAddresses data from the HostService into Interface data used by
34 public class HostToInterfaceAdaptor {
36 private final HostService hostService;
38 public HostToInterfaceAdaptor(HostService hostService) {
39 this.hostService = checkNotNull(hostService);
42 public Set<Interface> getInterfaces() {
43 Set<PortAddresses> addresses = hostService.getAddressBindings();
44 Set<Interface> interfaces = Sets.newHashSetWithExpectedSize(addresses.size());
45 for (PortAddresses a : addresses) {
46 interfaces.add(new Interface(a));
51 public Interface getInterface(ConnectPoint connectPoint) {
52 checkNotNull(connectPoint);
54 Set<PortAddresses> portAddresses =
55 hostService.getAddressBindingsForPort(connectPoint);
57 for (PortAddresses addresses : portAddresses) {
58 if (addresses.connectPoint().equals(connectPoint)) {
59 return new Interface(addresses);
66 public Interface getInterface(IpAddress ip) {
67 Set<PortAddresses> portAddresses = hostService.getAddressBindings();
69 for (PortAddresses portAddress : portAddresses) {
70 for (InterfaceIpAddress portIp : portAddress.ipAddresses()) {
71 if (portIp.ipAddress().equals(ip)) {
72 return new Interface(portAddress);
80 public Interface getMatchingInterface(IpAddress ipAddress) {
81 checkNotNull(ipAddress);
83 for (PortAddresses portAddresses : hostService.getAddressBindings()) {
84 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
85 if (ia.subnetAddress().contains(ipAddress)) {
86 return new Interface(portAddresses);