blob: 2958e5d652bd21e9ba3461a996b6c8a428542749 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070053 * @param millis time in millis of when the topology description was created
54 * @param devices collection of infrastructure devices
55 * @param links collection of infrastructure links
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050056 * @param annotations optional key/value annotations map
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050057 */
58 public DefaultGraphDescription(long nanos, long millis,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070059 Iterable<Device> devices,
60 Iterable<Link> links,
61 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070062 super(annotations);
tom97937552014-09-11 10:48:42 -070063 this.nanos = nanos;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050064 this.creationTime = millis;
tom97937552014-09-11 10:48:42 -070065 this.vertexes = buildVertexes(devices);
66 this.edges = buildEdges(links);
67 vertexesById.clear();
68 }
69
70 @Override
71 public long timestamp() {
72 return nanos;
73 }
74
75 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050076 public long creationTime() {
77 return creationTime;
78 }
79
80 @Override
tom97937552014-09-11 10:48:42 -070081 public ImmutableSet<TopologyVertex> vertexes() {
82 return vertexes;
83 }
84
85 @Override
86 public ImmutableSet<TopologyEdge> edges() {
87 return edges;
88 }
89
90 // Builds a set of topology vertexes from the specified list of devices
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070091 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
tom97937552014-09-11 10:48:42 -070092 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
93 for (Device device : devices) {
94 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
95 vertexes.add(vertex);
96 vertexesById.put(vertex.deviceId(), vertex);
97 }
98 return vertexes.build();
99 }
100
101 // Builds a set of topology vertexes from the specified list of links
102 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
103 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
104 for (Link link : links) {
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800105 try {
106 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500107 vertexOf(link.dst()),
108 link));
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800109 } catch (IllegalArgumentException e) {
Jonathan Hart1e37d5612014-12-03 18:47:10 -0800110 log.debug("Ignoring {}, missing vertex", link);
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800111 }
tom97937552014-09-11 10:48:42 -0700112 }
113 return edges.build();
114 }
115
116 // Fetches a vertex corresponding to the given connection point device.
117 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
118 DeviceId id = connectPoint.deviceId();
119 TopologyVertex vertex = vertexesById.get(id);
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700120 checkArgument(vertex != null, "Vertex missing for %s", id);
tom97937552014-09-11 10:48:42 -0700121 return vertex;
122 }
123
124}