aboutsummaryrefslogtreecommitdiffstats
path: root/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/RestInventoryClientImpl.java
blob: 6389db37e611e61cb408df498d4ebb1e809ded41 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package org.onap.ccsdk.dashboard.rest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.onap.ccsdk.dashboard.exceptions.inventory.ServiceAlreadyDeactivatedException;
import org.onap.ccsdk.dashboard.exceptions.inventory.ServiceNotFoundException;
import org.onap.ccsdk.dashboard.exceptions.inventory.ServiceTypeAlreadyDeactivatedException;
import org.onap.ccsdk.dashboard.exceptions.inventory.ServiceTypeNotFoundException;
import org.onap.ccsdk.dashboard.model.inventory.ApiResponseMessage;
import org.onap.ccsdk.dashboard.model.inventory.InventoryProperty;
import org.onap.ccsdk.dashboard.model.inventory.Link;
import org.onap.ccsdk.dashboard.model.inventory.Service;
import org.onap.ccsdk.dashboard.model.inventory.ServiceGroupByResults;
import org.onap.ccsdk.dashboard.model.inventory.ServiceList;
import org.onap.ccsdk.dashboard.model.inventory.ServiceQueryParams;
import org.onap.ccsdk.dashboard.model.inventory.ServiceRef;
import org.onap.ccsdk.dashboard.model.inventory.ServiceRefList;
import org.onap.ccsdk.dashboard.model.inventory.ServiceRequest;
import org.onap.ccsdk.dashboard.model.inventory.ServiceType;
import org.onap.ccsdk.dashboard.model.inventory.ServiceTypeList;
import org.onap.ccsdk.dashboard.model.inventory.ServiceTypeQueryParams;
import org.onap.ccsdk.dashboard.model.inventory.ServiceTypeRequest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;

public class RestInventoryClientImpl extends RestClientBase implements InventoryClient {

	private final String baseUrl;
	//private final RestTemplate restTemplate;
	public static final String SERVICE_TYPES = "dcae-service-types";
	public static final String SERVICES = "dcae-services";
	public static final String SERVICES_GROUPBY = "dcae-services-groupby";
	
	public RestInventoryClientImpl(String webapiUrl) {
		this(webapiUrl, null, null);
	}

	/**
	 * Builds a restTemplate. If username and password are supplied, uses basic
	 * HTTP authentication.
	 * 
	 * @param webapiUrl
	 *            URL of the web endpoint
	 * @param user
	 *            user name; ignored if null
	 * @param pass
	 *            password
	 */
	public RestInventoryClientImpl(String webapiUrl, String user, String pass) {
		super();
		if (webapiUrl == null)
			throw new IllegalArgumentException("Null URL not permitted");
		URL url = null;
		String urlScheme = "http";
		try {
			url = new URL(webapiUrl);
			baseUrl = url.toExternalForm();
		} catch (MalformedURLException ex) {
			throw new RuntimeException("Failed to parse URL", ex);
		}
		urlScheme = webapiUrl.split(":")[0];
		createRestTemplate(url, user, pass, urlScheme);
	}
		
	public Stream<ServiceType> getServiceTypes() {
		String url = buildUrl(new String[] {baseUrl, SERVICE_TYPES}, null);
		ResponseEntity<ServiceTypeList> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceTypeList>() {
				});
		Collection<ServiceType> collection = response.getBody().items;

		// Continue retrieving items on the next page if they exist
		Link nextLink = response.getBody().paginationLinks.nextLink;
		while (nextLink != null) {
			url = response.getBody().paginationLinks.nextLink.href;
			response = restTemplate.exchange(url, HttpMethod.GET, null,
					new ParameterizedTypeReference<ServiceTypeList>() {
					});
			collection.addAll(response.getBody().items);
			nextLink = response.getBody().paginationLinks.nextLink;
		}

