Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.event.AbstractListenerManager; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualHost; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 26 | import org.onosproject.incubator.net.virtual.VnetService; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.host.HostEvent; |
| 32 | import org.onosproject.net.host.HostListener; |
| 33 | import org.onosproject.net.host.HostService; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.Objects; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | import java.util.function.Predicate; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | |
| 44 | /** |
| 45 | * Host service implementation built on the virtual network service. |
| 46 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 47 | public class VirtualNetworkHostManager |
| 48 | extends AbstractListenerManager<HostEvent, HostListener> |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 49 | implements HostService, VnetService { |
| 50 | |
| 51 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 52 | private static final String HOST_NULL = "Host ID cannot be null"; |
| 53 | |
| 54 | private final VirtualNetwork network; |
| 55 | private final VirtualNetworkService manager; |
| 56 | |
| 57 | /** |
| 58 | * Creates a new virtual network host service object. |
| 59 | * |
| 60 | * @param virtualNetworkManager virtual network manager service |
| 61 | * @param network virtual network |
| 62 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 63 | public VirtualNetworkHostManager(VirtualNetworkService virtualNetworkManager, |
| 64 | VirtualNetwork network) { |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 65 | checkNotNull(network, NETWORK_NULL); |
| 66 | this.network = network; |
| 67 | this.manager = virtualNetworkManager; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | @Override |
| 72 | public int getHostCount() { |
| 73 | return manager.getVirtualHosts(this.network.id()).size(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Iterable<Host> getHosts() { |
| 78 | return getHostsColl(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Host getHost(HostId hostId) { |
| 83 | checkNotNull(hostId, HOST_NULL); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 84 | Optional<VirtualHost> foundHost = |
| 85 | manager.getVirtualHosts(this.network.id()) |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 86 | .stream() |
| 87 | .filter(host -> hostId.equals(host.id())) |
| 88 | .findFirst(); |
| 89 | if (foundHost.isPresent()) { |
| 90 | return foundHost.get(); |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Gets a collection of virtual hosts. |
| 97 | * |
| 98 | * @return collection of virtual hosts. |
| 99 | */ |
| 100 | private Collection<Host> getHostsColl() { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 101 | return manager.getVirtualHosts(this.network.id()) |
| 102 | .stream().collect(Collectors.toSet()); |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Filters specified collection. |
| 107 | * |
| 108 | * @param collection collection of hosts to filter |
| 109 | * @param predicate condition to filter on |
| 110 | * @return collection of virtual hosts that satisfy the filter condition |
| 111 | */ |
| 112 | private Set<Host> filter(Collection<Host> collection, Predicate<Host> predicate) { |
| 113 | return collection.stream().filter(predicate).collect(Collectors.toSet()); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
| 118 | checkNotNull(vlanId, "VLAN identifier cannot be null"); |
| 119 | return filter(getHostsColl(), host -> Objects.equals(host.vlan(), vlanId)); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Set<Host> getHostsByMac(MacAddress mac) { |
| 124 | checkNotNull(mac, "MAC address cannot be null"); |
| 125 | return filter(getHostsColl(), host -> Objects.equals(host.mac(), mac)); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public Set<Host> getHostsByIp(IpAddress ip) { |
| 130 | checkNotNull(ip, "IP address cannot be null"); |
| 131 | return filter(getHostsColl(), host -> host.ipAddresses().contains(ip)); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 136 | checkNotNull(connectPoint, "Connect point cannot be null"); |
| 137 | return filter(getHostsColl(), host -> host.location().equals(connectPoint)); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Set<Host> getConnectedHosts(DeviceId deviceId) { |
| 142 | checkNotNull(deviceId, "Device identifier cannot be null"); |
| 143 | return filter(getHostsColl(), host -> host.location().deviceId().equals(deviceId)); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void startMonitoringIp(IpAddress ip) { |
| 148 | //TODO check what needs to be done here |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void stopMonitoringIp(IpAddress ip) { |
| 153 | //TODO check what needs to be done here |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void requestMac(IpAddress ip) { |
| 158 | //TODO check what needs to be done here |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public VirtualNetwork network() { |
| 163 | return network; |
| 164 | } |
| 165 | } |