CMD: Enhace command profile with additional macros
[cli.git] / framework / src / main / java / org / onap / cli / fw / input / cache / OnapCommandParameterCache.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.input.cache;
18
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;
23
24 import java.io.File;
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;
30 import java.util.Map;
31
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;
37
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public class OnapCommandParameterCache {
41
42     private Map<String, Map<String, String>> paramCache = new HashMap<>();
43
44     private static OnapCommandParameterCache single = null;
45
46     private String profileName = OnapCommandConstants.PARAM_CACHE_FILE_NAME;
47
48     private OnapCommandParameterCache() {
49
50     }
51
52     public static OnapCommandParameterCache getInstance() {
53         if (single == null) {
54             single = new OnapCommandParameterCache();
55         }
56
57         single.load();
58         return single;
59     }
60
61     public void includeProfile(String profile) {
62         this.load(profile, true);
63     }
64
65     public void excludeProfile(String profile) {
66         this.load(profile, false);
67     }
68
69     public void add(String productVersion, String paramName, String paramValue) {
70
71         if (!paramCache.containsKey(productVersion)) {
72             paramCache.put(productVersion, new HashMap<String, String>());
73         }
74
75         paramCache.get(productVersion).put(paramName, paramValue);
76
77         this.persist();
78     }
79
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);
84             }
85         }
86
87         this.persist();
88     }
89
90     public Map<String, String> getParams(String productVersion) {
91         if (paramCache.containsKey(productVersion)) {
92             return this.paramCache.get(productVersion);
93         } else {
94             return new HashMap<>();
95         }
96     }
97
98     private void persist() {
99         List<OnapCommandParamEntity> params = new ArrayList<>();
100
101         for (Map.Entry<String, Map<String, String>> p: this.paramCache.entrySet()) {
102             for (Map.Entry<String, String> paramEntry: p.getValue().entrySet()) {
103
104                 OnapCommandParamEntity param = new OnapCommandParamEntity();
105                 param.setProduct(p.getKey());
106                 param.setName(paramEntry.getKey());
107                 param.setValue(paramEntry.getValue());
108
109                 params.add(param);
110              }
111         }
112
113         try {
114             this.persistProfile(params, this.profileName);
115         } catch (OnapCommandPersistProfileFailed e) {
116             throw new RuntimeException(e);   // NOSONAR
117         }
118     }
119
120     private void load() {
121         this.load(this.profileName, true);
122     }
123
124     private void load(String profileName, boolean include) {
125         List<OnapCommandParamEntity> params= new ArrayList<>();
126         try {
127             params = this.loadParamFromCache(profileName);
128         } catch (OnapCommandLoadProfileFailed e) {
129             throw new RuntimeException(e);   // NOSONAR
130         }
131
132         for (OnapCommandParamEntity p : params) {
133             if (include) {
134                 this.add(p.getProduct(), p.getName(), p.getValue());
135             } else {
136                 this.remove(p.getProduct(), p.getName());
137             }
138         }
139     }
140
141     public void setProfile(String profileName) {
142         this.profileName = profileName;
143         this.paramCache.clear();
144         this.load();
145     }
146
147     private void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
148         if (params != null) {
149             try {
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);
156                 }
157             } catch (IOException e1) {
158                 throw new OnapCommandPersistProfileFailed(e1);
159             }
160         }
161     }
162
163     private List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandLoadProfileFailed {
164         List<OnapCommandParamEntity> params = new ArrayList<>();
165
166         try {
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));
174             }
175         } catch (IOException e) {
176             throw new OnapCommandLoadProfileFailed(e);
177         }
178
179         return params;
180     }
181
182     public List<String> getProfiles() {
183         List<String> profiles = new ArrayList<>();
184
185         Resource[] resources;
186         try {
187             resources = OnapCommandDiscoveryUtils.findResources(DATA_PATH_PROFILE_JSON_PATTERN);
188         } catch (IOException e) {
189              throw new RuntimeException(e);   // NOSONAR
190         }
191
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);
196             }
197         }
198
199         return profiles;
200     }
201 }