blob: 5cd3e319422a423581085efdca2d1dc76af61163 [file] [log] [blame]
Brian Stanke86914282016-05-25 15:36:50 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke86914282016-05-25 15:36:50 -04003 *
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
yoonseonc6a69272017-01-12 18:22:20 -080019import org.onosproject.incubator.net.virtual.NetworkId;
Brian Stanke86914282016-05-25 15:36:50 -040020import org.onosproject.incubator.net.virtual.VirtualLink;
Brian Stanke86914282016-05-25 15:36:50 -040021import org.onosproject.incubator.net.virtual.VirtualNetworkService;
yoonseonc6a69272017-01-12 18:22:20 -080022import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
Brian Stanke86914282016-05-25 15:36:50 -040023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Link;
26import org.onosproject.net.link.LinkEvent;
27import org.onosproject.net.link.LinkListener;
28import org.onosproject.net.link.LinkService;
29
30import java.util.Optional;
31import java.util.Set;
32import java.util.stream.Collectors;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Link service implementation built on the virtual network service.
38 */
yoonseon214963b2016-11-21 15:41:07 -080039public class VirtualNetworkLinkManager
yoonseonc6a69272017-01-12 18:22:20 -080040 extends AbstractVirtualListenerManager<LinkEvent, LinkListener>
41 implements LinkService {
Brian Stanke86914282016-05-25 15:36:50 -040042
Brian Stanke86914282016-05-25 15:36:50 -040043 private static final String DEVICE_NULL = "Device cannot be null";
44 private static final String CONNECT_POINT_NULL = "Connect point cannot be null";
45
Brian Stanke86914282016-05-25 15:36:50 -040046 /**
47 * Creates a new VirtualNetworkLinkService object.
48 *
49 * @param virtualNetworkManager virtual network manager service
yoonseonc6a69272017-01-12 18:22:20 -080050 * @param networkId a virtual networkIdentifier
Brian Stanke86914282016-05-25 15:36:50 -040051 */
yoonseon214963b2016-11-21 15:41:07 -080052 public VirtualNetworkLinkManager(VirtualNetworkService virtualNetworkManager,
yoonseonc6a69272017-01-12 18:22:20 -080053 NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090054 super(virtualNetworkManager, networkId, LinkEvent.class);
Brian Stanke86914282016-05-25 15:36:50 -040055 }
56
57 @Override
58 public int getLinkCount() {
yoonseonc6a69272017-01-12 18:22:20 -080059 return manager.getVirtualLinks(this.networkId()).size();
Brian Stanke86914282016-05-25 15:36:50 -040060 }
61
62 @Override
63 public Iterable<Link> getLinks() {
yoonseonc6a69272017-01-12 18:22:20 -080064 return manager.getVirtualLinks(this.networkId())
yoonseon214963b2016-11-21 15:41:07 -080065 .stream().collect(Collectors.toSet());
Brian Stanke86914282016-05-25 15:36:50 -040066 }
67
68 @Override
69 public Iterable<Link> getActiveLinks() {
70
yoonseonc6a69272017-01-12 18:22:20 -080071 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -040072 .stream()
73 .filter(link -> (link.state().equals(Link.State.ACTIVE)))
74 .collect(Collectors.toSet());
75 }
76
77 @Override
78 public Set<Link> getDeviceLinks(DeviceId deviceId) {
79 checkNotNull(deviceId, DEVICE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -080080 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -040081 .stream()
82 .filter(link -> (deviceId.equals(link.src().elementId()) ||
83 deviceId.equals(link.dst().elementId())))
84 .collect(Collectors.toSet());
85 }
86
87 @Override
88 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
89 checkNotNull(deviceId, DEVICE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -080090 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -040091 .stream()
92 .filter(link -> (deviceId.equals(link.dst().elementId())))
93 .collect(Collectors.toSet());
94 }
95
96 @Override
97 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
98 checkNotNull(deviceId, DEVICE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -080099 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -0400100 .stream()
101 .filter(link -> (deviceId.equals(link.src().elementId())))
102 .collect(Collectors.toSet());
103 }
104
105 @Override
106 public Set<Link> getLinks(ConnectPoint connectPoint) {
107 checkNotNull(connectPoint, CONNECT_POINT_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800108 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -0400109 .stream()
110 .filter(link -> (connectPoint.equals(link.src()) ||
111 connectPoint.equals(link.dst())))
112 .collect(Collectors.toSet());
113 }
114
115 @Override
116 public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
117 checkNotNull(connectPoint, CONNECT_POINT_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800118 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -0400119 .stream()
120 .filter(link -> (connectPoint.equals(link.dst())))
121 .collect(Collectors.toSet());
122 }
123
124 @Override
125 public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
126 checkNotNull(connectPoint, CONNECT_POINT_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800127 return manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -0400128 .stream()
129 .filter(link -> (connectPoint.equals(link.src())))
130 .collect(Collectors.toSet());
131 }
132
133 @Override
134 public Link getLink(ConnectPoint src, ConnectPoint dst) {
135 checkNotNull(src, CONNECT_POINT_NULL);
136 checkNotNull(dst, CONNECT_POINT_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800137 Optional<VirtualLink> foundLink = manager.getVirtualLinks(this.networkId())
Brian Stanke86914282016-05-25 15:36:50 -0400138 .stream()
139 .filter(link -> (src.equals(link.src()) &&
140 dst.equals(link.dst())))
141 .findFirst();
142
143 if (foundLink.isPresent()) {
144 return foundLink.get();
145 }
146 return null;
147 }
148}