7c05cedb34e90bc30520d2d5e53596891a81518a
[dcaegen2/services/mapper.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
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 org.onap.universalvesadapter.service;
21
22 import java.net.URI;
23 import java.net.URISyntaxException;
24
25 import org.onap.universalvesadapter.configs.DiskRepoConfiguration;
26 import org.onap.universalvesadapter.exception.ConfigFileReadException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Component;
32 import org.springframework.web.client.RestTemplate;
33
34 /**
35  * Implementation of {@code ConfigFileService} using disk repository.
36  * 
37  * @author kmalbari
38  *
39  */
40 @Component
41 public class DiskRepoConfigFileService implements ConfigFileService {
42
43     private final Logger logger = LoggerFactory.getLogger(this.getClass());    
44     
45     @Autowired
46     private DiskRepoConfiguration diskRepoConfiguration;
47     
48     private RestTemplate restTemplate;
49     
50     private URI uri = null;
51
52     /* (non-Javadoc)
53      * @see org.onap.universalvesadapter.service.ConfigFileService#readConfigFile(java.lang.String)
54      */
55     @Override
56     public String readConfigFile(String fileName) throws ConfigFileReadException {
57         logger.debug("Reading config file for " + fileName);
58         if (null == uri) {
59             try {
60                 uri = new URI(diskRepoConfiguration.getFileRepositoryUrl() + fileName);
61                 logger.debug("Read URI for " + fileName);
62             } catch (URISyntaxException exception) {
63                 throw new ConfigFileReadException("Unable to read config file for file "
64             + fileName + "\n Reason : " + exception.getMessage(), exception);
65             }
66         }
67         logger.debug("Calling file repo service for URI" + uri);
68         ResponseEntity<String> fileDataEntity = getRestTemplate().getForEntity(uri, String.class);
69         logger.debug("Call completed successfully");
70         return fileDataEntity.getBody();
71     }
72     
73     /**
74      * Instantiates the instance if null and returns it.
75      * 
76      * @return {@code RestTemplate} instance
77      */
78     private RestTemplate getRestTemplate(){
79         
80         if (null == restTemplate) {
81             restTemplate = new RestTemplate();
82         }
83         
84         return restTemplate;
85     }
86
87 }