cd4b6eb69913b9f7c070c8101772948ef6f504d8
[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.rev220221.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     JsonStringConverter<Request> gnpyRequestConverter;
30     JsonStringConverter<Result> resultConverter;
31
32     public GnpyConsumerImpl(String baseUrl, String username, String password,
33             BindingDOMCodecServices bindingDOMCodecServices) {
34         gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices);
35         resultConverter = new JsonStringConverter<>(bindingDOMCodecServices);
36
37         JsonConfigurator jsonConfigurator = new JsonConfigurator(gnpyRequestConverter, resultConverter);
38         Client client = ClientBuilder.newClient();
39         HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
40         client.register(authFeature);
41         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
42             .register(JacksonFeature.class).register(jsonConfigurator);
43         api = WebResourceFactory.newResource(GnpyResource.class, client.target(baseUrl));
44     }
45
46     @Override
47     public boolean isAvailable() {
48         try {
49             api.getStatus();
50             LOG.info("GNPy is available");
51             return true;
52         } catch (WebApplicationException | ProcessingException e) {
53             LOG.info("GNPy is not available ", e);
54             return false;
55         }
56     }
57
58     @Override
59     public Result computePaths(Request request) {
60         try {
61             return api.computePathRequest(request);
62         } catch (WebApplicationException | ProcessingException e) {
63             LOG.info("Something went wrong while requesting GNPy ", e);
64             return null;
65         }
66     }
67 }