82df2f0cd448c3184d7067bb1b51ab1c93758721
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.URL;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
29 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleResourceAccess;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class IndexOdluxBundle extends OdluxBundle implements OdluxBundleResourceAccess {
34
35     private static Logger LOG = LoggerFactory.getLogger(IndexOdluxBundle.class);
36
37     private static final String LR = "\n";
38     private static final String BUNDLENAME_APP = "run";
39     private static final String regexRequire = "require\\(\\[(\"" + BUNDLENAME_APP + "\")\\]";
40     private static final String regexFunction = "function[\\ ]*\\((" + BUNDLENAME_APP + ")\\)[\\ ]*\\{";
41     private static final String regexFunctionBody = "(" + BUNDLENAME_APP + "\\.runApplication\\(\\);)";
42     private static final String regexConfigure = BUNDLENAME_APP+"\\.configureApplication\\(([^\\)]+)\\)";
43     private static final Pattern patternRequire = Pattern.compile(regexRequire);
44     private static final Pattern patternFunction = Pattern.compile(regexFunction);
45     private static final Pattern patternFunctionBody = Pattern.compile(regexFunctionBody);
46     private static final Pattern patternConfigure = Pattern.compile(regexConfigure);
47     private static final String ENV_ENABLE_OAUTH = "ENABLE_OAUTH";
48     private static final String ENABLE_OAUTH_DEFAULT = "false";
49     private static final String ENV_ENABLE_ODLUX_RBAC = "ENABLE_ODLUX_RBAC";
50     private static final String ENABLE_ODLUX_RBAC_DEFAULT = "false";
51     private static final String ENV_TRANSPORTPCEGUIURL = "TRPCEGUIURL";
52     private static final String TRANSPORTPCEGUIURL_DEFAULT = "";
53
54     private final boolean oauthEnabled;
55     private final boolean policyEnabled;
56     private final String transportPceGuiUrl;
57
58     public IndexOdluxBundle() {
59         super(null, BUNDLENAME_APP);
60         this.oauthEnabled = "true".equals(getEnvVar(ENV_ENABLE_OAUTH, ENABLE_OAUTH_DEFAULT));
61         this.policyEnabled = "true".equals(getEnvVar(ENV_ENABLE_ODLUX_RBAC, ENABLE_ODLUX_RBAC_DEFAULT));
62         this.transportPceGuiUrl = getEnvVar(ENV_TRANSPORTPCEGUIURL, TRANSPORTPCEGUIURL_DEFAULT);
63         LOG.info("instantiating index with oauthEnabled={} and policyEnabled={}",this.oauthEnabled, this.policyEnabled);
64
65     }
66
67     @Override
68     protected String loadFileContent(URL url) {
69         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
70     }
71
72     @Override
73     public String getResourceFileContent(String fn, List<String> bundleNames) {
74         return loadFileContent(this.getResource(fn), bundleNames);
75     }
76
77     private String loadFileContent(URL url, List<String> bundlesNamesList) {
78         if (url == null) {
79             return null;
80         }
81         LOG.debug("try to load res " + url.toString());
82         StringBuilder sb = new StringBuilder();
83         Matcher matcher;
84         BufferedReader in = null;
85         try {
86             in = new BufferedReader(new InputStreamReader(url.openStream()));
87
88             String inputLine;
89             while ((inputLine = in.readLine()) != null) {
90                 if (url.getFile().endsWith("index.html")) {
91                     matcher = patternRequire.matcher(inputLine);
92                     if (matcher.find()) {
93                         inputLine = inputLine.substring(0, matcher.start(1)) + "\""
94                                 + String.join("\",\"", bundlesNamesList) + "\"" + inputLine.substring(matcher.end(1));
95                     }
96                     matcher = patternFunction.matcher(inputLine);
97                     if (matcher.find()) {
98                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
99                                 + inputLine.substring(matcher.end(1));
100                     }
101                     matcher = patternFunctionBody.matcher(inputLine);
102                     if (matcher.find()) {
103                         String hlp = "";
104                         for (String bundle : bundlesNamesList) {
105                             if (!bundle.equals(BUNDLENAME_APP)) {
106                                 hlp += bundle + ".register();" + LR;
107                             }
108                         }
109                         inputLine =
110                                 inputLine.substring(0, matcher.start(1)) + hlp + inputLine.substring(matcher.start(1));
111                     }
112
113                 }
114                 sb.append(inputLine + LR);
115             }
116
117             if (url.getFile().endsWith("index.html")) {
118                 matcher = patternConfigure.matcher(sb);
119                 if(matcher.find() && matcher.groupCount()>0) {
120                     sb.replace(matcher.start(1),matcher.end(1), this.generateConfigureJson());
121                 }
122             }
123
124         } catch (IOException e) {
125             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
126             return null;
127         } finally {
128             try {
129                 if (in != null) {
130                     in.close();
131                 }
132             } catch (IOException e) {
133
134             }
135         }
136
137         return sb.toString();
138     }
139
140     private String generateConfigureJson() {
141         StringBuilder sb = new StringBuilder();
142         sb.append(String.format("{\"authentication\":\"%s\",\"enablePolicy\":%s", this.oauthEnabled ? "oauth" : "basic",
143                 String.valueOf(this.policyEnabled)));
144         if (this.transportPceGuiUrl != null && this.transportPceGuiUrl.length() > 0) {
145             sb.append(String.format(",\"transportpceUrl\":\"%s\"}", this.transportPceGuiUrl));
146         } else {
147             sb.append("}");
148         }
149         return sb.toString();
150     }
151
152     private static String getEnvVar(String v, String defaultValue) {
153         Map<String, String> env = System.getenv();
154         for (String envName : env.keySet()) {
155             if (envName != null && envName.equals(v))
156                 return env.get(envName);
157         }
158         return defaultValue;
159     }
160 }