aboutsummaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/dbca-common/src/main/java/org/openecomp/dcae/dmaapbc/dbcapp/rest/DbcUsvcRestClient.java
blob: 3700c3da8b6ed26aaed34efd5a45dd457783756c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.openecomp.dcae.dmaapbc.dbcapp.rest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openecomp.dcae.dmaapbc.dbcapp.domain.DmaapAccess;
import org.openecomp.dcae.dmaapbc.dbcapp.domain.ManifestTransportModel;
import org.openecomp.dcae.dmaapbc.dbcapp.service.DmaapAccessService;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * Provides methods for accessing the DBC microservice via REST using basic HTTP
 * authentication.
 * 
 */
public class DbcUsvcRestClient implements DmaapAccessService {

	public static final String endpointPath = "/dmaap_access";
	private final String baseUrl;
	private final RestTemplate restTemplate;

	/**
	 * Builds a restTemplate that uses basic HTTP authentication for use by all
	 * methods in this class.
	 * 
	 * @param webapiUrl
	 *            URL of the web endpoint
	 * @param user
	 *            user name
	 * @param pass
	 *            password
	 */
	public DbcUsvcRestClient(String webapiUrl, String user, String pass) {
		if (webapiUrl == null || user == null || pass == null)
			throw new IllegalArgumentException("Nulls not permitted");

		baseUrl = webapiUrl;
		URL url = null;
		try {
			url = new URL(baseUrl);
		} catch (MalformedURLException ex) {
			throw new RuntimeException("Failed to parse URL", ex);
		}
		final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort());

		// Build a client with a credentials provider
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass));
		HttpClientBuilder clientBuilder = HttpClientBuilder.create();
		CloseableHttpClient httpClient = clientBuilder.setDefaultCredentialsProvider(credsProvider).build();

		// Create request factory with our superpower client
		HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(
				httpHost);
		requestFactory.setHttpClient(httpClient);

		// Put the factory in the template
		this.restTemplate = new RestTemplate();
		restTemplate.setRequestFactory(requestFactory);
	}

	/**
	 * Gets the manifest from the micro service.
	 */
	@Override
	public ManifestTransportModel getManifest() {
		String url = baseUrl + "/manifest";
		ResponseEntity<ManifestTransportModel> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
				ManifestTransportModel.class);
		ManifestTransportModel response = daResponse.getBody();
		return response;
	}

	/**
	 * Gets the count of DMaaP access profiles.
	 * 
	 * @return Number of access profiles in the database.
	 */
	public int getDmaapAccessCount() {
		String url = baseUrl + "/count_dmaap_access";
		ResponseEntity<DbcUsvcRestResponse> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
				DbcUsvcRestResponse.class);
		DbcUsvcRestResponse response = daResponse.getBody();
		return response.getStatus();
	}

	/**
	 * Gets the DMaaP access profiles for the specified userId.
	 * 
	 * @param userId
	 *            User ID
	 * @return List of access profiles
	 */
	@Override
	public List<DmaapAccess> getDmaapAccessList(final String userId) {
		String url = baseUrl + endpointPath + "?userId=" + userId;
		ResponseEntity<List<DmaapAccess>> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<List<DmaapAccess>>() {
				});
		List<DmaapAccess> daList = daResponse.getBody();
		return daList;
	}

	/**
	 * Gets the specified DMaaP access profile.
	 */
	@Override
	public DmaapAccess getDmaapAccess(Long dmaapId) {
		String url = baseUrl + endpointPath + "?dmaapId=" + dmaapId;
		ResponseEntity<DmaapAccess> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<DmaapAccess>() {
				});
		DmaapAccess da = daResponse.getBody();
		return da;
	}

	/**
	 * POSTs or PUTs the DMaaP access profile as needed, based on whether the
	 * object's ID field is set. If not set it creates a new row; if set, it
	 * updates a row in the remote service table.
	 * 
	 * @param dmaapAccess
	 *            Access profile
	 */
	@Override
	public void saveDmaapAccess(final DmaapAccess dmaapAccess) {
		if (dmaapAccess.getId() == null) {
			String url = baseUrl + endpointPath;
			restTemplate.postForObject(url, dmaapAccess, String.class);
		} else {
			String url = baseUrl + endpointPath + "/" + Long.toString(dmaapAccess.getId());
			restTemplate.put(url, dmaapAccess);
		}
	}

	/**
	 * Deletes the DMaaP access profile row in the remote service table with the
	 * specified id.
	 * 
	 * @param id
	 *            Access profile ID
	 */
	@Override
	public void deleteDmaapAccess(final Long id) {
		String url = baseUrl + endpointPath + "/" + Long.toString(id);
		restTemplate.delete(url);
	}

	/**
	 * Simple test
	 * 
	 * @param args
	 *            UserID
	 * @throws Exception
	 *             On any failure
	 */
	public static void main(String[] args) throws Exception {
		if (args.length != 1)
			throw new IllegalArgumentException("Single argument expected: userid");
		DbcUsvcRestClient client = new DbcUsvcRestClient("http://localhost:8081/dbus", "dbus_user", "dbus_pass");
		final String userId = args[0];
		System.out.println("Requesting profiles for user " + userId);
		List<DmaapAccess> access = client.getDmaapAccessList(userId);
		if (access == null)
			System.err.println("Received null");
		else
			for (DmaapAccess da : access)
				System.out.println(da);
	}

}