c1cf875fc68c1bd35bf26fe1988367a0437f59e7
[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
52     private final boolean oauthEnabled;
53     private final boolean policyEnabled;
54
55     public IndexOdluxBundle() {
56         super(null, BUNDLENAME_APP);
57         this.oauthEnabled = "true".equals(getEnvVar(ENV_ENABLE_OAUTH, ENABLE_OAUTH_DEFAULT));
58         this.policyEnabled = "true".equals(getEnvVar(ENV_ENABLE_ODLUX_RBAC, ENABLE_ODLUX_RBAC_DEFAULT));
59         LOG.info("instantiating index with oauthEnabled={} and policyEnabled={}",this.oauthEnabled, this.policyEnabled);
60
61     }
62
63     @Override
64     protected String loadFileContent(URL url) {
65         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
66     }
67
68     @Override
69     public String getResourceFileContent(String fn, List<String> bundleNames) {
70         return loadFileContent(this.getResource(fn), bundleNames);
71     }
72
73     private String loadFileContent(URL url, List<String> bundlesNamesList) {
74         if (url == null) {
75             return null;
76         }
77         LOG.debug("try to load res " + url.toString());
78         StringBuilder sb = new StringBuilder();
79         Matcher matcher;
80         BufferedReader in = null;
81         try {
82             in = new BufferedReader(new InputStreamReader(url.openStream()));
83
84             String inputLine;
85             while ((inputLine = in.readLine()) != null) {
86                 if (url.getFile().endsWith("index.html")) {
87                     matcher = patternRequire.matcher(inputLine);
88                     if (matcher.find()) {
89                         inputLine = inputLine.substring(0, matcher.start(1)) + "\""
90                                 + String.join("\",\"", bundlesNamesList) + "\"" + inputLine.substring(matcher.end(1));
91                     }
92                     matcher = patternFunction.matcher(inputLine);
93                     if (matcher.find()) {
94                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
95                                 + inputLine.substring(matcher.end(1));
96                     }
97                     matcher = patternFunctionBody.matcher(inputLine);
98                     if (matcher.find()) {
99                         String hlp = "";
100                         for (String bundle : bundlesNamesList) {
101                             if (!bundle.equals(BUNDLENAME_APP)) {
102                                 hlp += bundle + ".register();" + LR;
103                             }
104                         }
105                         inputLine =
106                                 inputLine.substring(0, matcher.start(1)) + hlp + inputLine.substring(matcher.start(1));
107                     }
108
109                 }
110                 sb.append(inputLine + LR);
111             }
112
113             if (url.getFile().endsWith("index.html")) {
114                 matcher = patternConfigure.matcher(sb);
115                 if(matcher.find() && matcher.groupCount()>0) {
116                     sb.replace(matcher.start(1),matcher.end(1), this.generateConfigureJson());
117                 }
118             }
119
120         } catch (IOException e) {
121             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
122             return null;
123         } finally {
124             try {
125                 if (in != null) {
126                     in.close();
127                 }
128             } catch (IOException e) {
129
130             }
131         }
132
133         return sb.toString();
134     }
135
136     private String generateConfigureJson() {
137         return String.format("{\"authentication\":\"%s\",\"enablePolicy\":%s}", this.oauthEnabled ? "oauth" : "basic",
138                 String.valueOf(this.policyEnabled));
139     }
140
141     private static String getEnvVar(String v, String defaultValue) {
142         Map<String, String> env = System.getenv();
143         for (String envName : env.keySet()) {
144             if (envName != null && envName.equals(v))
145                 return env.get(envName);
146         }
147         return defaultValue;
148     }
149 }