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 | |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 20 | import java.util.Map; |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 21 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.AbstractDescription; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.SparseAnnotations; |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 29 | |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import com.google.common.collect.Maps; |
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 |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [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 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 46 | private final Map<DeviceId, TopologyVertex> vertexesById = Maps |
| 47 | .newHashMap(); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 48 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Creates a minimal topology graph description to allow core to construct |
| 51 | * and process the topology graph. |
| 52 | * |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 53 | * @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 | * |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 59 | * @param annotations optional key/value annotations map |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 60 | * |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 61 | */ |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 62 | @Deprecated |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 63 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 64 | 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) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 88 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 89 | this.nanos = nanos; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 90 | this.creationTime = millis; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 91 | 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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 102 | public long creationTime() { |
| 103 | return creationTime; |
| 104 | } |
| 105 | |
| 106 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 107 | 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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 117 | private ImmutableSet<TopologyVertex> |
| 118 | buildVertexes(Iterable<Device> devices) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 119 | 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 HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 132 | try { |
| 133 | edges.add(new DefaultTopologyEdge(vertexOf(link.src()), |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 134 | vertexOf(link.dst()), |
| 135 | link)); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 136 | } catch (IllegalArgumentException e) { |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 137 | log.debug("Ignoring {}, missing vertex", link); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 138 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 139 | } |
| 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) { |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 148 | throw new IllegalArgumentException("Vertex missing for " + id); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 149 | } |
| 150 | return vertex; |
| 151 | } |
| 152 | |
| 153 | } |