blob: ca64d1d47d91a0ce6c1bde975ec7c332ee8e2186 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.topology;
tom97937552014-09-11 10:48:42 -070017
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
tomf5d85d42014-10-02 05:27:56 -070020import org.onlab.onos.net.AbstractDescription;
tom97937552014-09-11 10:48:42 -070021import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.Device;
23import org.onlab.onos.net.DeviceId;
24import org.onlab.onos.net.Link;
tomf5d85d42014-10-02 05:27:56 -070025import org.onlab.onos.net.SparseAnnotations;
tom97937552014-09-11 10:48:42 -070026
27import java.util.Map;
28
29/**
30 * Default implementation of an immutable topology graph data carrier.
31 */
tomf5d85d42014-10-02 05:27:56 -070032public class DefaultGraphDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070033 implements GraphDescription {
tom97937552014-09-11 10:48:42 -070034
35 private final long nanos;
36 private final ImmutableSet<TopologyVertex> vertexes;
37 private final ImmutableSet<TopologyEdge> edges;
38
39 private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap();
40
tom97937552014-09-11 10:48:42 -070041 /**
42 * Creates a minimal topology graph description to allow core to construct
43 * and process the topology graph.
44 *
tom27ae0e62014-10-01 20:35:01 -070045 * @param nanos time in nanos of when the topology description was created
46 * @param devices collection of infrastructure devices
47 * @param links collection of infrastructure links
48 * @param annotations optional key/value annotations map
tom97937552014-09-11 10:48:42 -070049 */
tom27ae0e62014-10-01 20:35:01 -070050 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
51 Iterable<Link> links,
tomf5d85d42014-10-02 05:27:56 -070052 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070053 super(annotations);
tom97937552014-09-11 10:48:42 -070054 this.nanos = nanos;
55 this.vertexes = buildVertexes(devices);
56 this.edges = buildEdges(links);
57 vertexesById.clear();
58 }
59
60 @Override
61 public long timestamp() {
62 return nanos;
63 }
64
65 @Override
66 public ImmutableSet<TopologyVertex> vertexes() {
67 return vertexes;
68 }
69
70 @Override
71 public ImmutableSet<TopologyEdge> edges() {
72 return edges;
73 }
74
75 // Builds a set of topology vertexes from the specified list of devices
76 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
77 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
78 for (Device device : devices) {
79 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
80 vertexes.add(vertex);
81 vertexesById.put(vertex.deviceId(), vertex);
82 }
83 return vertexes.build();
84 }
85
86 // Builds a set of topology vertexes from the specified list of links
87 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
88 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
89 for (Link link : links) {
90 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
91 vertexOf(link.dst()), link));
92 }
93 return edges.build();
94 }
95
96 // Fetches a vertex corresponding to the given connection point device.
97 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
98 DeviceId id = connectPoint.deviceId();
99 TopologyVertex vertex = vertexesById.get(id);
100 if (vertex == null) {
tom747a2132014-10-02 08:18:41 -0700101 throw new IllegalArgumentException("Vertex missing for " + id);
tom97937552014-09-11 10:48:42 -0700102 }
103 return vertex;
104 }
105
106}