		return collection.stream();
	}

	public Stream<ServiceType> getServiceTypes(ServiceTypeQueryParams serviceTypeQueryParams) {
		
		// Only utilize the parameters that aren't null
		HashMap<String, String> map = new HashMap<>();
		if (serviceTypeQueryParams.getTypeName() != null) {
			map.put("typeName", serviceTypeQueryParams.getTypeName());
		}
		if (serviceTypeQueryParams.getOnlyLatest() != null) {
			map.put("onlyLatest", Boolean.toString(serviceTypeQueryParams.getOnlyLatest()));
		}
		if (serviceTypeQueryParams.getOnlyActive() != null) {
			map.put("onlyActive", Boolean.toString(serviceTypeQueryParams.getOnlyActive()));
		}
		if (serviceTypeQueryParams.getVnfType() != null) {
			map.put("vnfType", serviceTypeQueryParams.getVnfType());
		}
		if (serviceTypeQueryParams.getServiceId() != null) {
			map.put("serviceId", serviceTypeQueryParams.getServiceId());
		}
		if (serviceTypeQueryParams.getServiceLocation() != null) {
			map.put("serviceLocation", serviceTypeQueryParams.getServiceLocation());
		}
		if (serviceTypeQueryParams.getAsdcServiceId() != null) {
			map.put("asdcServiceId", serviceTypeQueryParams.getAsdcServiceId());
		}
		if (serviceTypeQueryParams.getAsdcResourceId() != null) {
			map.put("asdcResourceId", serviceTypeQueryParams.getAsdcResourceId());
		}
		ArrayList<String> params = new ArrayList<>();
		for (Entry<String, String> ent : map.entrySet()) {
			params.add(ent.getKey());
			params.add(ent.getValue());
		}
		
		String url = buildUrl(new String[] {baseUrl, SERVICE_TYPES}, params.toArray(new String[params.size()]));
		ResponseEntity<ServiceTypeList> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceTypeList>() {
				});
		Collection<ServiceType> collection = response.getBody().items;

		// Continue retrieving items on the next page if they exist
		Link nextLink = response.getBody().paginationLinks.nextLink;
		while (nextLink != null) {
			url = response.getBody().paginationLinks.nextLink.href;
			response = restTemplate.exchange(url, HttpMethod.GET, null,
					new ParameterizedTypeReference<ServiceTypeList>() {
					});
			collection.addAll(response.getBody().items);
			nextLink = response.getBody().paginationLinks.nextLink;
		}

		return collection.stream();
	}
	

	public ServiceType addServiceType(ServiceType serviceType) {
		String url = buildUrl(new String[] { baseUrl, SERVICE_TYPES }, null);
		
		//Take the ServiceType object and create a ServiceTypeRequest from it
		ServiceTypeRequest serviceTypeRequest = ServiceTypeRequest.from(serviceType);
		
		return restTemplate.postForObject(url, serviceTypeRequest, ServiceType.class);
	}
	
	public ServiceType addServiceType(ServiceTypeRequest serviceTypeRequest) {
		String url = buildUrl(new String[] { baseUrl, SERVICE_TYPES }, null);
		
		return restTemplate.postForObject(url, serviceTypeRequest, ServiceType.class);
	}
	
	public Optional<ServiceType> getServiceType(String typeId) {
		String url = buildUrl(new String[] {baseUrl, SERVICE_TYPES, typeId}, null);
		ResponseEntity<ServiceType> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceType>() {
				});
		return Optional.ofNullable(response.getBody());
	}

	public void deleteServiceType(String typeId) throws ServiceTypeNotFoundException, ServiceTypeAlreadyDeactivatedException {
		String url = buildUrl(new String[] {baseUrl, SERVICE_TYPES, typeId}, null);
		try {
			restTemplate.exchange(url, HttpMethod.DELETE, null,
				new ParameterizedTypeReference<ApiResponseMessage>() {
				});
		} catch (HttpClientErrorException e) {
			if (e.getStatusCode().value() == 410) {
				throw new ServiceTypeAlreadyDeactivatedException(e.getMessage());
			}
			else if (e.getStatusCode().value() == 404) {
				throw new ServiceTypeNotFoundException(e.getMessage());
			}
		}
	}

	
	public Stream<Service> getServices() {
		String url = buildUrl(new String[] {baseUrl, SERVICES}, null);
		ResponseEntity<ServiceList> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceList>() {
				});
		Collection<Service> collection = response.getBody().items;

		// Continue retrieving items on the next page if they exist
		Link nextLink = response.getBody().paginationLinks.nextLink;
		while (nextLink != null) {
			url = response.getBody().paginationLinks.nextLink.href;
			response = restTemplate.exchange(url, HttpMethod.GET, null,
					new ParameterizedTypeReference<ServiceList>() {
					});
			collection.addAll(response.getBody().items);
			nextLink = response.getBody().paginationLinks.nextLink;
		}

		return collection.stream();
	}

	public ServiceRefList getServicesForType(ServiceQueryParams serviceQueryParams) {
		
		// Only utilize the typeId 
		HashMap<String, String> map = new HashMap<>();
		if (serviceQueryParams.getTypeId() != null) {
			map.put("typeId", serviceQueryParams.getTypeId());
		}
		ArrayList<String> params = new ArrayList<>();
		for (Entry<String, String> ent : map.entrySet()) {
			params.add(ent.getKey());
			params.add(ent.getValue());
		}
		
		String url = buildUrl(new String[] {baseUrl, SERVICES}, params.toArray(new String[params.size()]));
		ResponseEntity<ServiceList> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceList>() {
				});
		Collection<Service> collection = response.getBody().items;
		int itemCnt = response.getBody().totalCount;
		
		// Continue retrieving items on the next page if they exist
		Link nextLink = response.getBody().paginationLinks.nextLink;
		while (nextLink != null) {
			url = response.getBody().paginationLinks.nextLink.href;
			response = restTemplate.exchange(url, HttpMethod.GET, null,
					new ParameterizedTypeReference<ServiceList>() {
					});
			collection.addAll(response.getBody().items);
			nextLink = response.getBody().paginationLinks.nextLink;
		}
		
		List<ServiceRef> srvcRefList = 
				collection.stream().map(e->e.createServiceRef()).collect(Collectors.toList());
		
		return new ServiceRefList(srvcRefList, itemCnt);
	}
	
	public Stream<Service> getServices(ServiceQueryParams serviceQueryParams) {
		
		// Only utilize the parameters that aren't null
		HashMap<String, String> map = new HashMap<>();
		if (serviceQueryParams.getTypeId() != null) {
			map.put("typeId", serviceQueryParams.getTypeId());
		}
		if (serviceQueryParams.getVnfId() != null) {
			map.put("vnfId", serviceQueryParams.getVnfId());
		}
		if (serviceQueryParams.getVnfType() != null) {
			map.put("vnfType", serviceQueryParams.getVnfType());
		}
		if (serviceQueryParams.getVnfLocation() != null) {
			map.put("vnfLocation", serviceQueryParams.getVnfLocation());
		}
		if (serviceQueryParams.getComponentType() != null) {
			map.put("componentType", serviceQueryParams.getComponentType());
		}
		if (serviceQueryParams.getShareable() != null) {
			map.put("shareable", Boolean.toString(serviceQueryParams.getShareable()));
		}
		if (serviceQueryParams.getCreated() != null) {
			map.put("created", serviceQueryParams.getCreated());
		}
		ArrayList<String> params = new ArrayList<>();
		for (Entry<String, String> ent : map.entrySet()) {
			params.add(ent.getKey());
			params.add(ent.getValue());
		}
		
		String url = buildUrl(new String[] {baseUrl, SERVICES}, params.toArray(new String[params.size()]));
		ResponseEntity<ServiceList> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceList>() {
				});
		Collection<Service> collection = response.getBody().items;

		// Continue retrieving items on the next page if they exist
		Link nextLink = response.getBody().paginationLinks.nextLink;
		while (nextLink != null) {
			url = response.getBody().paginationLinks.nextLink.href;
			response = restTemplate.exchange(url, HttpMethod.GET, null,
					new ParameterizedTypeReference<ServiceList>() {
					});
			collection.addAll(response.getBody().items);
			nextLink = response.getBody().paginationLinks.nextLink;
		}

		return collection.stream();
	}	

	public Set<InventoryProperty> getPropertiesOfServices(String propertyName) {
		String url = buildUrl(new String[] {baseUrl, SERVICES_GROUPBY, propertyName}, null);
		ResponseEntity<ServiceGroupByResults> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<ServiceGroupByResults>() {
				});
		return response.getBody().propertyValues;
	}

	public Optional<Service> getService(String serviceId) {
		String url = buildUrl(new String[] {baseUrl, SERVICES, serviceId}, null);
		ResponseEntity<Service> response = restTemplate.exchange(url, HttpMethod.GET, null,
				new ParameterizedTypeReference<Service>() {
				});
		return Optional.ofNullable(response.getBody());
	}
	
	public void putService(String typeId, Service service) {
		String url = buildUrl(new String[] {baseUrl, SERVICES, service.getServiceId()}, null);
		
		ServiceRequest serviceRequest = ServiceRequest.from(typeId, service);
		
		restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<ServiceRequest>(serviceRequest),
				new ParameterizedTypeReference<Service>() {
		});
	}
	
	public void deleteService(String serviceId) throws ServiceNotFoundException, ServiceAlreadyDeactivatedException {
		String url = buildUrl(new String[] {baseUrl, SERVICES, serviceId}, null);
		try {
			restTemplate.exchange(url, HttpMethod.DELETE, null,
					new ParameterizedTypeReference<ApiResponseMessage>() {
					});
		} catch (HttpClientErrorException e) {
			if (e.getStatusCode().value() == 410) {
				throw new ServiceAlreadyDeactivatedException(e.getMessage());
			}
			else if (e.getStatusCode().value() == 404) {
				throw new ServiceNotFoundException(e.getMessage());
			}
		}
	}
}