Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / extensions / ExtTools.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.extensions;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25 import org.reflections.Reflections;
26 import org.reflections.scanners.ResourcesScanner;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.BufferedReader;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.nio.charset.Charset;
34 import java.util.ArrayList;
35 import java.util.LinkedHashMap;
36 import java.util.Set;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 public class ExtTools {
41
42     private static Logger log = LoggerFactory.getLogger(ExtTools.class.getName());
43
44     private static LinkedHashMap<String, Object> extensionInfo = new LinkedHashMap<>();
45
46     public ExtTools() {
47         extensionInfo = loadExtensions();
48     }
49
50     private LinkedHashMap<String, Object> loadExtensions() {
51
52         LinkedHashMap<String, Object> extensions = new LinkedHashMap<>();
53
54         Reflections reflections = new Reflections("extensions", new ResourcesScanner());
55         Set<String> resourcePaths = reflections.getResources(Pattern.compile(".*\\.py$"));
56
57         for (String resourcePath : resourcePaths) {
58             try (InputStream is = ExtTools.class.getClassLoader().getResourceAsStream(resourcePath);
59                  InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
60                  BufferedReader br = new BufferedReader(isr);) {
61                 String version = null;
62                 ArrayList<String> sections = null;
63                 String defsFile = null;
64                 String line;
65
66                 Pattern pattern = Pattern.compile("^([^#]\\S+)\\s*=\\s*(\\S.*)$");
67                 while ((line = br.readLine()) != null) {
68                     line = line.replace("'", "\"");
69                     Matcher matcher = pattern.matcher(line);
70                     if (matcher.find()) {
71                         if (matcher.group(1).equals("VERSION")) {
72                             version = matcher.group(2);
73                             if (version.startsWith("'") || version.startsWith("\"")) {
74                                 version = version.substring(1, version.length() - 1);
75                             }
76                         } else if (matcher.group(1).equals("DEFS_FILE")) {
77                             String fn = matcher.group(2);
78                             if (fn.startsWith("'") || fn.startsWith("\"")) {
79                                 fn = fn.substring(1, fn.length() - 1);
80                             }
81                             defsFile = resourcePath.replaceFirst("\\w*.py$", fn);
82                         } else if (matcher.group(1).equals("SECTIONS")) {
83                             sections = new ArrayList<>();
84                             Pattern secpat = Pattern.compile("\"([^\"]+)\"");
85                             Matcher secmat = secpat.matcher(matcher.group(2));
86                             while (secmat.find()) {
87                                 sections.add(secmat.group(1));
88                             }
89                         }
90                     }
91                 }
92
93                 if (version != null && defsFile != null) {
94                     LinkedHashMap<String, Object> ext = new LinkedHashMap<>();
95                     ext.put("defs_file", defsFile);
96                     if (sections != null) {
97                         ext.put("sections", sections);
98                     }
99                     extensions.put(version, ext);
100                 }
101             } catch (Exception e) {
102                 log.error("ExtTools - loadExtensions - {}", e);
103                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue(
104                         "JE281", "Failed to load extensions" + e.getMessage()));
105             }
106         }
107         return extensions;
108     }
109
110     public ArrayList<String> getVersions() {
111         return new ArrayList<String>(extensionInfo.keySet());
112     }
113
114     public LinkedHashMap<String, ArrayList<String>> getSections() {
115         LinkedHashMap<String, ArrayList<String>> sections = new LinkedHashMap<>();
116         for (String version : extensionInfo.keySet()) {
117             LinkedHashMap<String, Object> eiv = (LinkedHashMap<String, Object>) extensionInfo.get(version);
118             sections.put(version, (ArrayList<String>) eiv.get("sections"));
119         }
120         return sections;
121     }
122
123     public String getDefsFile(String version) {
124         LinkedHashMap<String, Object> eiv = (LinkedHashMap<String, Object>) extensionInfo.get(version);
125         return (String) eiv.get("defs_file");
126     }
127
128 }
129
130 /*python
131
132 from toscaparser.common.exception import ToscaExtAttributeError
133 from toscaparser.common.exception import ToscaExtImportError
134
135 log = logging.getLogger("tosca.model")
136
137 REQUIRED_ATTRIBUTES = ['VERSION', 'DEFS_FILE']
138
139
140 class ExtTools(object):
141     def __init__(self):
142         self.extensionInfo = self._load_extensions()
143
144     def _load_extensions(self):
145         '''Dynamically load all the extensions .'''
146         extensions = {}
147
148         # Use the absolute path of the class path
149         abs_path = os.path.dirname(os.path.abspath(__file__))
150
151         extdirs = [e for e in os.listdir(abs_path) if
152                    not e.startswith('tests') and
153                    os.path.isdir(os.path.join(abs_path, e))]
154
155         for e in extdirs:
156             log.info(e)
157             extpath = abs_path + '/' + e
158             # Grab all the extension files in the given path
159             ext_files = [f for f in os.listdir(extpath) if f.endswith('.py')
160                          and not f.startswith('__init__')]
161
162             # For each module, pick out the target translation class
163             for f in ext_files:
164                 log.info(f)
165                 ext_name = 'toscaparser/extensions/' + e + '/' + f.strip('.py')
166                 ext_name = ext_name.replace('/', '.')
167                 try:
168                     extinfo = importlib.import_module(ext_name)
169                     version = getattr(extinfo, 'VERSION')
170                     defs_file = extpath + '/' + getattr(extinfo, 'DEFS_FILE')
171
172                     # Sections is an optional attribute
173                     sections = getattr(extinfo, 'SECTIONS', ())
174
175                     extensions[version] = {'sections': sections,
176                                            'defs_file': defs_file}
177                 except ImportError:
178                     raise ToscaExtImportError(ext_name=ext_name)
179                 except AttributeError:
180                     attrs = ', '.join(REQUIRED_ATTRIBUTES)
181                     raise ToscaExtAttributeError(ext_name=ext_name,
182                                                  attrs=attrs)
183
184         print 'Extensions ',extensions#GGG
185         return extensions
186
187     def get_versions(self):
188         return self.extensionInfo.keys()
189
190     def get_sections(self):
191         sections = {}
192         for version in self.extensionInfo.keys():
193             sections[version] = self.extensionInfo[version]['sections']
194
195         return sections
196
197     def get_defs_file(self, version):
198         versiondata = self.extensionInfo.get(version)
199
200         if versiondata:
201             return versiondata.get('defs_file')
202         else:
203             return None
204 */