blob: 54865b3522cbe780b44eebfa9ece1a3b4b0a06e3 [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
Yuta HIGUCHI22102822014-11-12 23:09:59 -080018import static org.slf4j.LoggerFactory.getLogger;
19
Jonathan Hart1e37d5612014-12-03 18:47:10 -080020import java.util.Map;
Yuta HIGUCHI22102822014-11-12 23:09:59 -080021
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.AbstractDescription;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Link;
27import org.onosproject.net.SparseAnnotations;
Yuta HIGUCHI22102822014-11-12 23:09:59 -080028import org.slf4j.Logger;
tom97937552014-09-11 10:48:42 -070029
Jonathan Hart1e37d5612014-12-03 18:47:10 -080030import com.google.common.collect.ImmutableSet;
31import com.google.common.collect.Maps;
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
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050037implements 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
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050046 private final Map<DeviceId, TopologyVertex> vertexesById = Maps
47 .newHashMap();
tom97937552014-09-11 10:48:42 -070048
tom97937552014-09-11 10:48:42 -070049 /**
50 * Creates a minimal topology graph description to allow core to construct
51 * and process the topology graph.
52 *
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050053 * @param nanos time in nanos of when the topology description was created
54 *
55 * @param devices collection of infrastructure devices
56 *
57 * @param links collection of infrastructure links
58 *
tom27ae0e62014-10-01 20:35:01 -070059 * @param annotations optional key/value annotations map
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050060 *
tom97937552014-09-11 10:48:42 -070061 */
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050062 @Deprecated
tom27ae0e62014-10-01 20:35:01 -070063 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050064 Iterable<Link> links,
65 SparseAnnotations... annotations) {
66 this(nanos, System.currentTimeMillis(), devices, links, annotations);
67 }
68
69 /**
70 * Creates a minimal topology graph description to allow core to construct
71 * and process the topology graph.
72 *
73 * @param nanos time in nanos of when the topology description was created
74 *
75 * @param millis time in millis of when the topology description was created
76 *
77 * @param devices collection of infrastructure devices
78 *
79 * @param links collection of infrastructure links
80 *
81 * @param annotations optional key/value annotations map
82 *
83 */
84 public DefaultGraphDescription(long nanos, long millis,
85 Iterable<Device> devices,
86 Iterable<Link> links,
87 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070088 super(annotations);
tom97937552014-09-11 10:48:42 -070089 this.nanos = nanos;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050090 this.creationTime = millis;
tom97937552014-09-11 10:48:42 -070091 this.vertexes = buildVertexes(devices);
92 this.edges = buildEdges(links);
93 vertexesById.clear();
94 }
95
96 @Override
97 public long timestamp() {
98 return nanos;
99 }
100
101 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500102 public long creationTime() {
103 return creationTime;
104 }
105
106 @Override
tom97937552014-09-11 10:48:42 -0700107 public ImmutableSet<TopologyVertex> vertexes() {
108 return vertexes;
109 }
110
111 @Override
112 public ImmutableSet<TopologyEdge> edges() {
113 return edges;
114 }
115
116 // Builds a set of topology vertexes from the specified list of devices
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500117 private ImmutableSet<TopologyVertex>
118 buildVertexes(Iterable<Device> devices) {
tom97937552014-09-11 10:48:42 -0700119 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
120 for (Device device : devices) {
121 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
122 vertexes.add(vertex);
123 vertexesById.put(vertex.deviceId(), vertex);
124 }
125 return vertexes.build();
126 }
127
128 // Builds a set of topology vertexes from the specified list of links
129 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
130 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
131 for (Link link : links) {
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800132 try {
133 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500134 vertexOf(link.dst()),
135 link));
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800136 } catch (IllegalArgumentException e) {
Jonathan Hart1e37d5612014-12-03 18:47:10 -0800137 log.debug("Ignoring {}, missing vertex", link);
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800138 }
tom97937552014-09-11 10:48:42 -0700139 }
140 return edges.build();
141 }
142
143 // Fetches a vertex corresponding to the given connection point device.
144 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
145 DeviceId id = connectPoint.deviceId();
146 TopologyVertex vertex = vertexesById.get(id);
147 if (vertex == null) {
tom747a2132014-10-02 08:18:41 -0700148 throw new IllegalArgumentException("Vertex missing for " + id);
tom97937552014-09-11 10:48:42 -0700149 }
150 return vertex;
151 }
152
153}