Graph modelisation for Path Computation Algorithm
[bgpcep.git] / graph / graph-impl / src / main / java / org / opendaylight / graph / impl / ConnectedVertexImpl.java
1 /*
2  * Copyright (c) 2019 Orange. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.graph.impl;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.graph.ConnectedEdge;
17 import org.opendaylight.graph.ConnectedVertex;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev191125.graph.topology.graph.Edge;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev191125.graph.topology.graph.Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev191125.graph.topology.graph.Vertex;
21
22 /**
23  * This Class implements the Connected Vertex used by the Connected Graph for path computation algorithms.
24  *
25  * @author Olivier Dugeon
26  * @author Philippe Niger
27  */
28
29 public class ConnectedVertexImpl implements ConnectedVertex {
30
31     /* Reference to input and output Connected Edge within the Connected Graph */
32     private ArrayList<ConnectedEdgeImpl> input = new ArrayList<>();
33     private ArrayList<ConnectedEdgeImpl> output = new ArrayList<>();
34
35     /* List of Prefixes announced by this Vertex */
36     private ArrayList<Prefix> prefixes = new ArrayList<>();
37
38     /* Reference to the Vertex of the standard Graph associated to the Connected Graph */
39     private Vertex vertex = null;
40
41     /* Connected Vertex Identifier */
42     private Long cvid;
43
44     public ConnectedVertexImpl(@NonNull Long key) {
45         checkArgument(key != 0, "Vertex Key must not be equal to 0");
46         this.cvid = key;
47         this.vertex = null;
48     }
49
50     public ConnectedVertexImpl(@NonNull Vertex vertex) {
51         checkArgument(vertex.getVertexId().longValue() != 0, "Vertex Key must not be equal to 0");
52         this.cvid = vertex.getVertexId().longValue();
53         this.vertex = vertex;
54     }
55
56     /**
57      * When vertex is removed, we must disconnect all Connected Edges.
58      */
59     void close() {
60         this.disconnect();
61     }
62
63     /**
64      * Set associated Vertex to this Connected Vertex.
65      *
66      * @param vertex Vertex
67      */
68     public ConnectedVertexImpl setVertex(Vertex vertex) {
69         this.vertex = vertex;
70         return this;
71     }
72
73     /**
74      * Add Connected Edge as input edge.
75      *
76      * @param edge Connected Edge
77      */
78     public ConnectedVertexImpl addInput(ConnectedEdgeImpl edge) {
79         if (!input.contains(edge)) {
80             input.add(edge);
81         }
82         return this;
83     }
84
85     /**
86      * Add Connected Edge as output edge.
87      *
88      * @param edge Connected Edge
89      */
90     public ConnectedVertexImpl addOutput(ConnectedEdgeImpl edge) {
91         if (!output.contains(edge)) {
92             output.add(edge);
93         }
94         return this;
95     }
96
97     /**
98      * Remove input Connected Edge.
99      *
100      * @param edge Connected Edge
101      */
102     public ConnectedVertexImpl removeInput(ConnectedEdgeImpl edge) {
103         input.remove(edge);
104         return this;
105     }
106
107     /**
108      * Remove output Connected Edge.
109      *
110      * @param edge Connected Edge
111      */
112     public ConnectedVertexImpl removeOutput(ConnectedEdgeImpl edge) {
113         output.remove(edge);
114         return this;
115     }
116
117     /**
118      * Disconnect all input and output Connected Edge.
119      */
120     public void disconnect() {
121         for (ConnectedEdgeImpl edge : input) {
122             edge.disconnectDestination();
123         }
124         for (ConnectedEdgeImpl edge : output) {
125             edge.disconnectSource();
126         }
127     }
128
129     /**
130      * Add Prefix to this Connected Vertex.
131      *
132      * @param prefix Prefix
133      */
134     public ConnectedVertexImpl addPrefix(Prefix prefix) {
135         if (!prefixes.contains(prefix)) {
136             prefixes.add(prefix);
137         }
138         return this;
139     }
140
141     /**
142      * Remove Prefix.
143      *
144      * @param prefix Prefix
145      */
146     public void removePrefix(Prefix prefix) {
147         if (prefixes.contains(prefix)) {
148             prefixes.remove(prefix);
149         }
150     }
151
152     @Override
153     public Long getKey() {
154         return this.cvid;
155     }
156
157     @Override
158     public Vertex getVertex() {
159         return this.vertex;
160     }
161
162     @Override
163     public List<ConnectedEdge> getEdgeTo(Long dstRid) {
164         ArrayList<ConnectedEdge> edgeList = new ArrayList<ConnectedEdge>();
165         for (ConnectedEdge edge : output) {
166             if (edge.getDestination().getKey().equals(dstRid)) {
167                 edgeList.add(edge);
168             }
169         }
170         return edgeList;
171     }
172
173     @Override
174     public List<Edge> getInputEdges() {
175         ArrayList<Edge> edgeList = new ArrayList<Edge>();
176         for (ConnectedEdge edge : input) {
177             edgeList.add(edge.getEdge());
178         }
179         return edgeList;
180     }
181
182     @Override
183     public List<ConnectedEdge> getInputConnectedEdges() {
184         return new ArrayList<ConnectedEdge>(this.input);
185     }
186
187     @Override
188     public List<Edge> getOutputEdges() {
189         ArrayList<Edge> edgeList = new ArrayList<Edge>();
190         for (ConnectedEdge edge : output) {
191             edgeList.add(edge.getEdge());
192         }
193         return edgeList;
194     }
195
196     @Override
197     public List<ConnectedEdge> getOutputConnectedEdges() {
198         return new ArrayList<ConnectedEdge>(this.output);
199     }
200
201     @Override
202     public List<Prefix> getPrefixes() {
203         return this.prefixes;
204     }
205
206     /**
207      * Return the name of the associated Vertex if set or the router-id otherwise.
208      *
209      * @return Vertex name or router-id
210     */
211     @Override
212     public String toString() {
213         if (vertex == null) {
214             return "Null";
215         }
216         if (vertex.getName() != null) {
217             return vertex.getName();
218         } else {
219             return vertex.getRouterId().toString();
220         }
221     }
222 }