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.store;
19 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
30 import org.apache.commons.io.FileUtils;
31 import org.onap.cli.fw.conf.OnapCommandConfig;
32 import org.onap.cli.fw.conf.OnapCommandConstants;
33 import org.onap.cli.fw.error.OnapCommandException;
34 import org.onap.cli.fw.error.OnapCommandPersistProfileFailed;
35 import org.onap.cli.fw.error.OnapCommandProfileLoadFailed;
36 import org.onap.cli.fw.error.OnapCommandProfileNotFound;
37 import org.onap.cli.fw.input.cache.OnapCommandParamEntity;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.fasterxml.jackson.databind.ObjectMapper;
43 public class OnapCommandProfileStore {
44 private static Logger log = LoggerFactory.getLogger(OnapCommandProfileStore.class);
45 private Map<String, Map<String, String>> paramCache = new HashMap<>();
47 private static OnapCommandProfileStore single = null;
49 private String profileName = OnapCommandConstants.PARAM_CACHE_FILE_NAME;
53 FileUtils.forceMkdir(new File(getDataStorePath()));
54 } catch (IOException e) {
55 log.error("Failed to create the data store profile");
58 private OnapCommandProfileStore() {
62 public static OnapCommandProfileStore getInstance() {
64 single = new OnapCommandProfileStore();
71 public void includeProfile(String profile) throws OnapCommandException {
72 this.load(profile, true);
75 public void excludeProfile(String profile) throws OnapCommandException {
76 this.load(profile, false);
79 public void add(String productVersion, String paramName, String paramValue) {
81 if (!paramCache.containsKey(productVersion)) {
82 paramCache.put(productVersion, new HashMap<String, String>());
85 paramCache.get(productVersion).put(paramName, paramValue);
90 public void remove(String productVersion, String paramName) {
91 if (paramCache.containsKey(productVersion)) {
92 if (paramCache.get(productVersion).containsKey(paramName)) {
93 paramCache.get(productVersion).remove(paramName);
100 public Map<String, String> getParams(String productVersion) {
101 //default profile used across products, set in profile-set command
102 Map<String, String> map = new HashMap<>();
104 if (paramCache.containsKey(OnapCommandConstants.OCLIP_GLOBAL_PROFILE)) {
105 map = this.paramCache.get(OnapCommandConstants.OCLIP_GLOBAL_PROFILE);
108 if (paramCache.containsKey(productVersion)) {
109 map.putAll(this.paramCache.get(productVersion));
115 private void persist() {
116 List<OnapCommandParamEntity> params = new ArrayList<>();
118 for (Map.Entry<String, Map<String, String>> p: this.paramCache.entrySet()) {
119 for (Map.Entry<String, String> paramEntry: p.getValue().entrySet()) {
121 OnapCommandParamEntity param = new OnapCommandParamEntity();
122 param.setProduct(p.getKey());
123 param.setName(paramEntry.getKey());
124 param.setValue(paramEntry.getValue());
131 this.persistProfile(params, this.profileName);
132 } catch (OnapCommandPersistProfileFailed e) {
133 throw new RuntimeException(e); // NOSONAR
137 private void load() throws OnapCommandException {
138 this.load(this.profileName, true);
141 private void load(String profileName, boolean include) throws OnapCommandException {
142 List<OnapCommandParamEntity> params= new ArrayList<>();
143 params = this.loadParamFromCache(profileName);
145 for (OnapCommandParamEntity p : params) {
147 this.add(p.getProduct(), p.getName(), p.getValue());
149 this.remove(p.getProduct(), p.getName());
154 public void setProfile(String profileName) throws OnapCommandException {
155 this.profileName = profileName;
156 this.paramCache.clear();
160 public static String getDataStorePath() {
161 return OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_DATA_DIR)
162 + File.separator + "profiles";
165 public void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
166 if (params != null) {
167 String dataDir = getDataStorePath();
169 File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
170 ObjectMapper mapper = new ObjectMapper();
171 mapper.writerWithDefaultPrettyPrinter().writeValue(file, params);
172 } catch (IOException e1) {
173 throw new OnapCommandPersistProfileFailed(e1);
178 public List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandException {
179 List<OnapCommandParamEntity> params = new ArrayList<>();
180 String dataDir = getDataStorePath();
182 File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
184 ObjectMapper mapper = new ObjectMapper();
185 OnapCommandParamEntity[] list = mapper.readValue(file, OnapCommandParamEntity[].class);
186 params.addAll(Arrays.asList(list));
188 throw new OnapCommandProfileNotFound(profileName);
190 } catch (IOException e) {
191 throw new OnapCommandProfileLoadFailed(e);
197 public void removeProfile(String profile) {
198 String dataDir = getDataStorePath();
199 File file = new File(dataDir + File.separator + profile + DATA_PATH_PROFILE_JSON);
205 public List<String> getProfiles() {
206 List<String> profiles = new ArrayList<>();
208 String dataDir = getDataStorePath();
209 for (File file: new File(dataDir).listFiles(new FilenameFilter() {
211 public boolean accept(File dir, String name) {
212 return name.endsWith(DATA_PATH_PROFILE_JSON);
215 String profile = file.getName().substring(0, file.getName().indexOf(DATA_PATH_PROFILE_JSON));
216 profiles.add(profile);