blob: a02ce274f0624dc8b71a06d4744523b18ce2c191 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070019package org.onlab.onos.sdnip;
20
21import static com.google.common.base.Preconditions.checkNotNull;
22
23import java.util.Set;
24
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070027import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070028import org.onlab.onos.net.host.PortAddresses;
29import org.onlab.onos.sdnip.config.Interface;
30import org.onlab.packet.IpAddress;
31
32import com.google.common.collect.Sets;
33
Jonathan Hartdc711bd2014-10-15 11:24:23 -070034/**
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070035 * Provides InterfaceService using PortAddresses data from the HostService.
Jonathan Hartdc711bd2014-10-15 11:24:23 -070036 */
Jonathan Hartce37f6d2014-10-20 10:25:03 -070037public class HostToInterfaceAdaptor implements InterfaceService {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070038
39 private final HostService hostService;
40
Jonathan Hartce37f6d2014-10-20 10:25:03 -070041 public HostToInterfaceAdaptor(HostService hostService) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070042 this.hostService = checkNotNull(hostService);
43 }
44
45 @Override
46 public Set<Interface> getInterfaces() {
47 Set<PortAddresses> addresses = hostService.getAddressBindings();
48 Set<Interface> interfaces = Sets.newHashSetWithExpectedSize(addresses.size());
49 for (PortAddresses a : addresses) {
50 interfaces.add(new Interface(a));
51 }
52 return interfaces;
53 }
54
55 @Override
56 public Interface getInterface(ConnectPoint connectPoint) {
57 checkNotNull(connectPoint);
58
59 PortAddresses portAddresses =
60 hostService.getAddressBindingsForPort(connectPoint);
61
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070062 if (!portAddresses.ipAddresses().isEmpty()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070063 return new Interface(portAddresses);
64 }
65
66 return null;
67 }
68
69 @Override
70 public Interface getMatchingInterface(IpAddress ipAddress) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070071 checkNotNull(ipAddress);
72
73 for (PortAddresses portAddresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070074 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
75 if (ia.subnetAddress().contains(ipAddress)) {
Jonathan Hart4c2b15e2014-10-20 13:10:56 -070076 return new Interface(portAddresses);
77 }
78 }
79 }
80
81 return null;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070082 }
83
84}