12c3726ff70960b3764a7aa1d93239d0e52e63b2
[dcaegen2/platform.git] / mod / genprocessor / src / main / java / org / onap / dcae / genprocessor / OnboardingAPIClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.genprocessor;
19
20 import java.net.URI;
21
22 import com.fasterxml.jackson.core.JsonFactory;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class OnboardingAPIClient {
29
30     static final Logger LOG = LoggerFactory.getLogger(OnboardingAPIClient.class);
31
32     public static CompList getComponents(String hostOnboardingAPI) {
33         JsonFactory jf = new JsonFactory();
34         ObjectMapper om = new ObjectMapper();
35
36         try {
37             URI uri = new URI(hostOnboardingAPI + "/components");
38             return om.readValue(jf.createParser(uri.toURL()), CompList.class);
39         } catch (Exception e) {
40             String message = "Error while pulling components from onboarding API";
41             LOG.error(message, e);
42             throw new OnboardingAPIClientError(message, e);
43         }
44     }
45
46     public static Comp getComponent(URI componentUri) {
47         JsonFactory jf = new JsonFactory();
48         ObjectMapper om = new ObjectMapper();
49
50         try {
51             return om.readValue(jf.createParser(componentUri.toURL()), Comp.class);
52         } catch (Exception e) {
53             String message = "Error while pulling component from onboarding API";
54             LOG.error(message, e);
55             throw new OnboardingAPIClientError(message, e);
56         }
57     }
58
59     public static class OnboardingAPIClientError extends RuntimeException {
60         public OnboardingAPIClientError(String message, Throwable exception) {
61             super(message, exception);
62         }
63     }
64
65 }