blob: 6642c5a1d593649ee79af08401f54140fde97353 [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;
26import 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 */
46public class VirtualNetworkHostService extends AbstractListenerManager<HostEvent, HostListener>
47 implements HostService, VnetService {
48
49 private static final String NETWORK_NULL = "Network ID cannot be null";
50 private static final String HOST_NULL = "Host ID cannot be null";
51
52 private final VirtualNetwork network;
53 private final VirtualNetworkService manager;
54
55 /**
56 * Creates a new virtual network host service object.
57 *
58 * @param virtualNetworkManager virtual network manager service
59 * @param network virtual network
60 */
61 public VirtualNetworkHostService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
62 checkNotNull(network, NETWORK_NULL);
63 this.network = network;
64 this.manager = virtualNetworkManager;
65 }
66
67
68 @Override
69 public int getHostCount() {
70 return manager.getVirtualHosts(this.network.id()).size();
71 }
72
73 @Override
74 public Iterable<Host> getHosts() {
75 return getHostsColl();
76 }
77
78 @Override
79 public Host getHost(HostId hostId) {
80 checkNotNull(hostId, HOST_NULL);
81 Optional<VirtualHost> foundHost = manager.getVirtualHosts(this.network.id())
82 .stream()
83 .filter(host -> hostId.equals(host.id()))
84 .findFirst();
85 if (foundHost.isPresent()) {
86 return foundHost.get();
87 }
88 return null;
89 }
90
91 /**
92 * Gets a collection of virtual hosts.
93 *
94 * @return collection of virtual hosts.
95 */
96 private Collection<Host> getHostsColl() {
97 return manager.getVirtualHosts(this.network.id()).stream().collect(Collectors.toSet());
98 }
99
100 /**
101 * Filters specified collection.
102 *
103 * @param collection collection of hosts to filter
104 * @param predicate condition to filter on
105 * @return collection of virtual hosts that satisfy the filter condition
106 */
107 private Set<Host> filter(Collection<Host> collection, Predicate<Host> predicate) {
108 return collection.stream().filter(predicate).collect(Collectors.toSet());
109 }
110
111 @Override
112 public Set<Host> getHostsByVlan(VlanId vlanId) {
113 checkNotNull(vlanId, "VLAN identifier cannot be null");
114 return filter(getHostsColl(), host -> Objects.equals(host.vlan(), vlanId));
115 }
116
117 @Override
118 public Set<Host> getHostsByMac(MacAddress mac) {
119 checkNotNull(mac, "MAC address cannot be null");
120 return filter(getHostsColl(), host -> Objects.equals(host.mac(), mac));
121 }
122
123 @Override
124 public Set<Host> getHostsByIp(IpAddress ip) {
125 checkNotNull(ip, "IP address cannot be null");
126 return filter(getHostsColl(), host -> host.ipAddresses().contains(ip));
127 }
128
129 @Override
130 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
131 checkNotNull(connectPoint, "Connect point cannot be null");
132 return filter(getHostsColl(), host -> host.location().equals(connectPoint));
133 }
134
135 @Override
136 public Set<Host> getConnectedHosts(DeviceId deviceId) {
137 checkNotNull(deviceId, "Device identifier cannot be null");
138 return filter(getHostsColl(), host -> host.location().deviceId().equals(deviceId));
139 }
140
141 @Override
142 public void startMonitoringIp(IpAddress ip) {
143 //TODO check what needs to be done here
144 }
145
146 @Override
147 public void stopMonitoringIp(IpAddress ip) {
148 //TODO check what needs to be done here
149 }
150
151 @Override
152 public void requestMac(IpAddress ip) {
153 //TODO check what needs to be done here
154 }
155
156 @Override
157 public VirtualNetwork network() {
158 return network;
159 }
160}