CVC: Add support for execution, schema, product
[cli.git] / framework / src / main / java / org / onap / cli / fw / store / OnapCommandProfileStore.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.store;
18
19 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON;
20
21 import java.io.File;
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;
28 import java.util.Map;
29
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;
40
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 public class OnapCommandProfileStore {
44     private static Logger log = LoggerFactory.getLogger(OnapCommandProfileStore.class);
45     private Map<String, Map<String, String>> paramCache = new HashMap<>();
46
47     private static OnapCommandProfileStore single = null;
48
49     private String profileName = OnapCommandConstants.PARAM_CACHE_FILE_NAME;
50
51     static {
52         try {
53             FileUtils.forceMkdir(new File(getDataStorePath()));
54         } catch (IOException e) {
55             log.error("Failed to create the data store profile");
56         }
57     }
58     private OnapCommandProfileStore() {
59
60     }
61
62     public static OnapCommandProfileStore getInstance() {
63         if (single == null) {
64             single = new OnapCommandProfileStore();
65         }
66
67         //single.load();
68         return single;
69     }
70
71     public void includeProfile(String profile) throws OnapCommandException {
72         this.load(profile, true);
73     }
74
75     public void excludeProfile(String profile) throws OnapCommandException {
76         this.load(profile, false);
77     }
78
79     public void add(String productVersion, String paramName, String paramValue) {
80
81         if (!paramCache.containsKey(productVersion)) {
82             paramCache.put(productVersion, new HashMap<String, String>());
83         }
84
85         paramCache.get(productVersion).put(paramName, paramValue);
86
87         this.persist();
88     }
89
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);
94             }
95         }
96
97         this.persist();
98     }
99
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<>();
103
104         if (paramCache.containsKey(OnapCommandConstants.OCLIP_GLOBAL_PROFILE)) {
105             map = this.paramCache.get(OnapCommandConstants.OCLIP_GLOBAL_PROFILE);
106         }
107
108         if (paramCache.containsKey(productVersion)) {
109             map.putAll(this.paramCache.get(productVersion));
110         }
111
112         return map;
113     }
114
115     private void persist() {
116         List<OnapCommandParamEntity> params = new ArrayList<>();
117
118         for (Map.Entry<String, Map<String, String>> p: this.paramCache.entrySet()) {
119             for (Map.Entry<String, String> paramEntry: p.getValue().entrySet()) {
120
121                 OnapCommandParamEntity param = new OnapCommandParamEntity();
122                 param.setProduct(p.getKey());
123                 param.setName(paramEntry.getKey());
124                 param.setValue(paramEntry.getValue());
125
126                 params.add(param);
127              }
128         }
129
130         try {
131             this.persistProfile(params, this.profileName);
132         } catch (OnapCommandPersistProfileFailed e) {
133             throw new RuntimeException(e);   // NOSONAR
134         }
135     }
136
137     private void load() throws OnapCommandException {
138         this.load(this.profileName, true);
139     }
140
141     private void load(String profileName, boolean include) throws OnapCommandException {
142         List<OnapCommandParamEntity> params= new ArrayList<>();
143         params = this.loadParamFromCache(profileName);
144
145         for (OnapCommandParamEntity p : params) {
146             if (include) {
147                 this.add(p.getProduct(), p.getName(), p.getValue());
148             } else {
149                 this.remove(p.getProduct(), p.getName());
150             }
151         }
152     }
153
154     public void setProfile(String profileName) throws OnapCommandException {
155         this.profileName = profileName;
156         this.paramCache.clear();
157         this.load();
158     }
159
160     public static String getDataStorePath() {
161         return OnapCommandConfig.getPropertyValue(OnapCommandConstants.OPEN_CLI_DATA_DIR)
162                 + File.separator + "profiles";
163     }
164
165     public void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
166         if (params != null) {
167             String dataDir = getDataStorePath();
168             try {
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);
174             }
175         }
176     }
177
178     public List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandException {
179         List<OnapCommandParamEntity> params = new ArrayList<>();
180         String dataDir = getDataStorePath();
181         try {
182             File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
183             if (file.exists()) {
184                 ObjectMapper mapper = new ObjectMapper();
185                 OnapCommandParamEntity[] list = mapper.readValue(file, OnapCommandParamEntity[].class);
186                 params.addAll(Arrays.asList(list));
187             } else {
188                 throw new OnapCommandProfileNotFound(profileName);
189             }
190         } catch (IOException e) {
191             throw new OnapCommandProfileLoadFailed(e);
192         }
193
194         return params;
195     }
196
197     public void removeProfile(String profile) {
198          String dataDir = getDataStorePath();
199          File file = new File(dataDir + File.separator + profile + DATA_PATH_PROFILE_JSON);
200          if (file.exists()) {
201             file.delete();
202          }
203     }
204
205     public List<String> getProfiles() {
206         List<String> profiles = new ArrayList<>();
207
208         String dataDir = getDataStorePath();
209         for (File file: new File(dataDir).listFiles(new FilenameFilter() {
210             @Override
211             public boolean accept(File dir, String name) {
212                 return name.endsWith(DATA_PATH_PROFILE_JSON);
213             }
214         })) {
215             String profile = file.getName().substring(0, file.getName().indexOf(DATA_PATH_PROFILE_JSON));
216             profiles.add(profile);
217         }
218
219         return profiles;
220     }
221 }