Path Computation Server
[bgpcep.git] / pcep / server / server-provider / src / main / java / org / opendaylight / bgpcep / pcep / server / provider / PceServerFactory.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.bgpcep.pcep.server.provider;
10
11 import com.google.common.base.Preconditions;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.algo.PathComputationProvider;
14 import org.opendaylight.bgpcep.pcep.server.PceServerProvider;
15 import org.opendaylight.graph.ConnectedGraph;
16 import org.opendaylight.graph.ConnectedGraphProvider;
17
18 public class PceServerFactory implements PceServerProvider {
19
20     private final ConnectedGraphProvider graphProvider;
21     private final PathComputationProvider algoProvider;
22     private ConnectedGraph tedGraph = null;
23
24     public PceServerFactory(ConnectedGraphProvider graphProvider, PathComputationProvider pathComputationProvider) {
25         Preconditions.checkArgument(graphProvider != null);
26         this.graphProvider = graphProvider;
27         this.algoProvider = pathComputationProvider;
28         setTedGraph();
29     }
30
31     /**
32      * Set Traffic Engineering Graph. This method is necessary as the TedGraph could be available
33      * after the PathComputationFactory start e.g. manual insertion of a ted Graph, or late tedGraph fulfillment
34      * from BGP Link State.
35      */
36     private void setTedGraph() {
37         for (ConnectedGraph cgraph : this.graphProvider.getConnectedGraphs()) {
38             if (cgraph.getGraph().getName().startsWith("ted://")) {
39                 this.tedGraph = cgraph;
40                 break;
41             }
42         }
43     }
44
45     @Override
46     public PathComputationImpl getPathComputation() {
47         /* Leave a change to get a valid Graph */
48         if (tedGraph == null) {
49             setTedGraph();
50         }
51         return new PathComputationImpl(tedGraph, algoProvider);
52     }
53
54     @Override
55     public @Nullable ConnectedGraph getTedGraph() {
56         /* Leave a change to get a valid Graph in case of late fulfillment */
57         if (tedGraph == null) {
58             setTedGraph();
59         }
60         return this.tedGraph;
61     }
62 }