blob: 5e4d05055ebf68f4a106f8e89acf496f12d97ce8 [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
Yuta HIGUCHI22102822014-11-12 23:09:59 -080018import static org.slf4j.LoggerFactory.getLogger;
19
tom97937552014-09-11 10:48:42 -070020import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Maps;
Yuta HIGUCHI22102822014-11-12 23:09:59 -080022
tomf5d85d42014-10-02 05:27:56 -070023import org.onlab.onos.net.AbstractDescription;
tom97937552014-09-11 10:48:42 -070024import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.Device;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.Link;
tomf5d85d42014-10-02 05:27:56 -070028import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI22102822014-11-12 23:09:59 -080029import org.slf4j.Logger;
tom97937552014-09-11 10:48:42 -070030
31import java.util.Map;
32
33/**
34 * Default implementation of an immutable topology graph data carrier.
35 */
tomf5d85d42014-10-02 05:27:56 -070036public class DefaultGraphDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -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;
42 private final ImmutableSet<TopologyVertex> vertexes;
43 private final ImmutableSet<TopologyEdge> edges;
44
45 private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap();
46
tom97937552014-09-11 10:48:42 -070047 /**
48 * Creates a minimal topology graph description to allow core to construct
49 * and process the topology graph.
50 *
tom27ae0e62014-10-01 20:35:01 -070051 * @param nanos time in nanos of when the topology description was created
52 * @param devices collection of infrastructure devices
53 * @param links collection of infrastructure links
54 * @param annotations optional key/value annotations map
tom97937552014-09-11 10:48:42 -070055 */
tom27ae0e62014-10-01 20:35:01 -070056 public DefaultGraphDescription(long nanos, Iterable<Device> devices,
57 Iterable<Link> links,
tomf5d85d42014-10-02 05:27:56 -070058 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -070059 super(annotations);
tom97937552014-09-11 10:48:42 -070060 this.nanos = nanos;
61 this.vertexes = buildVertexes(devices);
62 this.edges = buildEdges(links);
63 vertexesById.clear();
64 }
65
66 @Override
67 public long timestamp() {
68 return nanos;
69 }
70
71 @Override
72 public ImmutableSet<TopologyVertex> vertexes() {
73 return vertexes;
74 }
75
76 @Override
77 public ImmutableSet<TopologyEdge> edges() {
78 return edges;
79 }
80
81 // Builds a set of topology vertexes from the specified list of devices
82 private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) {
83 ImmutableSet.Builder<TopologyVertex> vertexes = ImmutableSet.builder();
84 for (Device device : devices) {
85 TopologyVertex vertex = new DefaultTopologyVertex(device.id());
86 vertexes.add(vertex);
87 vertexesById.put(vertex.deviceId(), vertex);
88 }
89 return vertexes.build();
90 }
91
92 // Builds a set of topology vertexes from the specified list of links
93 private ImmutableSet<TopologyEdge> buildEdges(Iterable<Link> links) {
94 ImmutableSet.Builder<TopologyEdge> edges = ImmutableSet.builder();
95 for (Link link : links) {
Yuta HIGUCHI22102822014-11-12 23:09:59 -080096 try {
97 edges.add(new DefaultTopologyEdge(vertexOf(link.src()),
98 vertexOf(link.dst()), link));
99 } catch (IllegalArgumentException e) {
100 log.debug("Ignoring {}, missing vertex", link, e);
101 }
tom97937552014-09-11 10:48:42 -0700102 }
103 return edges.build();
104 }
105
106 // Fetches a vertex corresponding to the given connection point device.
107 private TopologyVertex vertexOf(ConnectPoint connectPoint) {
108 DeviceId id = connectPoint.deviceId();
109 TopologyVertex vertex = vertexesById.get(id);
110 if (vertex == null) {
tom747a2132014-10-02 08:18:41 -0700111 throw new IllegalArgumentException("Vertex missing for " + id);
tom97937552014-09-11 10:48:42 -0700112 }
113 return vertex;
114 }
115
116}