2 * Copyright (c) 2020 Orange. All rights reserved.
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
8 package org.opendaylight.algo.impl;
10 import static java.util.Objects.requireNonNull;
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;
41 * Path Computation Algorithms provider.
43 * @author Olivier Dugeon
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);
50 private final ConnectedGraphProvider graphProvider;
51 private final Registration registration;
55 public PathComputationServer(@Reference final RpcProviderService rpcService,
56 @Reference final ConnectedGraphProvider graphProvider) {
57 this.graphProvider = requireNonNull(graphProvider);
58 registration = rpcService.registerRpcImplementation(this);
62 public ListenableFuture<RpcResult<GetConstrainedPathOutput>> invoke(final GetConstrainedPathInput input) {
63 final GetConstrainedPathOutputBuilder output = new GetConstrainedPathOutputBuilder();
65 LOG.info("Got Path Computation Service request");
67 /* First, get graph */
68 final ConnectedGraph cgraph = graphProvider.getConnectedGraph(input.getGraphName());
70 output.setStatus(ComputationStatus.Failed);
71 return RpcResultBuilder.<GetConstrainedPathOutput>failed()
72 .withError(ErrorType.RPC, "Unknown Graph Name").buildFuture();
75 /* get a new Path Computation Algorithm according to Input choice */
76 PathComputationAlgorithm algo = getPathComputationAlgorithm(cgraph, input.getAlgorithm());
78 output.setStatus(ComputationStatus.Failed);
79 return RpcResultBuilder.<GetConstrainedPathOutput>failed()
80 .withError(ErrorType.RPC, "Unknown Path Computation Algorithm").buildFuture();
84 * Request Path Computation for given source, destination and
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());
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());
98 return RpcResultBuilder.success(output.build()).buildFuture();
104 public void close() {
105 registration.close();
109 public PathComputationAlgorithm getPathComputationAlgorithm(final ConnectedGraph runningGraph,
110 final AlgorithmType algorithmType) {
111 PathComputationAlgorithm algo = null;
112 switch (algorithmType) {
114 algo = new ShortestPathFirst(runningGraph);
117 algo = new ConstrainedShortestPathFirst(runningGraph);
120 algo = new Samcra(runningGraph);