blob: 2ba68d1add46d5f39dbbeedfbdadceaea9d2ee71 [file] [log] [blame]
Seyeon Jeong8d3cad22020-02-28 01:17:34 -08001/*
2 * Copyright 2020-present Open Networking Foundation
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
17package org.onosproject.t3.api;
18
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.Host;
24import org.onosproject.net.HostId;
25
26import java.util.Set;
27import java.util.stream.Collectors;
28
29/**
30 * Represents Network Information Base (NIB) for hosts
31 * and supports alternative functions to
32 * {@link org.onosproject.net.host.HostService} for offline data.
33 */
Seyeon Jeongac129562020-02-28 01:17:34 -080034public class HostNib extends AbstractNib {
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080035
36 // TODO with method optimization, store into subdivided structures at the first load
37 private Set<Host> hosts;
38
39 // use the singleton helper to create the instance
40 protected HostNib() {
41 }
42
43 /**
44 * Sets a set of hosts.
45 *
46 * @param hosts host set
47 */
48 public void setHosts(Set<Host> hosts) {
49 this.hosts = hosts;
50 }
51
52 /**
53 * Returns the set of hosts.
54 *
55 * @return host set
56 */
57 public Set<Host> getHosts() {
58 return ImmutableSet.copyOf(hosts);
59 }
60
61 /**
62 * Returns the host with the specified identifier.
63 *
64 * @param hostId host identifier
65 * @return host or null if one with the given identifier is not known
66 */
67 public Host getHost(HostId hostId) {
68 return hosts.stream()
69 .filter(host -> host.id().equals(hostId))
70 .findFirst().orElse(null);
71 }
72
73 /**
74 * Returns the set of hosts whose most recent location is the specified
75 * connection point.
76 *
77 * @param connectPoint connection point
78 * @return set of hosts connected to the connection point
79 */
80 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
81 // TODO extend this method to support matching on auxLocations as well
82 Set<Host> connectedHosts = hosts.stream()
83 .filter(host -> host.locations().contains(connectPoint))
84 .collect(Collectors.toSet());
85 return connectedHosts != null ? ImmutableSet.copyOf(connectedHosts) : ImmutableSet.of();
86 }
87
88 /**
89 * Returns the set of hosts that have the specified IP address.
90 *
91 * @param ip ip address
92 * @return set of hosts with the given IP
93 */
94 public Set<Host> getHostsByIp(IpAddress ip) {
95 Set<Host> hostsByIp = hosts.stream()
96 .filter(host -> host.ipAddresses().contains(ip))
97 .collect(Collectors.toSet());
98 return hostsByIp != null ? ImmutableSet.copyOf(hostsByIp) : ImmutableSet.of();
99 }
100
101 /**
102 * Returns the set of hosts that have the specified MAC address.
103 *
104 * @param mac mac address
105 * @return set of hosts with the given mac
106 */
107 public Set<Host> getHostsByMac(MacAddress mac) {
108 Set<Host> hostsByMac = hosts.stream()
109 .filter(host -> host.mac().equals(mac))
110 .collect(Collectors.toSet());
111 return hostsByMac != null ? ImmutableSet.copyOf(hostsByMac) : ImmutableSet.of();
112 }
113
114 /**
115 * Returns the singleton instance of hosts NIB.
116 *
117 * @return instance of hosts NIB
118 */
119 public static HostNib getInstance() {
120 return HostNib.SingletonHelper.INSTANCE;
121 }
122
123 private static class SingletonHelper {
124 private static final HostNib INSTANCE = new HostNib();
125 }
126
127}