Moderinize algo-impl
[bgpcep.git] / algo / algo-impl / src / main / java / org / opendaylight / algo / impl / PathComputationServer.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 package org.opendaylight.algo.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.algo.PathComputationAlgorithm;
17 import org.opendaylight.algo.PathComputationProvider;
18 import org.opendaylight.graph.ConnectedGraph;
19 import org.opendaylight.graph.ConnectedGraphProvider;
20 import org.opendaylight.mdsal.binding.api.RpcProviderService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.graph.rev220720.graph.topology.graph.VertexKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.AlgorithmType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.ComputationStatus;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.ConstrainedPath;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.GetConstrainedPath;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.GetConstrainedPathInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.GetConstrainedPathOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.path.computation.rev220324.GetConstrainedPathOutputBuilder;
29 import org.opendaylight.yangtools.concepts.Registration;
30 import org.opendaylight.yangtools.yang.common.ErrorType;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Deactivate;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Path Computation Algorithms provider.
42  *
43  * @author Olivier Dugeon
44  */
45 @Singleton
46 @Component(immediate = true, service = PathComputationProvider.class)
47 public final class PathComputationServer implements AutoCloseable, PathComputationProvider, GetConstrainedPath {
48     private static final Logger LOG = LoggerFactory.getLogger(PathComputationServer.class);
49
50     private final ConnectedGraphProvider graphProvider;
51     private final Registration registration;
52
53     @Inject
54     @Activate
55     public PathComputationServer(@Reference final RpcProviderService rpcService,
56             @Reference final ConnectedGraphProvider graphProvider) {
57         this.graphProvider = requireNonNull(graphProvider);
58         registration = rpcService.registerRpcImplementation(this);
59     }
60
61     @Override
62     public ListenableFuture<RpcResult<GetConstrainedPathOutput>> invoke(final GetConstrainedPathInput input) {
63         final GetConstrainedPathOutputBuilder output = new GetConstrainedPathOutputBuilder();
64
65         LOG.info("Got Path Computation Service request");
66
67         /* First, get graph */
68         final ConnectedGraph cgraph = graphProvider.getConnectedGraph(input.getGraphName());
69         if (cgraph == null) {
70             output.setStatus(ComputationStatus.Failed);
71             return RpcResultBuilder.<GetConstrainedPathOutput>failed()
72                     .withError(ErrorType.RPC, "Unknown Graph Name").buildFuture();
73         }
74
75         /* get a new Path Computation Algorithm according to Input choice */
76         PathComputationAlgorithm algo = getPathComputationAlgorithm(cgraph, input.getAlgorithm());
77         if (algo == null) {
78             output.setStatus(ComputationStatus.Failed);
79             return RpcResultBuilder.<GetConstrainedPathOutput>failed()
80                     .withError(ErrorType.RPC, "Unknown Path Computation Algorithm").buildFuture();
81         }
82
83         /*
84          * Request Path Computation for given source, destination and
85          * constraints
86          */
87         final VertexKey source = new VertexKey(input.getSource());
88         final VertexKey destination = new VertexKey(input.getDestination());
89         LOG.info("Call Path Computation {} algorithm for path from {} to {} with contraints {}",
90                 input.getAlgorithm().getName(), source, destination, input.getConstraints());
91         final ConstrainedPath cpath = algo.computeP2pPath(source, destination, input.getConstraints());
92
93         /* Send back the Computed Path */
94         output.setPathDescription(cpath.getPathDescription()).setStatus(cpath.getStatus())
95                 .setComputedMetric(cpath.getMetric()).setComputedTeMetric(cpath.getTeMetric())
96                 .setComputedDelay(cpath.getDelay());
97
98         return RpcResultBuilder.success(output.build()).buildFuture();
99     }
100
101     @Override
102     @Deactivate
103     @PreDestroy
104     public void close() {
105         registration.close();
106     }
107
108     @Override
109     public PathComputationAlgorithm getPathComputationAlgorithm(final ConnectedGraph runningGraph,
110             final AlgorithmType algorithmType) {
111         PathComputationAlgorithm algo = null;
112         switch (algorithmType) {
113             case Spf:
114                 algo = new ShortestPathFirst(runningGraph);
115                 break;
116             case Cspf:
117                 algo = new ConstrainedShortestPathFirst(runningGraph);
118                 break;
119             case Samcra:
120                 algo = new Samcra(runningGraph);
121                 break;
122             default:
123                 break;
124         }
125         return algo;
126     }
127 }