35dbeec9fe97df91a00e09312b32f4f2df1b38be
[ccsdk/features.git] / sdnr / wt / helpserver / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / helpserver / data / HelpInfrastructureObject.java
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.isEmpty()) {
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.isEmpty()) {
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.isEmpty()) {
116                     NodeObject o = new NodeObject(pRoot, f, f.getName(), versions);
117                     this.put(o.getString("label").toLowerCase(), o);
118                 }
119             }
120         }
121     }
122
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();
127         if (files != null) {
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", "", "",
131                             f.getName()));
132                 }
133             }
134         }
135         Collections.sort(list, VersionObject.getComparer());
136         Collections.reverse(list);
137         if (!list.isEmpty() && appendCurrent) {
138             list.add(list.get(0).cloneAsCurrent());
139         }
140         return list;
141     }
142
143
144     public static void createFilesFromResources() {
145
146         if (KARAFHELPDIRECTORY.exists()) {
147             LOG.info("Delete existing directory");
148             try {
149                 ExtactBundleResource.deleteRecursively(KARAFHELPDIRECTORY);
150             } catch (IOException e1) {
151                 LOG.warn(e1.toString());
152             }
153         }
154
155         LOG.info("Extract");
156         try {
157             Bundle b = FrameworkUtil.getBundle(HelpInfrastructureObject.class);
158             if (b == null) {
159                 LOG.info("No bundlereference: Use target in filesystem.");
160                 // URL helpRessource =
161                 // JarFileUtils.stringToJarURL("target/helpserver-impl-0.4.0-SNAPSHOT.jar",KARAFBUNDLERESOURCEHELPROOT);
162
163             } else {
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);
168             }
169         } catch (IOException e) {
170             LOG.warn("No help files available. Exception: " + e.toString());
171         }
172     }
173
174     public static Path getHelpDirectoryBase() {
175         return KARAFHELPDIRECTORY.toPath();
176     }
177 }