Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
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 |
tom | 27ae0e6 | 2014-10-01 20:35:01 -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; |
| 42 | private final ImmutableSet<TopologyVertex> vertexes; |
| 43 | private final ImmutableSet<TopologyEdge> edges; |
| 44 | |
| 45 | private final Map<DeviceId, TopologyVertex> vertexesById = Maps.newHashMap(); |
| 46 | |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Creates a minimal topology graph description to allow core to construct |
| 49 | * and process the topology graph. |
| 50 | * |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 51 | * @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 |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 55 | */ |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 56 | public DefaultGraphDescription(long nanos, Iterable<Device> devices, |
| 57 | Iterable<Link> links, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 58 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 59 | super(annotations); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 60 | 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 HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 96 | try { |
| 97 | edges.add(new DefaultTopologyEdge(vertexOf(link.src()), |
| 98 | vertexOf(link.dst()), link)); |
| 99 | } catch (IllegalArgumentException e) { |
Jonathan Hart | 1e37d561 | 2014-12-03 18:47:10 -0800 | [diff] [blame] | 100 | log.debug("Ignoring {}, missing vertex", link); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 101 | } |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 102 | } |
| 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) { |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 111 | throw new IllegalArgumentException("Vertex missing for " + id); |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 112 | } |
| 113 | return vertex; |
| 114 | } |
| 115 | |
| 116 | } |