78eecb86aede9152cdde7d4bdf77e65aa326a63d
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
4  * 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
7  *
8  *     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.
15  */
16 package org.onosproject.routing.config.impl;
17
18 import com.google.common.collect.Sets;
19 import org.onlab.packet.IpAddress;
20 import org.onosproject.net.ConnectPoint;
21 import org.onosproject.net.host.HostService;
22 import org.onosproject.net.host.InterfaceIpAddress;
23 import org.onosproject.net.host.PortAddresses;
24 import org.onosproject.routing.config.Interface;
25
26 import java.util.Set;
27
28 import static com.google.common.base.Preconditions.checkNotNull;
29
30 /**
31  * Adapts PortAddresses data from the HostService into Interface data used by
32  * the routing module.
33  */
34 public class HostToInterfaceAdaptor {
35
36     private final HostService hostService;
37
38     public HostToInterfaceAdaptor(HostService hostService) {
39         this.hostService = checkNotNull(hostService);
40     }
41
42     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
51     public Interface getInterface(ConnectPoint connectPoint) {
52         checkNotNull(connectPoint);
53
54         Set<PortAddresses> portAddresses =
55                 hostService.getAddressBindingsForPort(connectPoint);
56
57         for (PortAddresses addresses : portAddresses) {
58             if (addresses.connectPoint().equals(connectPoint)) {
59                 return new Interface(addresses);
60             }
61         }
62
63         return null;
64     }
65
66     public Interface getInterface(IpAddress ip) {
67         Set<PortAddresses> portAddresses = hostService.getAddressBindings();
68
69         for (PortAddresses portAddress : portAddresses) {
70             for (InterfaceIpAddress portIp : portAddress.ipAddresses()) {
71                 if (portIp.ipAddress().equals(ip)) {
72                     return new Interface(portAddress);
73                 }
74             }
75         }
76
77         return null;
78     }
79
80     public Interface getMatchingInterface(IpAddress ipAddress) {
81         checkNotNull(ipAddress);
82
83         for (PortAddresses portAddresses : hostService.getAddressBindings()) {
84             for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
85                 if (ia.subnetAddress().contains(ipAddress)) {
86                     return new Interface(portAddresses);
87                 }
88             }
89         }
90
91         return null;
92     }
93
94 }