Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 19 | package org.onlab.onos.sdnip; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.ConnectPoint; |
| 26 | import org.onlab.onos.net.host.HostService; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 27 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.host.PortAddresses; |
| 29 | import org.onlab.onos.sdnip.config.Interface; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | |
| 32 | import com.google.common.collect.Sets; |
| 33 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 34 | /** |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 35 | * Provides InterfaceService using PortAddresses data from the HostService. |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 36 | */ |
Jonathan Hart | ce37f6d | 2014-10-20 10:25:03 -0700 | [diff] [blame] | 37 | public class HostToInterfaceAdaptor implements InterfaceService { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 38 | |
| 39 | private final HostService hostService; |
| 40 | |
Jonathan Hart | ce37f6d | 2014-10-20 10:25:03 -0700 | [diff] [blame] | 41 | public HostToInterfaceAdaptor(HostService hostService) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 42 | 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 Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 62 | if (!portAddresses.ipAddresses().isEmpty()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 63 | return new Interface(portAddresses); |
| 64 | } |
| 65 | |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Interface getMatchingInterface(IpAddress ipAddress) { |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 71 | checkNotNull(ipAddress); |
| 72 | |
| 73 | for (PortAddresses portAddresses : hostService.getAddressBindings()) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame^] | 74 | for (InterfaceIpAddress ia : portAddresses.ipAddresses()) { |
| 75 | if (ia.subnetAddress().contains(ipAddress)) { |
Jonathan Hart | 4c2b15e | 2014-10-20 13:10:56 -0700 | [diff] [blame] | 76 | return new Interface(portAddresses); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return null; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } |