blob: 85b8a0ac0092f31ac46ab1ae6196911fbfd3baa2 [file] [log] [blame]
Brian Stankefb61df42016-07-25 11:47:51 -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.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;
25import org.onosproject.incubator.net.tunnel.TunnelId;
26import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
27import org.onosproject.incubator.net.virtual.NetworkId;
28import org.onosproject.incubator.net.virtual.VirtualNetworkProvider;
29import org.onosproject.incubator.net.virtual.VirtualNetworkProviderRegistry;
30import org.onosproject.incubator.net.virtual.VirtualNetworkProviderService;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.Link;
33import org.onosproject.net.Path;
34import org.onosproject.net.link.LinkEvent;
35import org.onosproject.net.provider.AbstractProvider;
36import org.onosproject.net.topology.Topology;
37import org.onosproject.net.topology.TopologyCluster;
38import org.onosproject.net.topology.TopologyEvent;
39import org.onosproject.net.topology.TopologyListener;
40import org.onosproject.net.topology.TopologyService;
41import org.slf4j.Logger;
42
43import java.util.HashSet;
44import java.util.Set;
Thomas Vachuskac8187a02016-08-08 11:56:24 -070045import java.util.concurrent.ExecutorService;
Brian Stankefb61df42016-07-25 11:47:51 -040046
Thomas Vachuskac8187a02016-08-08 11:56:24 -070047import static java.util.concurrent.Executors.newSingleThreadExecutor;
48import static org.onlab.util.Tools.groupedThreads;
Brian Stankefb61df42016-07-25 11:47:51 -040049import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Virtual network topology provider.
53 */
54@Component(immediate = true)
55@Service
56public class VirtualNetworkTopologyProvider extends AbstractProvider implements VirtualNetworkProvider {
57
58 private final Logger log = getLogger(VirtualNetworkTopologyProvider.class);
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected VirtualNetworkProviderRegistry providerRegistry;
62
63 private VirtualNetworkProviderService providerService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected TopologyService topologyService;
67
68 protected TopologyListener topologyListener = new InternalTopologyListener();
69
Thomas Vachuskac8187a02016-08-08 11:56:24 -070070 private ExecutorService executor;
71
Brian Stankefb61df42016-07-25 11:47:51 -040072 /**
73 * Default constructor.
74 */
75 public VirtualNetworkTopologyProvider() {
76 super(DefaultVirtualLink.PID);
77 }
78
79 @Activate
80 public void activate() {
Thomas Vachuskac8187a02016-08-08 11:56:24 -070081 executor = newSingleThreadExecutor(groupedThreads("onos/vnet", "provider", log));
Brian Stankefb61df42016-07-25 11:47:51 -040082 providerService = providerRegistry.register(this);
83 topologyService.addListener(topologyListener);
Brian Stankefb61df42016-07-25 11:47:51 -040084 log.info("Started");
85 }
86
87 @Deactivate
88 public void deactivate() {
89 topologyService.removeListener(topologyListener);
Thomas Vachuskac8187a02016-08-08 11:56:24 -070090 executor.shutdown();
91 executor = null;
Brian Stankefb61df42016-07-25 11:47:51 -040092 providerRegistry.unregister(this);
93 providerService = null;
94 log.info("Stopped");
95 }
96
97 @Override
98 public boolean isTraversable(ConnectPoint src, ConnectPoint dst) {
99 final boolean[] foundSrc = new boolean[1];
100 final boolean[] foundDst = new boolean[1];
101 Topology topology = topologyService.currentTopology();
102 Set<Path> paths = topologyService.getPaths(topology, src.deviceId(), dst.deviceId());
103 paths.forEach(path -> {
104 foundDst[0] = false;
105 foundSrc[0] = false;
106 // Traverse the links in each path to determine if both the src and dst connection
107 // point are in the path, if so then this src/dst pair are traversable.
108 path.links().forEach(link -> {
109 if (link.src().equals(src)) {
110 foundSrc[0] = true;
111 }
112 if (link.dst().equals(dst)) {
113 foundDst[0] = true;
114 }
115 });
116 if (foundSrc[0] && foundDst[0]) {
117 return;
118 }
119 });
120 return foundSrc[0] && foundDst[0];
121 }
122
123 @Override
124 public TunnelId createTunnel(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
125 return null;
126 }
127
128 @Override
129 public void destroyTunnel(NetworkId networkId, TunnelId tunnelId) {
130
131 }
132
133 /**
134 * Returns a set of set of interconnected connect points in the default topology.
135 * The inner set represents the interconnected connect points, and the outerset
136 * represents separate clusters.
137 *
138 * @param topology the default topology
139 * @return set of set of interconnected connect points.
140 */
141 protected Set<Set<ConnectPoint>> getConnectPoints(Topology topology) {
142 Set<Set<ConnectPoint>> clusters = new HashSet<>();
143 Set<TopologyCluster> topologyClusters = topologyService.getClusters(topology);
144 topologyClusters.forEach(topologyCluster -> {
145 Set<ConnectPoint> connectPointSet = new HashSet<>();
146 Set<Link> clusterLinks = topologyService.getClusterLinks(topology, topologyCluster);
147 clusterLinks.forEach(link -> {
148 connectPointSet.add(link.src());
149 connectPointSet.add(link.dst());
150 });
151 if (!connectPointSet.isEmpty()) {
152 clusters.add(connectPointSet);
153 }
154 });
155 return clusters;
156 }
157
158 /**
159 * Topology event listener.
160 */
161 private class InternalTopologyListener implements TopologyListener {
162 @Override
163 public void event(TopologyEvent event) {
Thomas Vachuskac8187a02016-08-08 11:56:24 -0700164 // Perform processing off the listener thread.
165 executor.submit(() -> providerService.topologyChanged(getConnectPoints(event.subject())));
Brian Stankefb61df42016-07-25 11:47:51 -0400166 }
167
168 @Override
169 public boolean isRelevant(TopologyEvent event) {
Thomas Vachuskac8187a02016-08-08 11:56:24 -0700170 return event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED &&
171 event.reasons().stream().anyMatch(reason -> reason instanceof LinkEvent);
Brian Stankefb61df42016-07-25 11:47:51 -0400172 }
173 }
174}