Brian Stanke | 8691428 | 2016-05-25 15:36:50 -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.onosproject.event.AbstractListenerManager; |
| 20 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 21 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 22 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.link.LinkEvent; |
| 27 | import org.onosproject.net.link.LinkListener; |
| 28 | import org.onosproject.net.link.LinkService; |
| 29 | |
| 30 | import java.util.Optional; |
| 31 | import java.util.Set; |
| 32 | import java.util.stream.Collectors; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * Link service implementation built on the virtual network service. |
| 38 | */ |
| 39 | public class VirtualNetworkLinkService extends AbstractListenerManager<LinkEvent, LinkListener> |
| 40 | implements LinkService, VnetService { |
| 41 | |
| 42 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 43 | private static final String DEVICE_NULL = "Device cannot be null"; |
| 44 | private static final String CONNECT_POINT_NULL = "Connect point cannot be null"; |
| 45 | |
| 46 | private final VirtualNetwork network; |
| 47 | private final VirtualNetworkService manager; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new VirtualNetworkLinkService object. |
| 51 | * |
| 52 | * @param virtualNetworkManager virtual network manager service |
| 53 | * @param network virtual network |
| 54 | */ |
| 55 | public VirtualNetworkLinkService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) { |
| 56 | checkNotNull(network, NETWORK_NULL); |
| 57 | this.network = network; |
| 58 | this.manager = virtualNetworkManager; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public VirtualNetwork network() { |
| 63 | return network; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int getLinkCount() { |
| 68 | return manager.getVirtualLinks(this.network.id()).size(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Iterable<Link> getLinks() { |
| 73 | return manager.getVirtualLinks(this.network.id()).stream().collect(Collectors.toSet()); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Iterable<Link> getActiveLinks() { |
| 78 | |
| 79 | return manager.getVirtualLinks(this.network.id()) |
| 80 | .stream() |
| 81 | .filter(link -> (link.state().equals(Link.State.ACTIVE))) |
| 82 | .collect(Collectors.toSet()); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 87 | checkNotNull(deviceId, DEVICE_NULL); |
| 88 | return manager.getVirtualLinks(this.network.id()) |
| 89 | .stream() |
| 90 | .filter(link -> (deviceId.equals(link.src().elementId()) || |
| 91 | deviceId.equals(link.dst().elementId()))) |
| 92 | .collect(Collectors.toSet()); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 97 | checkNotNull(deviceId, DEVICE_NULL); |
| 98 | return manager.getVirtualLinks(this.network.id()) |
| 99 | .stream() |
| 100 | .filter(link -> (deviceId.equals(link.dst().elementId()))) |
| 101 | .collect(Collectors.toSet()); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
| 106 | checkNotNull(deviceId, DEVICE_NULL); |
| 107 | return manager.getVirtualLinks(this.network.id()) |
| 108 | .stream() |
| 109 | .filter(link -> (deviceId.equals(link.src().elementId()))) |
| 110 | .collect(Collectors.toSet()); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 115 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 116 | return manager.getVirtualLinks(this.network.id()) |
| 117 | .stream() |
| 118 | .filter(link -> (connectPoint.equals(link.src()) || |
| 119 | connectPoint.equals(link.dst()))) |
| 120 | .collect(Collectors.toSet()); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Set<Link> getEgressLinks(ConnectPoint connectPoint) { |
| 125 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 126 | return manager.getVirtualLinks(this.network.id()) |
| 127 | .stream() |
| 128 | .filter(link -> (connectPoint.equals(link.dst()))) |
| 129 | .collect(Collectors.toSet()); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Set<Link> getIngressLinks(ConnectPoint connectPoint) { |
| 134 | checkNotNull(connectPoint, CONNECT_POINT_NULL); |
| 135 | return manager.getVirtualLinks(this.network.id()) |
| 136 | .stream() |
| 137 | .filter(link -> (connectPoint.equals(link.src()))) |
| 138 | .collect(Collectors.toSet()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
| 143 | checkNotNull(src, CONNECT_POINT_NULL); |
| 144 | checkNotNull(dst, CONNECT_POINT_NULL); |
| 145 | Optional<VirtualLink> foundLink = manager.getVirtualLinks(this.network.id()) |
| 146 | .stream() |
| 147 | .filter(link -> (src.equals(link.src()) && |
| 148 | dst.equals(link.dst()))) |
| 149 | .findFirst(); |
| 150 | |
| 151 | if (foundLink.isPresent()) { |
| 152 | return foundLink.get(); |
| 153 | } |
| 154 | return null; |
| 155 | } |
| 156 | } |