blob: 4cfc1d664eb2b9d04afb8168ed9a20b309e1c7dd [file] [log] [blame]
Claudine Chiu30a8a2a2016-07-14 17:09:04 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Claudine Chiu30a8a2a2016-07-14 17:09:04 -04003 *
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.incubator.net.virtual.impl;
18
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
yoonseonc6a69272017-01-12 18:22:20 -080022import org.onosproject.incubator.net.virtual.NetworkId;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040023import org.onosproject.incubator.net.virtual.VirtualHost;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040024import org.onosproject.incubator.net.virtual.VirtualNetworkService;
yoonseonc6a69272017-01-12 18:22:20 -080025import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.host.HostEvent;
31import org.onosproject.net.host.HostListener;
32import org.onosproject.net.host.HostService;
33
34import java.util.Collection;
35import java.util.Objects;
36import java.util.Optional;
37import java.util.Set;
38import java.util.function.Predicate;
39import java.util.stream.Collectors;
40
41import static com.google.common.base.Preconditions.checkNotNull;
42
43/**
44 * Host service implementation built on the virtual network service.
45 */
yoonseon214963b2016-11-21 15:41:07 -080046public class VirtualNetworkHostManager
yoonseonc6a69272017-01-12 18:22:20 -080047 extends AbstractVirtualListenerManager<HostEvent, HostListener>
48 implements HostService {
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040049
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040050 private static final String HOST_NULL = "Host ID cannot be null";
51
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040052 /**
53 * Creates a new virtual network host service object.
54 *
55 * @param virtualNetworkManager virtual network manager service
yoonseonc6a69272017-01-12 18:22:20 -080056 * @param networkId a virtual network identifier
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040057 */
yoonseon214963b2016-11-21 15:41:07 -080058 public VirtualNetworkHostManager(VirtualNetworkService virtualNetworkManager,
yoonseonc6a69272017-01-12 18:22:20 -080059 NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090060 super(virtualNetworkManager, networkId, HostEvent.class);
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040061 }
62
63
64 @Override
65 public int getHostCount() {
yoonseonc6a69272017-01-12 18:22:20 -080066 return manager.getVirtualHosts(this.networkId()).size();
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040067 }
68
69 @Override
70 public Iterable<Host> getHosts() {
71 return getHostsColl();
72 }
73
74 @Override
75 public Host getHost(HostId hostId) {
76 checkNotNull(hostId, HOST_NULL);
yoonseon214963b2016-11-21 15:41:07 -080077 Optional<VirtualHost> foundHost =
yoonseonc6a69272017-01-12 18:22:20 -080078 manager.getVirtualHosts(this.networkId())
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040079 .stream()
80 .filter(host -> hostId.equals(host.id()))
81 .findFirst();
82 if (foundHost.isPresent()) {
83 return foundHost.get();
84 }
85 return null;
86 }
87
88 /**
89 * Gets a collection of virtual hosts.
90 *
91 * @return collection of virtual hosts.
92 */
93 private Collection<Host> getHostsColl() {
yoonseonc6a69272017-01-12 18:22:20 -080094 return manager.getVirtualHosts(this.networkId())
yoonseon214963b2016-11-21 15:41:07 -080095 .stream().collect(Collectors.toSet());
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040096 }
97
98 /**
99 * Filters specified collection.
100 *
101 * @param collection collection of hosts to filter
102 * @param predicate condition to filter on
103 * @return collection of virtual hosts that satisfy the filter condition
104 */
105 private Set<Host> filter(Collection<Host> collection, Predicate<Host> predicate) {
106 return collection.stream().filter(predicate).collect(Collectors.toSet());
107 }
108
109 @Override
110 public Set<Host> getHostsByVlan(VlanId vlanId) {
111 checkNotNull(vlanId, "VLAN identifier cannot be null");
112 return filter(getHostsColl(), host -> Objects.equals(host.vlan(), vlanId));
113 }
114
115 @Override
116 public Set<Host> getHostsByMac(MacAddress mac) {
117 checkNotNull(mac, "MAC address cannot be null");
118 return filter(getHostsColl(), host -> Objects.equals(host.mac(), mac));
119 }
120
121 @Override
122 public Set<Host> getHostsByIp(IpAddress ip) {
123 checkNotNull(ip, "IP address cannot be null");
124 return filter(getHostsColl(), host -> host.ipAddresses().contains(ip));
125 }
126
127 @Override
128 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
129 checkNotNull(connectPoint, "Connect point cannot be null");
130 return filter(getHostsColl(), host -> host.location().equals(connectPoint));
131 }
132
133 @Override
134 public Set<Host> getConnectedHosts(DeviceId deviceId) {
135 checkNotNull(deviceId, "Device identifier cannot be null");
136 return filter(getHostsColl(), host -> host.location().deviceId().equals(deviceId));
137 }
138
139 @Override
140 public void startMonitoringIp(IpAddress ip) {
141 //TODO check what needs to be done here
142 }
143
144 @Override
145 public void stopMonitoringIp(IpAddress ip) {
146 //TODO check what needs to be done here
147 }
148
149 @Override
150 public void requestMac(IpAddress ip) {
151 //TODO check what needs to be done here
152 }
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400153}