blob: 8fabe3907a52925776dbf9c354fe553f85a6109b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing.config.impl;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070017
Jonathan Hart90a02c22015-02-13 11:52:07 -080018import com.google.common.collect.Sets;
Pavlin Radoslavov2020eac2015-01-06 17:26:10 -080019import org.onlab.packet.IpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.host.HostService;
22import org.onosproject.net.host.InterfaceIpAddress;
23import org.onosproject.net.host.PortAddresses;
Jonathan Hart2da1e602015-02-18 19:09:24 -080024import org.onosproject.routing.config.Interface;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025
Jonathan Hart90a02c22015-02-13 11:52:07 -080026import java.util.Set;
27
28import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070029
Jonathan Hartdc711bd2014-10-15 11:24:23 -070030/**
Jonathan Hart90a02c22015-02-13 11:52:07 -080031 * Adapts PortAddresses data from the HostService into Interface data used by
32 * the routing module.
Jonathan Hartdc711bd2014-10-15 11:24:23 -070033 */
Jonathan Hart90a02c22015-02-13 11:52:07 -080034public class HostToInterfaceAdaptor {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070035
36 private final HostService hostService;
37
Jonathan Hartce37f6d2014-10-20 10:25:03 -070038 public HostToInterfaceAdaptor(HostService hostService) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070039 this.hostService = checkNotNull(hostService);
40 }
41
Jonathan Hartdc711bd2014-10-15 11:24:23 -070042 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));
47 }
48 return interfaces;
49 }
50
Jonathan Hartdc711bd2014-10-15 11:24:23 -070051 public Interface getInterface(ConnectPoint connectPoint) {
52 checkNotNull(connectPoint);
53
Jonathan Harta887ba82014-11-03 15:20:52 -080054 Set<PortAddresses> portAddresses =
Jonathan Hartdc711bd2014-10-15 11:24:23 -070055 hostService.getAddressBindingsForPort(connectPoint);
56
Jonathan Harta887ba82014-11-03 15:20:52 -080057 for (PortAddresses addresses : portAddresses) {
58 if (addresses.connectPoint().equals(connectPoint)) {
59 return new Interface(addresses);
60 }
Jonathan Hartdc711bd2014-10-15 11:24:23 -070061 }
62
63 return null;
64 }
65
Jonathan Hartdc711bd2014-10-15 11:24:23 -070066 public Interface getMatchingInterface(IpAddress ipAddress) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070067 checkNotNull(ipAddress);
68
69 for (PortAddresses portAddresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070070 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
71 if (ia.subnetAddress().contains(ipAddress)) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070072 return new Interface(portAddresses);
73 }
74 }
75 }
76
77 return null;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070078 }
79
80}