3700c3da8b6ed26aaed34efd5a45dd457783756c
[ui/dmaapbc.git] /
1 package org.openecomp.dcae.dmaapbc.dbcapp.rest;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.List;
6
7 import org.apache.http.HttpHost;
8 import org.apache.http.auth.AuthScope;
9 import org.apache.http.auth.UsernamePasswordCredentials;
10 import org.apache.http.client.CredentialsProvider;
11 import org.apache.http.impl.client.BasicCredentialsProvider;
12 import org.apache.http.impl.client.CloseableHttpClient;
13 import org.apache.http.impl.client.HttpClientBuilder;
14 import org.openecomp.dcae.dmaapbc.dbcapp.domain.DmaapAccess;
15 import org.openecomp.dcae.dmaapbc.dbcapp.domain.ManifestTransportModel;
16 import org.openecomp.dcae.dmaapbc.dbcapp.service.DmaapAccessService;
17 import org.springframework.core.ParameterizedTypeReference;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.http.ResponseEntity;
20 import org.springframework.web.client.RestTemplate;
21
22 /**
23  * Provides methods for accessing the DBC microservice via REST using basic HTTP
24  * authentication.
25  * 
26  */
27 public class DbcUsvcRestClient implements DmaapAccessService {
28
29         public static final String endpointPath = "/dmaap_access";
30         private final String baseUrl;
31         private final RestTemplate restTemplate;
32
33         /**
34          * Builds a restTemplate that uses basic HTTP authentication for use by all
35          * methods in this class.
36          * 
37          * @param webapiUrl
38          *            URL of the web endpoint
39          * @param user
40          *            user name
41          * @param pass
42          *            password
43          */
44         public DbcUsvcRestClient(String webapiUrl, String user, String pass) {
45                 if (webapiUrl == null || user == null || pass == null)
46                         throw new IllegalArgumentException("Nulls not permitted");
47
48                 baseUrl = webapiUrl;
49                 URL url = null;
50                 try {
51                         url = new URL(baseUrl);
52                 } catch (MalformedURLException ex) {
53                         throw new RuntimeException("Failed to parse URL", ex);
54                 }
55                 final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort());
56
57                 // Build a client with a credentials provider
58                 CredentialsProvider credsProvider = new BasicCredentialsProvider();
59                 credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass));
60                 HttpClientBuilder clientBuilder = HttpClientBuilder.create();
61                 CloseableHttpClient httpClient = clientBuilder.setDefaultCredentialsProvider(credsProvider).build();
62
63                 // Create request factory with our superpower client
64                 HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(
65                                 httpHost);
66                 requestFactory.setHttpClient(httpClient);
67
68                 // Put the factory in the template
69                 this.restTemplate = new RestTemplate();
70                 restTemplate.setRequestFactory(requestFactory);
71         }
72
73         /**
74          * Gets the manifest from the micro service.
75          */
76         @Override
77         public ManifestTransportModel getManifest() {
78                 String url = baseUrl + "/manifest";
79                 ResponseEntity<ManifestTransportModel> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
80                                 ManifestTransportModel.class);
81                 ManifestTransportModel response = daResponse.getBody();
82                 return response;
83         }
84
85         /**
86          * Gets the count of DMaaP access profiles.
87          * 
88          * @return Number of access profiles in the database.
89          */
90         public int getDmaapAccessCount() {
91                 String url = baseUrl + "/count_dmaap_access";
92                 ResponseEntity<DbcUsvcRestResponse> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
93                                 DbcUsvcRestResponse.class);
94                 DbcUsvcRestResponse response = daResponse.getBody();
95                 return response.getStatus();
96         }
97
98         /**
99          * Gets the DMaaP access profiles for the specified userId.
100          * 
101          * @param userId
102          *            User ID
103          * @return List of access profiles
104          */
105         @Override
106         public List<DmaapAccess> getDmaapAccessList(final String userId) {
107                 String url = baseUrl + endpointPath + "?userId=" + userId;
108                 ResponseEntity<List<DmaapAccess>> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
109                                 new ParameterizedTypeReference<List<DmaapAccess>>() {
110                                 });
111                 List<DmaapAccess> daList = daResponse.getBody();
112                 return daList;
113         }
114
115         /**
116          * Gets the specified DMaaP access profile.
117          */
118         @Override
119         public DmaapAccess getDmaapAccess(Long dmaapId) {
120                 String url = baseUrl + endpointPath + "?dmaapId=" + dmaapId;
121                 ResponseEntity<DmaapAccess> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
122                                 new ParameterizedTypeReference<DmaapAccess>() {
123                                 });
124                 DmaapAccess da = daResponse.getBody();
125                 return da;
126         }
127
128         /**
129          * POSTs or PUTs the DMaaP access profile as needed, based on whether the
130          * object's ID field is set. If not set it creates a new row; if set, it
131          * updates a row in the remote service table.
132          * 
133          * @param dmaapAccess
134          *            Access profile
135          */
136         @Override
137         public void saveDmaapAccess(final DmaapAccess dmaapAccess) {
138                 if (dmaapAccess.getId() == null) {
139                         String url = baseUrl + endpointPath;
140                         restTemplate.postForObject(url, dmaapAccess, String.class);
141                 } else {
142                         String url = baseUrl + endpointPath + "/" + Long.toString(dmaapAccess.getId());
143                         restTemplate.put(url, dmaapAccess);
144                 }
145         }
146
147         /**
148          * Deletes the DMaaP access profile row in the remote service table with the
149          * specified id.
150          * 
151          * @param id
152          *            Access profile ID
153          */
154         @Override
155         public void deleteDmaapAccess(final Long id) {
156                 String url = baseUrl + endpointPath + "/" + Long.toString(id);
157                 restTemplate.delete(url);
158         }
159
160         /**
161          * Simple test
162          * 
163          * @param args
164          *            UserID
165          * @throws Exception
166          *             On any failure
167          */
168         public static void main(String[] args) throws Exception {
169                 if (args.length != 1)
170                         throw new IllegalArgumentException("Single argument expected: userid");
171                 DbcUsvcRestClient client = new DbcUsvcRestClient("http://localhost:8081/dbus", "dbus_user", "dbus_pass");
172                 final String userId = args[0];
173                 System.out.println("Requesting profiles for user " + userId);
174                 List<DmaapAccess> access = client.getDmaapAccessList(userId);
175                 if (access == null)
176                         System.err.println("Received null");
177                 else
178                         for (DmaapAccess da : access)
179                                 System.out.println(da);
180         }
181
182 }