GNPy client refactor
[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.rev190103.GnpyApi;
21 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.Result;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class GnpyConsumerImpl implements GnpyConsumer {
26     private static final Logger LOG = LoggerFactory.getLogger(GnpyConsumerImpl.class);
27
28     private GnpyResource api;
29     private JsonStringConverter<GnpyApi> gnpyApiConverter;
30     private JsonStringConverter<Result> resultConverter;
31
32     public GnpyConsumerImpl(String baseUrl, String username, String password,
33             BindingDOMCodecServices bindingDOMCodecServices) {
34         gnpyApiConverter = new JsonStringConverter<>(bindingDOMCodecServices);
35         resultConverter = new JsonStringConverter<>(bindingDOMCodecServices);
36         JsonConfigurator jsonConfigurator = new JsonConfigurator(gnpyApiConverter, resultConverter);
37         Client client = ClientBuilder.newClient();
38         HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
39         client.register(authFeature);
40         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
41             .register(JacksonFeature.class).register(jsonConfigurator);
42         api = WebResourceFactory.newResource(GnpyResource.class, client.target(baseUrl));
43     }
44
45     @Override
46     public boolean isAvailable() {
47         try {
48             api.checkStatus();
49             LOG.info("GNPy is available");
50             return true;
51         } catch (WebApplicationException | ProcessingException e) {
52             LOG.info("GNPy is not available ", e);
53             return false;
54         }
55     }
56
57     @Override
58     public Result computePaths(GnpyApi request) {
59         try {
60             return api.computePathRequest(request);
61         } catch (WebApplicationException | ProcessingException e) {
62             LOG.info("Something went wrong while requesting GNPy ", e);
63             return null;
64         }
65     }
66 }