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