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.helpserver.data;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.nio.file.Path;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import org.json.JSONObject;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.FrameworkUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class HelpInfrastructureObject extends JSONObject {
35 private static final Logger LOG = LoggerFactory.getLogger(HelpInfrastructureObject.class);
36 private static String HELPBASE = "help";
37 private static String KARAFBUNDLERESOURCEHELPROOT = "/" + HELPBASE;
38 private static String KARAFHELPDIRPREFIX = "data/cache/com.highstreet.technologies.";
39 public static File KARAFHELPDIRECTORY = new File(KARAFHELPDIRPREFIX + HELPBASE);
41 public static class VersionObject extends JSONObject {
42 private static Comparator<VersionObject> comp;
43 private final String mVersion;
45 public String getVersion() {
49 public VersionObject(String path, String date, String label, String version) {
50 this.mVersion = version;
51 this.put("path", path);
52 this.put("date", date);
53 this.put("label", label);
56 public static Comparator<VersionObject> getComparer() {
58 comp = (o1, o2) -> o1.getVersion().compareTo(o2.getVersion());
63 public VersionObject cloneAsLatest() {
64 return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "latest");
67 public VersionObject cloneAsCurrent() {
68 return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"),
72 public static class NodeObject extends JSONObject {
73 public NodeObject(Path base, File dir, String label, ArrayList<VersionObject> versions) {
74 this.put("label", label);
75 if (versions != null && !versions.isEmpty()) {
76 JSONObject o = new JSONObject();
77 this.put("versions", o);
78 for (VersionObject version : versions) {
79 o.put(version.getVersion(), version);
83 File[] list = dir.listFiles();
88 if (f.isDirectory()) {
89 ArrayList<VersionObject> versions2 = findReadmeVersionFolders(base, f.toPath(), true);
90 if (versions2 != null && !versions2.isEmpty()) {
92 if (!this.has("nodes")) {
93 this.put("nodes", new JSONObject());
95 nodes = this.getJSONObject("nodes");
97 NodeObject o = new NodeObject(base, f, f.getName(), versions2);
98 nodes.put(o.getString("label").toLowerCase(), o);
106 public HelpInfrastructureObject(Path pRoot) throws URISyntaxException {
107 File root = pRoot.toFile();
108 File[] list = root.listFiles();
112 for (File f : list) {
113 if (f.isDirectory()) {
114 ArrayList<VersionObject> versions = findReadmeVersionFolders(root.toPath(), f.toPath(), true);
115 if (versions != null && !versions.isEmpty()) {
116 NodeObject o = new NodeObject(pRoot, f, f.getName(), versions);
117 this.put(o.getString("label").toLowerCase(), o);
123 private static ArrayList<VersionObject> findReadmeVersionFolders(Path base, Path root, boolean appendCurrent) {
124 ArrayList<VersionObject> list = new ArrayList<>();
125 File[] files = root.toFile().listFiles();
126 int baselen = base.toFile().getAbsolutePath().length();
128 for (File f : files) {
129 if (f.isDirectory() && new File(f.getAbsolutePath() + "/README.md").exists()) {
130 list.add(new VersionObject(f.getAbsolutePath().substring(baselen + 1) + "/README.md", "", "",
135 Collections.sort(list, VersionObject.getComparer());
136 Collections.reverse(list);
137 if (!list.isEmpty() && appendCurrent) {
138 list.add(list.get(0).cloneAsCurrent());
144 public static void createFilesFromResources() {
146 if (KARAFHELPDIRECTORY.exists()) {
147 LOG.info("Delete existing directory");
149 ExtactBundleResource.deleteRecursively(KARAFHELPDIRECTORY);
150 } catch (IOException e1) {
151 LOG.warn(e1.toString());
157 Bundle b = FrameworkUtil.getBundle(HelpInfrastructureObject.class);
159 LOG.info("No bundlereference: Use target in filesystem.");
160 // URL helpRessource =
161 // JarFileUtils.stringToJarURL("target/helpserver-impl-0.4.0-SNAPSHOT.jar",KARAFBUNDLERESOURCEHELPROOT);
164 LOG.info("Bundle location:{} State:{}", b.getLocation(), b.getState());
165 LOG.info("Write files from Resource");
166 ExtactBundleResource.copyBundleResoucesRecursively(b, "data/cache/com.highstreet.technologies.",
167 KARAFBUNDLERESOURCEHELPROOT);
169 } catch (IOException e) {
170 LOG.warn("No help files available. Exception: " + e.toString());
174 public static Path getHelpDirectoryBase() {
175 return KARAFHELPDIRECTORY.toPath();