Added UniversalVesAdapter in the Mapper
[dcaegen2/services/mapper.git] / UniversalVesAdapter / src / main / java / org / onap / universalvesadapter / service / DiskRepoConfigFileService.java
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 = new 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());
65                         }
66                 }
67                 LOGGER.debug("Calling file repo service for URI" + uri);
68                 ResponseEntity<String> fileDataEntity = restTemplate.getForEntity(uri, String.class);
69                 LOGGER.debug("Call completed successfully");
70                 return fileDataEntity.getBody();
71         }
72
73 }