2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api.extensions;
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;
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;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
40 public class ExtTools {
42 private static Logger log = LoggerFactory.getLogger(ExtTools.class.getName());
44 private static LinkedHashMap<String, Object> extensionInfo = new LinkedHashMap<>();
47 extensionInfo = loadExtensions();
50 private LinkedHashMap<String, Object> loadExtensions() {
52 LinkedHashMap<String, Object> extensions = new LinkedHashMap<>();
54 Reflections reflections = new Reflections("extensions", new ResourcesScanner());
55 Set<String> resourcePaths = reflections.getResources(Pattern.compile(".*\\.py$"));
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;
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);
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);
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);
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));
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);
99 extensions.put(version, ext);
101 } catch (Exception e) {
102 log.error("ExtTools - loadExtensions - {}", e);
103 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue(
104 "JE281", "Failed to load extensions" + e.getMessage()));
110 public ArrayList<String> getVersions() {
111 return new ArrayList<String>(extensionInfo.keySet());
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"));
123 public String getDefsFile(String version) {
124 LinkedHashMap<String, Object> eiv = (LinkedHashMap<String, Object>) extensionInfo.get(version);
125 return (String) eiv.get("defs_file");
132 from toscaparser.common.exception import ToscaExtAttributeError
133 from toscaparser.common.exception import ToscaExtImportError
135 log = logging.getLogger("tosca.model")
137 REQUIRED_ATTRIBUTES = ['VERSION', 'DEFS_FILE']
140 class ExtTools(object):
142 self.extensionInfo = self._load_extensions()
144 def _load_extensions(self):
145 '''Dynamically load all the extensions .'''
148 # Use the absolute path of the class path
149 abs_path = os.path.dirname(os.path.abspath(__file__))
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))]
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__')]
162 # For each module, pick out the target translation class
165 ext_name = 'toscaparser/extensions/' + e + '/' + f.strip('.py')
166 ext_name = ext_name.replace('/', '.')
168 extinfo = importlib.import_module(ext_name)
169 version = getattr(extinfo, 'VERSION')
170 defs_file = extpath + '/' + getattr(extinfo, 'DEFS_FILE')
172 # Sections is an optional attribute
173 sections = getattr(extinfo, 'SECTIONS', ())
175 extensions[version] = {'sections': sections,
176 'defs_file': defs_file}
178 raise ToscaExtImportError(ext_name=ext_name)
179 except AttributeError:
180 attrs = ', '.join(REQUIRED_ATTRIBUTES)
181 raise ToscaExtAttributeError(ext_name=ext_name,
184 print 'Extensions ',extensions#GGG
187 def get_versions(self):
188 return self.extensionInfo.keys()
190 def get_sections(self):
192 for version in self.extensionInfo.keys():
193 sections[version] = self.extensionInfo[version]['sections']
197 def get_defs_file(self, version):
198 versiondata = self.extensionInfo.get(version)
201 return versiondata.get('defs_file')