2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about;
24 import java.io.IOException;
26 import java.util.HashMap;
28 import java.util.Map.Entry;
29 import java.util.jar.Attributes;
30 import java.util.jar.Manifest;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletOutputStream;
33 import javax.servlet.http.HttpServlet;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 //import org.apache.karaf.bundle.core.BundleInfo;
37 //import org.apache.karaf.bundle.core.BundleService;
38 import org.onap.ccsdk.features.sdnr.wt.common.Resources;
39 import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
40 import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class AboutHttpServlet extends HttpServlet {
49 private static final long serialVersionUID = 1L;
50 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
51 private static final String UNKNOWN = "unknown";
52 private static final String METAINF_MAVEN = "/META-INF/maven/";
53 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
55 private static final String URI_PRE = "/about";
56 private static final String RES_BASEPATH = "about/";
58 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
59 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
60 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
61 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
62 private static final String PLACEHOLDER_ODLUX_REVISION = "{odlux-revision}";
63 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
64 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
65 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
66 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
67 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
68 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
69 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
70 private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}";
71 private static final String README_FILE = "README.md";
73 private final String groupId = "org.onap.ccsdk.features.sdnr.wt";
74 private final String artifactId = "sdnr-wt-data-provider-provider";
76 private final Map<String, String> data;
77 private final String readmeContent;
78 // private BundleService bundleService;
81 public AboutHttpServlet() {
83 this.data = new HashMap<>();
84 this.collectStaticData();
85 this.readmeContent = this.render(this.getResourceFileContent(README_FILE));
86 //BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
90 // public void setBundleService(BundleService bundleService) {
91 // this.bundleService = bundleService;
95 * collect static versioning data
97 private void collectStaticData() {
98 PomPropertiesFile props = this.getPomProperties();
99 final String ccsdkVersion = this.getPomParentVersion();
100 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
101 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(ccsdkVersion, UNKNOWN));
102 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
103 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
104 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
105 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, "2.0.0-SNAPSHOT");
106 this.data.put(PLACEHOLDER_MDSAL_VERSION, SystemInfo.getMdSalVersion(UNKNOWN));
107 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
108 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
112 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
114 String uri = req.getRequestURI().substring(URI_PRE.length());
115 LOG.debug("request for {}", uri);
116 if (uri.length() <= 0 || uri.equals("/")) {
120 String content = this.render();
121 byte[] output = content != null ? content.getBytes() : new byte[0];
123 resp.setStatus(HttpServletResponse.SC_OK);
124 resp.setContentLength(output.length);
125 resp.setContentType("text/plain");
126 ServletOutputStream os = null;
128 os = resp.getOutputStream();
130 } catch (IOException e) {
131 LOG.warn("problem writing response for {}: {}", uri, e);
136 } catch (IOException e) {
137 LOG.warn("problem closing response stream: {}", e);
143 this.doGetFile(uri, resp);
148 * load git.commit.id from jar /META-INF/git.properties
152 private String getGitHash(String def) {
153 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
154 if (content == null) {
157 String lines[] = content.split("\n");
158 for (String line : lines) {
159 if (line.startsWith("git.commit.id")) {
160 def = line.substring("git.commit.id=".length());
167 private String getResourceFileContent(String filename) {
168 LOG.debug("try ti get content of {}", filename);
169 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
173 * collect dynamic data for about.md
175 private void collectData() {
176 LOG.info("collecting dynamic data");
178 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
179 this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles());
180 } catch (Exception e) {
181 LOG.warn("problem collecting system data: {}", e);
186 * get value for key out of /META-INF/MANIFEST.MF
191 private String getManifestValue(String key) {
192 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
198 manifest = new Manifest(url.openStream());
199 Attributes attr = manifest.getMainAttributes();
200 return attr.getValue(key);
201 } catch (IOException e) {
202 LOG.warn("problem reading manifest: {}", e);
209 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
213 private PomPropertiesFile getPomProperties() {
214 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
215 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
216 PomPropertiesFile propfile;
221 propfile = new PomPropertiesFile(url.openStream());
223 } catch (Exception e) {
224 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
230 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
235 private String getPomProperty(String key) {
236 LOG.info("try to get pom property for {}", key);
237 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
238 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
244 pomfile = new PomFile(url.openStream());
245 return pomfile.getProperty(key);
246 } catch (Exception e) {
247 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
253 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
257 private String getPomParentVersion() {
258 LOG.info("try to get pom parent version");
259 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
260 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
266 pomfile = new PomFile(url.openStream());
267 return pomfile.getParentVersion();
268 } catch (Exception e) {
269 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
274 private String getDevicemanagerBundles() {
275 // if(this.bundleService==null) {
276 // LOG.debug("no bundle service available");
280 // List<String> ids = new ArrayList<String>();
281 // List<Bundle> bundles = bundleService.selectBundles("0", ids , true);
282 // if(bundles==null || bundles.size()<=0) {
283 // LOG.debug("no bundles found");
286 // LOG.debug("found {} bundles",bundles.size());
287 // MarkdownTable table = new MarkdownTable();
288 // for(Bundle bundle:bundles) {
289 // BundleInfo info = this.bundleService.getInfo(bundle);
290 // table.addRow(new String[] {String.valueOf(info.getBundleId()),info.getVersion(),info.getName(),info.getState().toString()});
292 // return table.toMarkDown();
297 * get file by uri from resources and write out to response stream
302 private void doGetFile(String uri, HttpServletResponse resp) {
303 String content = this.getResourceFileContent(uri);
304 if (content == null) {
306 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
307 } catch (IOException e) {
308 LOG.debug("unable to send error response : {}", e);
311 byte[] data = content.getBytes();
312 resp.setStatus(HttpServletResponse.SC_OK);
313 resp.setContentType(this.getContentType(uri));
315 resp.getOutputStream().write(data);
316 } catch (IOException e) {
317 LOG.debug("unable to send data : {}", e);
324 * create http response contentType by filename
329 private String getContentType(String filename) {
330 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
338 return "image/" + ext;
340 return "application/json";
352 * render this.readmeContent with this.data
356 private String render() {
357 return this.render(null);
361 * render content with this.data
366 private String render(String content) {
367 if (content == null) {
368 content = this.readmeContent;
370 if (content == null) {
373 for (Entry<String, String> entry : this.data.entrySet()) {
374 if (entry.getValue() != null && content.contains(entry.getKey())) {
375 content = content.replace(entry.getKey(), entry.getValue());
382 public void setClusterSize(String value) {
383 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);