Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 53 | * @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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 56 | * @param annotations optional key/value annotations map |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 57 | */ |
| 58 | public DefaultGraphDescription(long nanos, long millis, |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 59 | Iterable<Device> devices, |
| 60 | Iterable<Link> links, |
| 61 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 62 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 63 | this.nanos = nanos; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 64 | this.creationTime = millis; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 65 | 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 Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 76 | public long creationTime() { |
| 77 | return creationTime; |
| 78 | } |
| 79 | |
| 80 | @Override |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 81 | 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 Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 91 | private ImmutableSet<TopologyVertex> buildVertexes(Iterable<Device> devices) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 92 | 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 HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 105 | try { |
| 106 | edges.add(new DefaultTopologyEdge(vertexOf(link.src()), |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 107 | vertexOf(link.dst()), |
| 108 | link)); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 109 | } catch (IllegalArgumentException e) { |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 110 | log.debug("Ignoring {}, missing vertex", link); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 111 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 112 | } |
| 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 Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 120 | checkArgument(vertex != null, "Vertex missing for %s", id); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 121 | return vertex; |
| 122 | } |
| 123 | |
| 124 | } |