Path Computation Algorithms
[bgpcep.git] / algo / algo-impl / src / main / java / org / opendaylight / algo / impl / CspfPath.java
1 /*
2  * Copyright (c) 2020 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.algo.impl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.graph.ConnectedEdge;
14 import org.opendaylight.graph.ConnectedVertex;
15
16 /**
17  * This Class implements the Constrained Shortest Path First (CSPF) Path stored in the Priority Queue used by various
18  * Path Computation Algorithms.
19  *
20  * <p>The path corresponds to the computed path between the Source Vertex and the Current Vertex. Cost (based
21  * on TE Metric) and Delay are accumulated values from the source to the current vertex.
22  *
23  * <p>The class uses standard java "Comparable" interface to support "natural ordering" and thus implements
24  * the compareTo() method based on the "key" value. However, the equals() method uses Vertex Key for comparison.
25  * HashCode() method is also overridden by the Connected Vertex hashCode() method.
26  *
27  * @author Olivier Dugeon
28  *
29  */
30
31 public class CspfPath implements Comparable<CspfPath> {
32
33     /* Associated Connected Vertex: i.e. the current vertex in the Path */
34     private ConnectedVertex cvertex;
35
36     /* Path Length and associated cost and delay */
37     private float pathLength = 0;
38     private int cost = Integer.MAX_VALUE;
39     private int delay = Integer.MAX_VALUE;
40
41     /* Path as Connected Edge list from the source up to the Connected Vertex */
42     private ArrayList<ConnectedEdge> currentPath = new ArrayList<ConnectedEdge>();
43
44     /* Penultimate Connected Vertex in the current Path */
45     private Long predecessor;
46
47     public static final byte UNKNOWN   = 0x00;
48     public static final byte ACTIVE    = 0x01;
49     public static final byte SELECTED  = 0x02;
50     public static final byte DOMINATED = 0x03;
51     public static final byte PROCESSED = 0x04;
52     private byte pathStatus;
53     /* Key used by the Priority Queue to sort the paths */
54     private Integer key = Integer.MAX_VALUE;
55
56     public CspfPath(ConnectedVertex vertex) {
57         this.cvertex = vertex;
58     }
59
60     public ConnectedVertex getVertex() {
61         return this.cvertex;
62     }
63
64     public Long getVertexKey() {
65         return this.cvertex.getKey();
66     }
67
68     public CspfPath setCost(int cost) {
69         this.cost = cost;
70         return this;
71     }
72
73     public int getCost() {
74         return this.cost;
75     }
76
77     public CspfPath setDelay(int delay) {
78         this.delay = delay;
79         return this;
80     }
81
82     public int getDelay() {
83         return this.delay;
84     }
85
86     public CspfPath addConnectedEdge(ConnectedEdge edge) {
87         this.currentPath.add(edge);
88         return this;
89     }
90
91     public CspfPath replacePath(List<ConnectedEdge> list) {
92         if ((list != null) && (list.size() != 0)) {
93             this.currentPath.clear();
94         }
95         this.currentPath.addAll(list);
96         return this;
97     }
98
99     public List<ConnectedEdge> getPath() {
100         return this.currentPath;
101     }
102
103     public int getPathCount() {
104         return this.currentPath.size();
105     }
106
107     public CspfPath setPathStatus(byte status) {
108         this.pathStatus = status;
109         return this;
110     }
111
112     public byte getPathStatus() {
113         return this.pathStatus;
114     }
115
116     public CspfPath setPredecessor(Long vertexId) {
117         this.predecessor = vertexId;
118         return this;
119     }
120
121     public Long getPredecessor() {
122         return this.predecessor;
123     }
124
125     public CspfPath setPathLength(float length) {
126         this.pathLength = length;
127         return this;
128     }
129
130     public float getPathLength() {
131         return this.pathLength;
132     }
133
134     public void clearPath() {
135         this.currentPath.clear();
136     }
137
138     /*
139      * Definition of the comparator method to be used by the java Priority Queue:
140      * "In the PQ the elements are classified relying on the "key" attribute. The "compareTo" method return a negative,
141      * zero or positive integer as this object is less than, equal to or greater than the specified object"
142      *
143      * The "key" attribute is here represented by the weight of the associated Vertex in the Path. For example in
144      * Shortest Path First algorithm, the key represent the cost between the source and the Vertex.
145      *
146      * The "equals" method performs the comparison on the Vertex Key. The "equals" method is used by the "remove"
147      * method of the priority queue
148      *
149      */
150
151     public CspfPath setKey(Integer key) {
152         this.key = key;
153         return this;
154     }
155
156     public Integer getKey() {
157         return this.key;
158     }
159
160     @Override
161     public int compareTo(CspfPath other) {
162         return this.key.compareTo(other.getKey());
163     }
164
165     @Override
166     public boolean equals(Object object) {
167         if (!(object instanceof CspfPath)) {
168             return false;
169         }
170
171         CspfPath cspfPath = (CspfPath) object;
172         return this.getVertexKey().equals(cspfPath.getVertexKey());
173     }
174
175     @Override
176     public int hashCode() {
177         return this.cvertex.getKey().hashCode();
178     }
179
180 }