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
 
  10  * http://www.apache.org/licenses/LICENSE-2.0
 
  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
 
  16  * ============LICENSE_END==========================================================================
 
  18 package org.onap.ccsdk.features.sdnr.wt.odlux;
 
  20 import java.io.BufferedReader;
 
  21 import java.io.IOException;
 
  22 import java.io.InputStreamReader;
 
  24 import java.util.List;
 
  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;
 
  33 public class IndexOdluxBundle extends OdluxBundle implements OdluxBundleResourceAccess {
 
  35     private static Logger LOG = LoggerFactory.getLogger(IndexOdluxBundle.class);
 
  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 = "";
 
  54     private final boolean oauthEnabled;
 
  55     private final boolean policyEnabled;
 
  56     private final String transportPceGuiUrl;
 
  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);
 
  68     protected String loadFileContent(URL url) {
 
  69         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
 
  73     public String getResourceFileContent(String fn, List<String> bundleNames) {
 
  74         return loadFileContent(this.getResource(fn), bundleNames);
 
  77     private String loadFileContent(URL url, List<String> bundlesNamesList) {
 
  81         LOG.debug("try to load res " + url.toString());
 
  82         StringBuilder sb = new StringBuilder();
 
  84         BufferedReader in = null;
 
  86             in = new BufferedReader(new InputStreamReader(url.openStream()));
 
  89             while ((inputLine = in.readLine()) != null) {
 
  90                 if (url.getFile().endsWith("index.html")) {
 
  91                     matcher = patternRequire.matcher(inputLine);
 
  93                         inputLine = inputLine.substring(0, matcher.start(1)) + "\""
 
  94                                 + String.join("\",\"", bundlesNamesList) + "\"" + inputLine.substring(matcher.end(1));
 
  96                     matcher = patternFunction.matcher(inputLine);
 
  98                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
 
  99                                 + inputLine.substring(matcher.end(1));
 
 101                     matcher = patternFunctionBody.matcher(inputLine);
 
 102                     if (matcher.find()) {
 
 104                         for (String bundle : bundlesNamesList) {
 
 105                             if (!bundle.equals(BUNDLENAME_APP)) {
 
 106                                 hlp += bundle + ".register();" + LR;
 
 110                                 inputLine.substring(0, matcher.start(1)) + hlp + inputLine.substring(matcher.start(1));
 
 114                 sb.append(inputLine + LR);
 
 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());
 
 124         } catch (IOException e) {
 
 125             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
 
 132             } catch (IOException e) {
 
 137         return sb.toString();
 
 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.isBlank()) {
 
 145             sb.append(String.format(",\"transportpceUrl\":\"%s\"", this.transportPceGuiUrl));
 
 148         return sb.toString();
 
 151     private static String getEnvVar(String v, String defaultValue) {
 
 152         Map<String, String> env = System.getenv();
 
 153         for (String envName : env.keySet()) {
 
 154             if (envName != null && envName.equals(v))
 
 155                 return env.get(envName);