blob: 92c165f7e087f5542b3381fc22a7e3c4abae1440 [file] [log] [blame]
Claudine Chiu30a8a2a2016-07-14 17:09:04 -04001/*
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
22import org.onosproject.event.AbstractListenerManager;
23import org.onosproject.incubator.net.virtual.VirtualHost;
24import org.onosproject.incubator.net.virtual.VirtualNetwork;
25import org.onosproject.incubator.net.virtual.VirtualNetworkService;
yoonseon214963b2016-11-21 15:41:07 -080026import org.onosproject.incubator.net.virtual.VnetService;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Host;
30import org.onosproject.net.HostId;
31import org.onosproject.net.host.HostEvent;
32import org.onosproject.net.host.HostListener;
33import org.onosproject.net.host.HostService;
34
35import java.util.Collection;
36import java.util.Objects;
37import java.util.Optional;
38import java.util.Set;
39import java.util.function.Predicate;
40import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43
44/**
45 * Host service implementation built on the virtual network service.
46 */
yoonseon214963b2016-11-21 15:41:07 -080047public class VirtualNetworkHostManager
48 extends AbstractListenerManager<HostEvent, HostListener>
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040049 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 */
yoonseon214963b2016-11-21 15:41:07 -080063 public VirtualNetworkHostManager(VirtualNetworkService virtualNetworkManager,
64 VirtualNetwork network) {
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040065 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);
yoonseon214963b2016-11-21 15:41:07 -080084 Optional<VirtualHost> foundHost =
85 manager.getVirtualHosts(this.network.id())
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040086 .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() {
yoonseon214963b2016-11-21 15:41:07 -0800101 return manager.getVirtualHosts(this.network.id())
102 .stream().collect(Collectors.toSet());
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400103 }
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}