blob: f1e20dacefa1637698a44428871e2abba150c3f0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.topology;
tom97937552014-09-11 10:48:42 -070017
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.AbstractDescription;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Link;
25import org.onosproject.net.SparseAnnotations;
Yuta HIGUCHI22102822014-11-12 23:09:59 -080026import org.slf4j.Logger;
tom97937552014-09-11 10:48:42 -070027
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070028import java.util.Map;
29
Thomas Vachuska320c58f2015-08-05 10:42:32 -070030import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070031import static org.slf4j.LoggerFactory.getLogger;
tom97937552014-09-11 10:48:42 -070032
33/**
34 * Default implementation of an immutable topology graph data carrier.
35 */
tomf5d85d42014-10-02 05:27:56 -070036public class DefaultGraphDescription extends AbstractDescription
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070037 implements GraphDescription {
tom97937552014-09-11 10:48:42 -070038
Yuta HIGUCHI22102822014-11-12 23:09:59 -080039 private static final Logger log = getLogger(DefaultGraphDescription.class);
40
tom97937552014-09-11 10:48:42 -070041 private final long nanos;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050042 private final long creationTime;
tom97937552014-09-11 10:48:42 -070043 private final ImmutableSet<TopologyVertex> vertexes;
44 private final ImmutableSet<TopologyEdge> edges;
45
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070046 private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap();
tom97937552014-09-11 10:48:42 -070047
tom97937552014-09-11 10:48:42 -070048 /**
49 * Creates a minimal topology graph description to allow core to construct
50 * and process the topology graph.
51 *
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070052 * @param nanos time in nanos of when the topology description was created
53 * @param devices collection of infrastructure devices
54 * @param links collection of infrastructure links
tom27ae0e62014-10-01 20:35:01 -070055 * @param annotations optional key/value annotations map
Sho SHIMIZUbe63b232015-06-30 10:57:58 -070056 * @deprecated in Cardinal Release
tom97937552014-09-11 10:48:42 -070057 */
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050058 @Deprecated
tom27ae0e62014-10-01 20:35:01 -070059 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070060 Iterable<Link> links,
61 SparseAnnotations... annotations) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050062 this(nanos, System.currentTimeMillis(), devices, links, annotations);
63 }
64
65 /**
66 * Creates a minimal topology graph description to allow core to construct
67 * and process the topology graph.
68 *
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070069 * @param nanos time in nanos of when the topology description was created
70 * @param millis time in millis of when the topology description was created
71 * @param devices collection of infrastructure devices
72 * @param links collection of infrastructure links
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050073 * @param annotations optional key/value annotations map
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050074 */
75 public DefaultGraphDescription(long nanos, long millis,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070076 Iterable<Device> devices,
77 Iterable<Link> links,
78 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070079 super(annotations);
tom97937552014-09-11 10:48:42 -070080 this.nanos = nanos;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050081 this.creationTime = millis;
tom97937552014-09-11 10:48:42 -070082 this.vertexes = buildVertexes(devices);
83 this.edges = buildEdges(links);
84 vertexesById.clear();
85 }
86
87 @Override
88 public long timestamp() {
89 return nanos;
90 }
91
92 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050093 public long creationTime() {
94 return creationTime;
95 }
96
97 @Override
tom97937552014-09-11 10:48:42 -070098 public ImmutableSet<TopologyVertex> vertexes() {
99 return vertexes;
100 }
101
102 @Override
103 public ImmutableSet<TopologyEdge> edges() {
104 return edges;
105 }
106
107 // Builds a set of topology vertexes from the specified list of devices
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700108 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
tom97937552014-09-11 10:48:42 -0700109 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
110 for (Device device : devices) {
111 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
112 vertexes.add(vertex);
113 vertexesById.put(vertex.deviceId(), vertex);
114 }
115 return vertexes.build();
116 }
117
118 // Builds a set of topology vertexes from the specified list of links
119 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
120 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
121 for (Link link : links) {
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800122 try {
123 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500124 vertexOf(link.dst()),
125 link));
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800126 } catch (IllegalArgumentException e) {
Jonathan Hart1e37d5612014-12-03 18:47:10 -0800127 log.debug("Ignoring {}, missing vertex", link);
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800128 }
tom97937552014-09-11 10:48:42 -0700129 }
130 return edges.build();
131 }
132
133 // Fetches a vertex corresponding to the given connection point device.
134 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
135 DeviceId id = connectPoint.deviceId();
136 TopologyVertex vertex = vertexesById.get(id);
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700137 checkArgument(vertex != null, "Vertex missing for %s", id);
tom97937552014-09-11 10:48:42 -0700138 return vertex;
139 }
140
141}