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.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.apache.commons.io.FileUtils;
32 import org.onap.cli.fw.conf.OnapCommandConfig;
33 import org.onap.cli.fw.conf.OnapCommandConstants;
34 import org.onap.cli.fw.error.OnapCommandException;
35 import org.onap.cli.fw.error.OnapCommandPersistProfileFailed;
36 import org.onap.cli.fw.error.OnapCommandProfileLoadFailed;
37 import org.onap.cli.fw.input.cache.OnapCommandParamEntity;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.gson.Gson;
42 import com.google.gson.GsonBuilder;
43 import java.io.FileWriter;
44 import java.io.FileReader;
45 import java.io.Writer;
46 import java.io.Reader;
50 public class OnapCommandProfileStore {
51 private static Logger log = LoggerFactory.getLogger(OnapCommandProfileStore.class);
52 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
54 private Map<String, Map<String, String>> paramCache = new HashMap<>();
56 private static OnapCommandProfileStore single = null;
58 private String profileName = OnapCommandConstants.PARAM_CACHE_FILE_NAME;
62 FileUtils.forceMkdir(new File(getDataStorePath()));
63 } catch (IOException e) {
64 log.error("Failed to create the data store profile");
67 private OnapCommandProfileStore() {
71 public static OnapCommandProfileStore getInstance() {
73 single = new OnapCommandProfileStore();
80 public void includeProfile(String profile) throws OnapCommandException {
81 this.load(profile, true);
84 public void excludeProfile(String profile) throws OnapCommandException {
85 this.load(profile, false);
88 public void add(String productVersion, String paramName, String paramValue) {
90 if (!paramCache.containsKey(productVersion)) {
91 paramCache.put(productVersion, new HashMap<>());
94 paramCache.get(productVersion).put(paramName, paramValue);
99 public void remove(String productVersion, String paramName) {
100 if (paramCache.containsKey(productVersion) && paramCache.get(productVersion).containsKey(paramName)) {
101 paramCache.get(productVersion).remove(paramName);
107 public Map<String, String> getParams(String productVersion) {
108 //default profile used across products, set in profile-set command
109 Map<String, String> map = new HashMap<>();
111 if (paramCache.containsKey(OnapCommandConstants.OCLIP_GLOBAL_PROFILE)) {
112 map = this.paramCache.get(OnapCommandConstants.OCLIP_GLOBAL_PROFILE);
115 if (paramCache.containsKey(productVersion)) {
116 map.putAll(this.paramCache.get(productVersion));
122 private void persist() {
123 List<OnapCommandParamEntity> params = new ArrayList<>();
125 for (Map.Entry<String, Map<String, String>> p: this.paramCache.entrySet()) {
126 for (Map.Entry<String, String> paramEntry: p.getValue().entrySet()) {
128 OnapCommandParamEntity param = new OnapCommandParamEntity();
129 param.setProduct(p.getKey());
130 param.setName(paramEntry.getKey());
131 param.setValue(paramEntry.getValue());
138 this.persistProfile(params, this.profileName);
139 } catch (OnapCommandPersistProfileFailed e) {
140 throw new RuntimeException(e); // NOSONAR
144 private void load() throws OnapCommandException {
145 this.load(this.profileName, true);
148 private void load(String profileName, boolean include) throws OnapCommandException {
149 List<OnapCommandParamEntity> params = this.loadParamFromCache(profileName);
151 for (OnapCommandParamEntity p : params) {
153 this.add(p.getProduct(), p.getName(), p.getValue());
155 this.remove(p.getProduct(), p.getName());
160 public void setProfile(String profileName) throws OnapCommandException {
161 this.profileName = profileName;
162 this.paramCache.clear();
166 public static String getDataStorePath() {
167 return OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_DATA_DIR)
168 + File.separator + "profiles";
171 public void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
172 if (params != null) {
173 String dataDir = getDataStorePath();
174 File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
175 try (Writer writer = new FileWriter(file)){
176 gson.toJson(params, writer);
177 } catch (Exception e1) { // NOSONAR
178 throw new OnapCommandPersistProfileFailed(e1);
183 public List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandException {
184 List<OnapCommandParamEntity> params = new ArrayList<>();
185 String dataDir = getDataStorePath();
186 File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
188 try (Reader jsonReader = new FileReader(file)){
190 OnapCommandParamEntity[] list = gson.fromJson(jsonReader,
191 OnapCommandParamEntity[].class);
192 params.addAll(Arrays.asList(list));
194 // throw new OnapCommandProfileNotFound(profileName);
195 } catch (Exception e) { // NOSONAR
196 throw new OnapCommandProfileLoadFailed(e);
203 public void removeProfile(String profile) {
204 String dataDir = getDataStorePath();
205 File file = new File(dataDir + File.separator + profile + DATA_PATH_PROFILE_JSON);
207 Files.delete(Paths.get(dataDir + File.separator + profile + DATA_PATH_PROFILE_JSON));
208 } catch (IOException e) {
209 String fileAbsPath = file.getAbsolutePath();
210 log.error("Failed to delete profile {}", fileAbsPath);
214 public List<String> getProfiles() {
215 List<String> profiles = new ArrayList<>();
217 String dataDir = getDataStorePath();
218 for (File file: new File(dataDir).listFiles((dir, name) -> name.endsWith(DATA_PATH_PROFILE_JSON))) {
219 String profile = file.getName().substring(0, file.getName().indexOf(DATA_PATH_PROFILE_JSON));
220 profiles.add(profile);