blob: be08685aaf0a0e995d0dc0be38cb4f3e18f84e9e [file] [log] [blame]
Brian Stankefb61df42016-07-25 11:47:51 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stankefb61df42016-07-25 11:47:51 -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
yoonseon214963b2016-11-21 15:41:07 -080017package org.onosproject.incubator.net.virtual.impl.provider;
Brian Stankefb61df42016-07-25 11:47:51 -040018
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Brian Stankefb61df42016-07-25 11:47:51 -040025import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
yoonseon6b972c32016-12-06 16:45:03 -080026import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProvider;
27import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderRegistry;
28import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderService;
Brian Stankefb61df42016-07-25 11:47:51 -040029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Link;
31import org.onosproject.net.Path;
32import org.onosproject.net.link.LinkEvent;
33import org.onosproject.net.provider.AbstractProvider;
34import org.onosproject.net.topology.Topology;
35import org.onosproject.net.topology.TopologyCluster;
36import org.onosproject.net.topology.TopologyEvent;
37import org.onosproject.net.topology.TopologyListener;
38import org.onosproject.net.topology.TopologyService;
39import org.slf4j.Logger;
40
41import java.util.HashSet;
42import java.util.Set;
Thomas Vachuskac8187a02016-08-08 11:56:24 -070043import java.util.concurrent.ExecutorService;
Brian Stankefb61df42016-07-25 11:47:51 -040044
Thomas Vachuskac8187a02016-08-08 11:56:24 -070045import static java.util.concurrent.Executors.newSingleThreadExecutor;
46import static org.onlab.util.Tools.groupedThreads;
Brian Stankefb61df42016-07-25 11:47:51 -040047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Virtual network topology provider.
51 */
52@Component(immediate = true)
53@Service
yoonseon214963b2016-11-21 15:41:07 -080054public class DefaultVirtualNetworkProvider
55 extends AbstractProvider implements VirtualNetworkProvider {
Brian Stankefb61df42016-07-25 11:47:51 -040056
yoonseon214963b2016-11-21 15:41:07 -080057 private final Logger log = getLogger(DefaultVirtualNetworkProvider.class);
Brian Stankefb61df42016-07-25 11:47:51 -040058
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected VirtualNetworkProviderRegistry providerRegistry;
61
62 private VirtualNetworkProviderService providerService;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected TopologyService topologyService;
66
67 protected TopologyListener topologyListener = new InternalTopologyListener();
68
Thomas Vachuskac8187a02016-08-08 11:56:24 -070069 private ExecutorService executor;
70
Brian Stankefb61df42016-07-25 11:47:51 -040071 /**
72 * Default constructor.
73 */
yoonseon214963b2016-11-21 15:41:07 -080074 public DefaultVirtualNetworkProvider() {
Brian Stankefb61df42016-07-25 11:47:51 -040075 super(DefaultVirtualLink.PID);
76 }
77
78 @Activate
79 public void activate() {
Thomas Vachuskac8187a02016-08-08 11:56:24 -070080 executor = newSingleThreadExecutor(groupedThreads("onos/vnet", "provider", log));
Brian Stankefb61df42016-07-25 11:47:51 -040081 providerService = providerRegistry.register(this);
82 topologyService.addListener(topologyListener);
Brian Stankefb61df42016-07-25 11:47:51 -040083 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
88 topologyService.removeListener(topologyListener);
Thomas Vachuskad13e4a42016-08-08 14:39:51 -070089 executor.shutdownNow();
Thomas Vachuskac8187a02016-08-08 11:56:24 -070090 executor = null;
Brian Stankefb61df42016-07-25 11:47:51 -040091 providerRegistry.unregister(this);
92 providerService = null;
93 log.info("Stopped");
94 }
95
96 @Override
97 public boolean isTraversable(ConnectPoint src, ConnectPoint dst) {
98 final boolean[] foundSrc = new boolean[1];
99 final boolean[] foundDst = new boolean[1];
100 Topology topology = topologyService.currentTopology();
101 Set<Path> paths = topologyService.getPaths(topology, src.deviceId(), dst.deviceId());
102 paths.forEach(path -> {
103 foundDst[0] = false;
104 foundSrc[0] = false;
105 // Traverse the links in each path to determine if both the src and dst connection
106 // point are in the path, if so then this src/dst pair are traversable.
107 path.links().forEach(link -> {
108 if (link.src().equals(src)) {
109 foundSrc[0] = true;
110 }
111 if (link.dst().equals(dst)) {
112 foundDst[0] = true;
113 }
114 });
115 if (foundSrc[0] && foundDst[0]) {
116 return;
117 }
118 });
119 return foundSrc[0] && foundDst[0];
120 }
121
Brian Stankefb61df42016-07-25 11:47:51 -0400122 /**
123 * Returns a set of set of interconnected connect points in the default topology.
124 * The inner set represents the interconnected connect points, and the outerset
125 * represents separate clusters.
126 *
127 * @param topology the default topology
128 * @return set of set of interconnected connect points.
129 */
yoonseon214963b2016-11-21 15:41:07 -0800130 public Set<Set<ConnectPoint>> getConnectPoints(Topology topology) {
Brian Stankefb61df42016-07-25 11:47:51 -0400131 Set<Set<ConnectPoint>> clusters = new HashSet<>();
132 Set<TopologyCluster> topologyClusters = topologyService.getClusters(topology);
133 topologyClusters.forEach(topologyCluster -> {
134 Set<ConnectPoint> connectPointSet = new HashSet<>();
yoonseon214963b2016-11-21 15:41:07 -0800135 Set<Link> clusterLinks =
136 topologyService.getClusterLinks(topology, topologyCluster);
Brian Stankefb61df42016-07-25 11:47:51 -0400137 clusterLinks.forEach(link -> {
138 connectPointSet.add(link.src());
139 connectPointSet.add(link.dst());
140 });
141 if (!connectPointSet.isEmpty()) {
142 clusters.add(connectPointSet);
143 }
144 });
145 return clusters;
146 }
147
148 /**
149 * Topology event listener.
150 */
151 private class InternalTopologyListener implements TopologyListener {
152 @Override
153 public void event(TopologyEvent event) {
Thomas Vachuskac8187a02016-08-08 11:56:24 -0700154 // Perform processing off the listener thread.
yoonseon214963b2016-11-21 15:41:07 -0800155 executor.submit(() -> providerService
156 .topologyChanged(getConnectPoints(event.subject())));
Brian Stankefb61df42016-07-25 11:47:51 -0400157 }
158
159 @Override
160 public boolean isRelevant(TopologyEvent event) {
Thomas Vachuskac8187a02016-08-08 11:56:24 -0700161 return event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED &&
162 event.reasons().stream().anyMatch(reason -> reason instanceof LinkEvent);
Brian Stankefb61df42016-07-25 11:47:51 -0400163 }
164 }
165}