Graph modelisation for Path Computation Algorithm
[bgpcep.git] / graph / graph-impl / src / main / java / org / opendaylight / graph / impl / ConnectedEdgeImpl.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 org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.graph.ConnectedEdge;
15 import org.opendaylight.graph.ConnectedVertex;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev191125.graph.topology.graph.Edge;
17
18 /**
19  * This Class implements the Connected Edge used by the Connected Graph for path computation algorithms.
20  *
21  * @author Olivier Dugeon
22  * @author Philippe Niger
23  */
24
25 public class ConnectedEdgeImpl implements ConnectedEdge {
26
27     /* Reference to Source and Destination Connected Vertex within the Connected Graph */
28     private ConnectedVertexImpl source;
29     private ConnectedVertexImpl destination;
30
31     /* Reference to the Edge within the Graph associated to the Connected Graph */
32     private Edge edge;
33
34     /* Edge key in the Connected Graph */
35     private Long ceid;
36
37     public ConnectedEdgeImpl(@NonNull Long key) {
38         checkArgument(key != 0, "Edge Key must not be equal to 0");
39         this.ceid = key;
40         this.edge = null;
41     }
42
43     public ConnectedEdgeImpl(@NonNull Edge edge) {
44         checkArgument(edge.getEdgeId().longValue() != 0, "Edge Key must not be equal to 0");
45         this.edge = edge;
46         this.ceid = edge.getEdgeId().longValue();
47     }
48
49     /**
50      * When edge is removed, we must disconnect source and destination Connected Vertices.
51      */
52     void close() {
53         this.disconnect();
54     }
55
56     /**
57      * Set Connected Vertex as source.
58      *
59      * @param vertex Vertex
60      */
61     public ConnectedEdgeImpl setSource(ConnectedVertexImpl vertex) {
62         source = vertex;
63         return this;
64     }
65
66     /**
67      * Set Connected Vertex as destination.
68      *
69      * @param vertex Vertex
70      */
71     public ConnectedEdgeImpl setDestination(ConnectedVertexImpl vertex) {
72         destination = vertex;
73         return this;
74     }
75
76     /**
77      * Disconnect source Connected Vertex.
78      */
79     public void disconnectSource() {
80         if (source != null) {
81             source.removeOutput(this);
82             source = null;
83         }
84     }
85
86     /**
87      * Disconnect destination Connected Vertex.
88      */
89     public void disconnectDestination() {
90         if (destination != null) {
91             destination.removeInput(this);
92             destination = null;
93         }
94     }
95
96     /**
97      * Disconnect both source and destination Connected Vertices.
98      */
99     public void disconnect() {
100         disconnectSource();
101         disconnectDestination();
102     }
103
104     /**
105      * Set associated Edge to this Connected Edge.
106      *
107      * @param edge Edge
108      */
109     public ConnectedEdgeImpl setEdge(Edge edge) {
110         this.edge = edge;
111         return this;
112     }
113
114     @Override
115     public @NonNull Long getKey() {
116         return this.ceid;
117     }
118
119     @Override
120     public ConnectedVertex getSource() {
121         return this.source;
122     }
123
124     @Override
125     public ConnectedVertex getDestination() {
126         return this.destination;
127     }
128
129     @Override
130     public Edge getEdge() {
131         return this.edge;
132     }
133
134     /**
135      * Returns the name of the associated Edge if set or the interface address otherwise.
136      *
137      * @return Edge name or interface address
138      */
139     @Override
140     public String toString() {
141         if (edge == null) {
142             return "Null";
143         }
144         if (edge.getName() != null) {
145             return edge.getName();
146         }
147         if (edge.getEdgeAttributes() != null) {
148             if (edge.getEdgeAttributes().getLocalAddress() != null) {
149                 return edge.getEdgeAttributes().getLocalAddress().toString();
150             }
151             if (edge.getEdgeAttributes().getLocalIdentifier() != null) {
152                 return edge.getEdgeAttributes().getLocalIdentifier().toString();
153             }
154         }
155         return "Unknown Edge";
156     }
157 }