blob: f9d69513ea79d65a3fee3c2c8668bd845f5ad5d3 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 Hartdc711bd2014-10-15 11:24:23 -070016package org.onlab.onos.sdnip;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Set;
21
Jonathan Hartdc711bd2014-10-15 11:24:23 -070022import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070024import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025import org.onlab.onos.net.host.PortAddresses;
26import org.onlab.onos.sdnip.config.Interface;
27import org.onlab.packet.IpAddress;
28
29import com.google.common.collect.Sets;
30
Jonathan Hartdc711bd2014-10-15 11:24:23 -070031/**
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070032 * Provides InterfaceService using PortAddresses data from the HostService.
Jonathan Hartdc711bd2014-10-15 11:24:23 -070033 */
Jonathan Hartce37f6d2014-10-20 10:25:03 -070034public class HostToInterfaceAdaptor implements InterfaceService {
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
42 @Override
43 public Set<Interface> getInterfaces() {
44 Set<PortAddresses> addresses = hostService.getAddressBindings();
45 Set<Interface> interfaces = Sets.newHashSetWithExpectedSize(addresses.size());
46 for (PortAddresses a : addresses) {
47 interfaces.add(new Interface(a));
48 }
49 return interfaces;
50 }
51
52 @Override
53 public Interface getInterface(ConnectPoint connectPoint) {
54 checkNotNull(connectPoint);
55
56 PortAddresses portAddresses =
57 hostService.getAddressBindingsForPort(connectPoint);
58
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070059 if (!portAddresses.ipAddresses().isEmpty()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070060 return new Interface(portAddresses);
61 }
62
63 return null;
64 }
65
66 @Override
67 public Interface getMatchingInterface(IpAddress ipAddress) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070068 checkNotNull(ipAddress);
69
70 for (PortAddresses portAddresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070071 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
72 if (ia.subnetAddress().contains(ipAddress)) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070073 return new Interface(portAddresses);
74 }
75 }
76 }
77
78 return null;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070079 }
80
81}