2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.input.cache;
19 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_DIRECTORY;
20 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_JSON_PATTERN;
21 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON;
22 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON_PATTERN;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
32 import org.onap.cli.fw.conf.OnapCommandConstants;
33 import org.onap.cli.fw.error.OnapCommandLoadProfileFailed;
34 import org.onap.cli.fw.error.OnapCommandPersistProfileFailed;
35 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
36 import org.springframework.core.io.Resource;
38 import com.fasterxml.jackson.databind.ObjectMapper;
40 public class OnapCommandParameterCache {
42 private Map<String, Map<String, String>> paramCache = new HashMap<>();
44 private static OnapCommandParameterCache single = null;
46 private String profileName = OnapCommandConstants.PARAM_CACHE_FILE_NAME;
48 private OnapCommandParameterCache() {
52 public static OnapCommandParameterCache getInstance() {
54 single = new OnapCommandParameterCache();
61 public void includeProfile(String profile) {
62 this.load(profile, true);
65 public void excludeProfile(String profile) {
66 this.load(profile, false);
69 public void add(String productVersion, String paramName, String paramValue) {
71 if (!paramCache.containsKey(productVersion)) {
72 paramCache.put(productVersion, new HashMap<String, String>());
75 paramCache.get(productVersion).put(paramName, paramValue);
80 public void remove(String productVersion, String paramName) {
81 if (paramCache.containsKey(productVersion)) {
82 if (paramCache.get(productVersion).containsKey(paramName)) {
83 paramCache.get(productVersion).remove(paramName);
90 public Map<String, String> getParams(String productVersion) {
91 if (paramCache.containsKey(productVersion)) {
92 return this.paramCache.get(productVersion);
94 return new HashMap<>();
98 private void persist() {
99 List<OnapCommandParamEntity> params = new ArrayList<>();
101 for (Map.Entry<String, Map<String, String>> p: this.paramCache.entrySet()) {
102 for (Map.Entry<String, String> paramEntry: p.getValue().entrySet()) {
104 OnapCommandParamEntity param = new OnapCommandParamEntity();
105 param.setProduct(p.getKey());
106 param.setName(paramEntry.getKey());
107 param.setValue(paramEntry.getValue());
114 this.persistProfile(params, this.profileName);
115 } catch (OnapCommandPersistProfileFailed e) {
116 throw new RuntimeException(e); // NOSONAR
120 private void load() {
121 this.load(this.profileName, true);
124 private void load(String profileName, boolean include) {
125 List<OnapCommandParamEntity> params= new ArrayList<>();
127 params = this.loadParamFromCache(profileName);
128 } catch (OnapCommandLoadProfileFailed e) {
129 throw new RuntimeException(e); // NOSONAR
132 for (OnapCommandParamEntity p : params) {
134 this.add(p.getProduct(), p.getName(), p.getValue());
136 this.remove(p.getProduct(), p.getName());
141 public void setProfile(String profileName) {
142 this.profileName = profileName;
143 this.paramCache.clear();
147 private void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
148 if (params != null) {
150 Resource[] resources = OnapCommandDiscoveryUtils.findResources(DATA_DIRECTORY);
151 if (resources != null && resources.length == 1) {
152 String path = resources[0].getURI().getPath();
153 File file = new File(path + File.separator + profileName + DATA_PATH_PROFILE_JSON);
154 ObjectMapper mapper = new ObjectMapper();
155 mapper.writerWithDefaultPrettyPrinter().writeValue(file, params);
157 } catch (IOException e1) {
158 throw new OnapCommandPersistProfileFailed(e1);
163 private List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandLoadProfileFailed {
164 List<OnapCommandParamEntity> params = new ArrayList<>();
167 Resource resource = OnapCommandDiscoveryUtils.findResource(profileName + DATA_PATH_PROFILE_JSON,
168 DATA_PATH_JSON_PATTERN);
169 if (resource != null) {
170 File file = new File(resource.getURI().getPath());
171 ObjectMapper mapper = new ObjectMapper();
172 OnapCommandParamEntity[] list = mapper.readValue(file, OnapCommandParamEntity[].class);
173 params.addAll(Arrays.asList(list));
175 } catch (IOException e) {
176 throw new OnapCommandLoadProfileFailed(e);
182 public List<String> getProfiles() {
183 List<String> profiles = new ArrayList<>();
185 Resource[] resources;
187 resources = OnapCommandDiscoveryUtils.findResources(DATA_PATH_PROFILE_JSON_PATTERN);
188 } catch (IOException e) {
189 throw new RuntimeException(e); // NOSONAR
192 if (resources != null && resources.length > 0) {
193 for (Resource res : resources) {
194 String profile = res.getFilename().substring(0, res.getFilename().indexOf(DATA_PATH_PROFILE_JSON));
195 profiles.add(profile);