Epic-231:versioning, and backup the configuration
[sdnc/oam.git] / configbackuprestore / vnfconfigbackupservice / src / main / java / com / onap / sdnc / vnfbackupservice / scheduler / VnfRestClient.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : SDNC-FEATURES
4 * ================================================================================
5 * Copyright 2018 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20 package com.onap.sdnc.vnfbackupservice.scheduler;
21
22
23 import java.util.Arrays;
24
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpEntity;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.HttpMethod;
29 import org.springframework.http.MediaType;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.client.RestTemplate;
33
34
35 @Service
36 public class VnfRestClient {
37
38         @Autowired
39         private RestTemplate restTemplate;
40
41         public String vnfRestClient(String url, String userName, String password) {
42                 restTemplate = new RestTemplate();
43                 HttpHeaders headers = new HttpHeaders();
44                 HttpEntity<String> entity = new HttpEntity<String>(generateHeaders(headers, userName, password));
45                 ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
46                 String result = response.getBody();
47                 return result;
48         }
49         
50         public HttpHeaders generateHeaders(HttpHeaders headers, String userName, String password) {
51                 headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
52                 headers.setContentType(MediaType.APPLICATION_JSON);
53                 String base64Username = userName + ":" + password;
54                 byte[] message = base64Username.getBytes();
55                 headers.set("Authorization", "Basic " + java.util.Base64.getEncoder().encodeToString(message));
56                 return headers;
57         }
58 }