11042d13a0bad55c12805ce4bf79610de2e43059
[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.helpserver.data;
19
20 import java.io.File;
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;
32
33 public class HelpInfrastructureObject extends JSONObject {
34
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);
40
41     public static class VersionObject extends JSONObject {
42         private static Comparator<VersionObject> comp;
43         private final String mVersion;
44
45         public String getVersion() {
46             return this.mVersion;
47         }
48
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);
54         }
55
56         public static Comparator<VersionObject> getComparer() {
57             if (comp == null) {
58                 comp = (o1, o2) -> o1.getVersion().compareTo(o2.getVersion());
59             }
60             return comp;
61         }
62
63         public VersionObject cloneAsLatest() {
64             return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"), "latest");
65         }
66
67         public VersionObject cloneAsCurrent() {
68             return new VersionObject(this.getString("path"), this.getString("date"), this.getString("label"),
69                     "current");
70         }
71     }
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.size() > 0) {
76                 JSONObject o = new JSONObject();
77                 this.put("versions", o);
78                 for (VersionObject version : versions) {
79                     o.put(version.getVersion(), version);
80                 }
81
82             }
83             File[] list = dir.listFiles();
84             if (list == null) {
85                 return;
86             }
87             for (File f : list) {
88                 if (f.isDirectory()) {
89                     ArrayList<VersionObject> versions2 = findReadmeVersionFolders(base, f.toPath(), true);
90                     if (versions2 != null && versions2.size() > 0) {
91                         JSONObject nodes;
92                         if (!this.has("nodes")) {
93                             this.put("nodes", new JSONObject());
94                         }
95                         nodes = this.getJSONObject("nodes");
96
97                         NodeObject o = new NodeObject(base, f, f.getName(), versions2);
98                         nodes.put(o.getString("label").toLowerCase(), o);
99                     }
100                 }
101             }
102         }
103
104     }
105
106     public HelpInfrastructureObject(Path proot) throws URISyntaxException {
107         File root = proot.toFile();
108         File[] list = root.listFiles();
109         if (list == null) {
110             return;
111         }
112         for (File f : list) {
113             if (f.isDirectory()) {
114                 ArrayList<VersionObject> versions = findReadmeVersionFolders(root.toPath(), f.toPath(), true);
115                 if (versions != null && versions.size() > 0) {
116                     NodeObject o = new NodeObject(proot, f, f.getName(), versions);
117                     this.put(o.getString("label").toLowerCase(), o);
118                 }
119             }
120         }
121
122
123     }
124
125     public static void walk(ArrayList<File> results, String path) {
126
127         File root = new File(path);
128         File[] list = root.listFiles();
129
130         if (list == null) {
131             return;
132         }
133
134         for (File f : list) {
135             if (f.isDirectory()) {
136                 walk(results, f.getAbsolutePath());
137                 // System.out.println( "Dir:" + f.getAbsoluteFile() );
138             } else {
139                 // System.out.println( "File:" + f.getAbsoluteFile() );
140                 if (f.isFile() && f.getName().endsWith(".md")) {
141                     results.add(f);
142                 }
143             }
144         }
145     }
146
147     private static ArrayList<VersionObject> findReadmeVersionFolders(Path base, Path root, boolean appendCurrent) {
148         ArrayList<VersionObject> list = new ArrayList<>();
149         File[] files = root.toFile().listFiles();
150         int baselen = base.toFile().getAbsolutePath().length();
151         if (files != null) {
152             for (File f : files) {
153                 if (f.isDirectory() && new File(f.getAbsolutePath() + "/README.md").exists()) {
154                     list.add(new VersionObject(f.getAbsolutePath().substring(baselen + 1) + "/README.md", "", "",
155                             f.getName()));
156                 }
157             }
158         }
159         Collections.sort(list, VersionObject.getComparer());
160         Collections.reverse(list);
161         if (list.size() > 0 && appendCurrent) {
162             list.add(list.get(0).cloneAsCurrent());
163         }
164         return list;
165     }
166
167
168     public static void createFilesFromResources() {
169
170         if (KARAFHELPDIRECTORY.exists()) {
171             LOG.info("Delete existing directory");
172             try {
173                 ExtactBundleResource.deleteRecursively(KARAFHELPDIRECTORY);
174             } catch (IOException e1) {
175                 LOG.warn(e1.toString());
176             }
177         }
178
179         LOG.info("Extract");
180         try {
181             Bundle b = FrameworkUtil.getBundle(HelpInfrastructureObject.class);
182             if (b == null) {
183                 LOG.info("No bundlereference: Use target in filesystem.");
184                 // URL helpRessource =
185                 // JarFileUtils.stringToJarURL("target/helpserver-impl-0.4.0-SNAPSHOT.jar",KARAFBUNDLERESOURCEHELPROOT);
186
187             } else {
188                 LOG.info("Bundle location:{} State:{}", b.getLocation(), b.getState());
189                 LOG.info("Write files from Resource");
190                 ExtactBundleResource.copyBundleResoucesRecursively(b, "data/cache/com.highstreet.technologies.",
191                         KARAFBUNDLERESOURCEHELPROOT);
192             }
193         } catch (IOException e) {
194             LOG.warn("No help files available. Exception: " + e.toString());
195         }
196     }
197
198     public static Path getHelpDirectoryBase() {
199         return KARAFHELPDIRECTORY.toPath();
200     }
201 }