Update release in docs/conf.yaml
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / consumer / GnpyConsumerImpl.java
1 /*
2  * Copyright © 2021 Orange, Inc. and others.  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.transportpce.pce.gnpy.consumer;
9
10 import javax.ws.rs.ProcessingException;
11 import javax.ws.rs.WebApplicationException;
12 import javax.ws.rs.client.Client;
13 import javax.ws.rs.client.ClientBuilder;
14 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
15 import org.glassfish.jersey.client.proxy.WebResourceFactory;
16 import org.glassfish.jersey.jackson.JacksonFeature;
17 import org.glassfish.jersey.logging.LoggingFeature;
18 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
19 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
20 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request;
21 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result;
22 import org.osgi.service.component.annotations.Activate;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Reference;
25 import org.osgi.service.metatype.annotations.AttributeDefinition;
26 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Component(configurationPid = "org.opendaylight.transportpce.pce")
31 public class GnpyConsumerImpl implements GnpyConsumer {
32     @ObjectClassDefinition
33     public @interface Configuration {
34         @AttributeDefinition
35         String url() default "http://127.0.0.1:8008";
36         @AttributeDefinition
37         String username() default "gnpy";
38         @AttributeDefinition
39         String password() default "gnpy";
40     }
41
42     private static final Logger LOG = LoggerFactory.getLogger(GnpyConsumerImpl.class);
43
44     private final GnpyResource api;
45
46     @Activate
47     public GnpyConsumerImpl(final Configuration configuration,
48             @Reference BindingDOMCodecServices bindingDOMCodecServices) {
49         this(configuration.url(), configuration.username(), configuration.password(), bindingDOMCodecServices);
50     }
51
52     public GnpyConsumerImpl(String baseUrl, String username, String password,
53             BindingDOMCodecServices bindingDOMCodecServices) {
54         JsonStringConverter<Request> gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices);
55         JsonStringConverter<Result> resultConverter = new JsonStringConverter<>(bindingDOMCodecServices);
56
57         Client client = ClientBuilder.newClient();
58         HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
59         client.register(authFeature);
60         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
61             .register(JacksonFeature.class)
62                 .register(new ResultMessageBodyReader(resultConverter))
63                 .register(new RequestMessageBodyWriter(gnpyRequestConverter));
64         api = WebResourceFactory.newResource(GnpyResource.class, client.target(baseUrl));
65     }
66
67     @Override
68     public boolean isAvailable() {
69         try {
70             api.getStatus();
71             LOG.info("GNPy is available");
72             return true;
73         } catch (WebApplicationException | ProcessingException e) {
74             LOG.info("GNPy is not available ", e);
75             return false;
76         }
77     }
78
79     @Override
80     public Result computePaths(Request request) {
81         try {
82             return api.computePathRequest(request);
83         } catch (WebApplicationException | ProcessingException e) {
84             LOG.info("Something went wrong while requesting GNPy ", e);
85             return null;
86         }
87     }
88 }