Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.topology; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.AbstractDescription; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.SparseAnnotations; |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 26 | import org.slf4j.Logger; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 27 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 28 | import java.util.Map; |
| 29 | |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 31 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Default implementation of an immutable topology graph data carrier. |
| 35 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 36 | public class DefaultGraphDescription extends AbstractDescription |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 37 | implements GraphDescription { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 38 | |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 39 | private static final Logger log = getLogger(DefaultGraphDescription.class); |
| 40 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 41 | private final long nanos; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 42 | private final long creationTime; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 43 | private final ImmutableSet<TopologyVertex> vertexes; |
| 44 | private final ImmutableSet<TopologyEdge> edges; |
| 45 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 46 | private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap(); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 47 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Creates a minimal topology graph description to allow core to construct |
| 50 | * and process the topology graph. |
| 51 | * |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 52 | * @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 |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 55 | * @param annotations optional key/value annotations map |
Sho SHIMIZU | be63b23 | 2015-06-30 10:57:58 -0700 | [diff] [blame] | 56 | * @deprecated in Cardinal Release |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 57 | */ |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 58 | @Deprecated |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 59 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 60 | Iterable<Link> links, |
| 61 | SparseAnnotations... annotations) { |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 62 | 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 Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 69 | * @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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 73 | * @param annotations optional key/value annotations map |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 74 | */ |
| 75 | public DefaultGraphDescription(long nanos, long millis, |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 76 | Iterable<Device> devices, |
| 77 | Iterable<Link> links, |
| 78 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 79 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 80 | this.nanos = nanos; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 81 | this.creationTime = millis; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 82 | 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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 93 | public long creationTime() { |
| 94 | return creationTime; |
| 95 | } |
| 96 | |
| 97 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 98 | 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 Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 108 | private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 109 | 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 HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 122 | try { |
| 123 | edges.add(new DefaultTopologyEdge(vertexOf(link.src()), |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 124 | vertexOf(link.dst()), |
| 125 | link)); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 126 | } catch (IllegalArgumentException e) { |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 127 | log.debug("Ignoring {}, missing vertex", link); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 128 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 129 | } |
| 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 Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 137 | checkArgument(vertex != null, "Vertex missing for %s", id); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 138 | return vertex; |
| 139 | } |
| 140 | |
| 141 | } |