blob: 604d12d143aa29c2d016df7cd32a2ffc7fd14224 [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
Jonathan Harta887ba82014-11-03 15:20:52 -080056 Set<PortAddresses> portAddresses =
Jonathan Hartdc711bd2014-10-15 11:24:23 -070057 hostService.getAddressBindingsForPort(connectPoint);
58
Jonathan Harta887ba82014-11-03 15:20:52 -080059 for (PortAddresses addresses : portAddresses) {
60 if (addresses.connectPoint().equals(connectPoint)) {
61 return new Interface(addresses);
62 }
Jonathan Hartdc711bd2014-10-15 11:24:23 -070063 }
64
65 return null;
66 }
67
68 @Override
69 public Interface getMatchingInterface(IpAddress ipAddress) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070070 checkNotNull(ipAddress);
71
72 for (PortAddresses portAddresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070073 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
74 if (ia.subnetAddress().contains(ipAddress)) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070075 return new Interface(portAddresses);
76 }
77 }
78 }
79
80 return null;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070081 }
82
83}