2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.client.namingservice;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.onap.namingservice.model.NameGenDeleteRequest;
30 import org.onap.namingservice.model.NameGenDeleteResponse;
31 import org.onap.namingservice.model.NameGenRequest;
32 import org.onap.namingservice.model.NameGenResponse;
33 import org.onap.so.client.exception.BadResponseException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.core.env.Environment;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Component;
44 import org.springframework.web.client.HttpStatusCodeException;
45 import org.springframework.web.client.RestTemplate;
50 public class NamingClient{
51 private static final Logger logger = LoggerFactory.getLogger(NamingClient.class);
52 private static final String ENDPOINT = "mso.naming.endpoint";
53 private static final String AUTH = "mso.naming.auth";
56 private RestTemplate restTemplate;
58 private Environment env;
60 private NamingClientResponseValidator namingClientResponseValidator;
62 public String postNameGenRequest(NameGenRequest request) throws BadResponseException, IOException {
63 String targetUrl = env.getProperty(ENDPOINT);
64 HttpHeaders headers = setHeaders(env.getProperty(AUTH));
65 logger.info("Sending postNameGenRequest to url: {}", targetUrl);
66 HttpEntity<NameGenRequest> requestEntity = new HttpEntity<>(request, headers);
67 ResponseEntity<NameGenResponse> response;
69 response = restTemplate.postForEntity(targetUrl, requestEntity, NameGenResponse.class);
70 }catch(HttpStatusCodeException e){
71 throw new BadResponseException(namingClientResponseValidator.formatError(e));
73 return namingClientResponseValidator.validateNameGenResponse(response);
76 public String deleteNameGenRequest(NameGenDeleteRequest request) throws BadResponseException, IOException {
77 String targetUrl = env.getProperty(ENDPOINT);
78 HttpHeaders headers = setHeaders(env.getProperty(AUTH));
79 logger.info("Sending deleteNameGenRequest to url: {}", targetUrl);
80 HttpEntity<NameGenDeleteRequest> requestEntity = new HttpEntity<>(request, headers);
81 ResponseEntity<NameGenDeleteResponse> response;
83 response = restTemplate.exchange(targetUrl, HttpMethod.DELETE, requestEntity, NameGenDeleteResponse.class);
84 }catch(HttpStatusCodeException e){
85 throw new BadResponseException(namingClientResponseValidator.formatError(e));
87 return namingClientResponseValidator.validateNameGenDeleteResponse(response);
90 private HttpHeaders setHeaders(String auth) {
91 HttpHeaders headers = new HttpHeaders();
92 headers.setContentType(MediaType.APPLICATION_JSON);
93 List<MediaType> acceptableMediaTypes = new ArrayList<>();
94 acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
95 headers.setAccept(acceptableMediaTypes);
96 headers.add(HttpHeaders.AUTHORIZATION, auth);