blob: 972b5dc47abe960560d04536d00f95468e6daefb [file] [log] [blame]
Brian Stanke8e9f8d12016-06-08 14:48:33 -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.common.DefaultTopology;
20import org.onosproject.event.AbstractListenerManager;
21import org.onosproject.incubator.net.virtual.VirtualNetwork;
22import org.onosproject.incubator.net.virtual.VirtualNetworkService;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.DisjointPath;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
29import org.onosproject.net.topology.ClusterId;
30import org.onosproject.net.topology.DefaultGraphDescription;
31import org.onosproject.net.topology.LinkWeight;
32import org.onosproject.net.topology.Topology;
33import org.onosproject.net.topology.TopologyCluster;
34import org.onosproject.net.topology.TopologyEvent;
35import org.onosproject.net.topology.TopologyGraph;
36import org.onosproject.net.topology.TopologyListener;
37import org.onosproject.net.topology.TopologyService;
38
39import java.util.Map;
40import java.util.Set;
41import java.util.stream.Collectors;
42
43import static com.google.common.base.Preconditions.checkArgument;
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.onosproject.incubator.net.virtual.DefaultVirtualLink.PID;
46
47/**
48 * Topology service implementation built on the virtual network service.
49 */
50public class VirtualNetworkTopologyService extends AbstractListenerManager<TopologyEvent, TopologyListener>
51 implements TopologyService, VnetService {
52
53 private static final String NETWORK_NULL = "Network ID cannot be null";
54 private static final String TOPOLOGY_NULL = "Topology cannot be null";
55 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
56 private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
57 private static final String CLUSTER_NULL = "Topology cluster cannot be null";
58 private static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
59 private static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
60
61 private final VirtualNetwork network;
62 private final VirtualNetworkService manager;
63
64 /**
65 * Creates a new VirtualNetworkTopologyService object.
66 *
67 * @param virtualNetworkManager virtual network manager service
68 * @param network virtual network
69 */
70 public VirtualNetworkTopologyService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
71 checkNotNull(network, NETWORK_NULL);
72 this.network = network;
73 this.manager = virtualNetworkManager;
74 }
75
76 @Override
77 public Topology currentTopology() {
78 Iterable<Device> devices = manager.getVirtualDevices(network().id())
79 .stream()
80 .collect(Collectors.toSet());
81 Iterable<Link> links = manager.getVirtualLinks(network().id())
82 .stream()
83 .collect(Collectors.toSet());
84
85 DefaultGraphDescription graph = new DefaultGraphDescription(System.nanoTime(), System.currentTimeMillis(),
86 devices, links);
87 return new DefaultTopology(PID, graph);
88 }
89
90 @Override
91 public boolean isLatest(Topology topology) {
92 Topology currentTopology = currentTopology();
93 return defaultTopology(topology).getGraph().equals(defaultTopology(currentTopology).getGraph());
94 }
95
96 @Override
97 public TopologyGraph getGraph(Topology topology) {
98 return defaultTopology(topology).getGraph();
99 }
100
101 // Validates the specified topology and returns it as a default
102 private DefaultTopology defaultTopology(Topology topology) {
103 checkNotNull(topology, TOPOLOGY_NULL);
104 checkArgument(topology instanceof DefaultTopology,
105 "Topology class %s not supported", topology.getClass());
106 return (DefaultTopology) topology;
107 }
108
109 @Override
110 public Set<TopologyCluster> getClusters(Topology topology) {
111 return defaultTopology(topology).getClusters();
112 }
113
114 @Override
115 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
116 checkNotNull(clusterId, CLUSTER_ID_NULL);
117 return defaultTopology(topology).getCluster(clusterId);
118 }
119
120 @Override
121 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
122 checkNotNull(cluster, CLUSTER_NULL);
123 return defaultTopology(topology).getClusterDevices(cluster);
124 }
125
126 @Override
127 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
128 checkNotNull(cluster, CLUSTER_NULL);
129 return defaultTopology(topology).getClusterLinks(cluster);
130 }
131
132 @Override
133 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
134 checkNotNull(src, DEVICE_ID_NULL);
135 checkNotNull(dst, DEVICE_ID_NULL);
136 return defaultTopology(topology).getPaths(src, dst);
137 }
138
139 @Override
140 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
141 checkNotNull(src, DEVICE_ID_NULL);
142 checkNotNull(dst, DEVICE_ID_NULL);
143 checkNotNull(weight, LINK_WEIGHT_NULL);
144 return defaultTopology(topology).getPaths(src, dst, weight);
145 }
146
147 @Override
148 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
149 checkNotNull(src, DEVICE_ID_NULL);
150 checkNotNull(dst, DEVICE_ID_NULL);
151 return defaultTopology(topology).getDisjointPaths(src, dst);
152 }
153
154 @Override
155 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
156 checkNotNull(src, DEVICE_ID_NULL);
157 checkNotNull(dst, DEVICE_ID_NULL);
158 checkNotNull(weight, LINK_WEIGHT_NULL);
159 return defaultTopology(topology).getDisjointPaths(src, dst, weight);
160 }
161
162 @Override
163 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
164 Map<Link, Object> riskProfile) {
165 checkNotNull(src, DEVICE_ID_NULL);
166 checkNotNull(dst, DEVICE_ID_NULL);
167 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
168 }
169
170 @Override
171 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
172 LinkWeight weight, Map<Link, Object> riskProfile) {
173 checkNotNull(src, DEVICE_ID_NULL);
174 checkNotNull(dst, DEVICE_ID_NULL);
175 checkNotNull(weight, LINK_WEIGHT_NULL);
176 return defaultTopology(topology).getDisjointPaths(src, dst, weight, riskProfile);
177 }
178
179 @Override
180 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
181 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
182 return defaultTopology(topology).isInfrastructure(connectPoint);
183 }
184
185 @Override
186 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
187 checkNotNull(connectPoint, CONNECTION_POINT_NULL);
188 return defaultTopology(topology).isBroadcastPoint(connectPoint);
189 }
190
191 @Override
192 public VirtualNetwork network() {
193 return network;
194 }
195}