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.http.HttpHeaders;
37 //import org.apache.karaf.bundle.core.BundleInfo;
38 //import org.apache.karaf.bundle.core.BundleService;
39 import org.onap.ccsdk.features.sdnr.wt.common.Resources;
40 import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
41 import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
42 import org.osgi.framework.Bundle;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class AboutHttpServlet extends HttpServlet {
53 private static final long serialVersionUID = 1L;
54 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
55 private static final String UNKNOWN = "unknown";
56 private static final String METAINF_MAVEN = "/META-INF/maven/";
57 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
59 private static final String URI_PRE = "/about";
60 private static final String RES_BASEPATH = "about/";
62 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
63 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
64 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
65 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
66 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
67 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
68 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
69 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
70 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
71 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
72 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
73 private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}";
74 private static final String README_FILE = "README.md";
75 private static final String JSON_FILE = "README.json";
76 private static final String NO_DEVICEMANAGERS_RUNNING_MESSAGE = null;
77 private static final String MIMETYPE_JSON = "application/json";
78 private static final String MIMETYPE_MARKDOWN = "text/markdown";
80 private final String groupId = this.getGroupIdOrDefault("org.onap.ccsdk.features.sdnr.wt");
81 private final String artifactId = "sdnr-wt-data-provider-provider";
83 private final Map<Integer, String> BUNDLESTATE_LUT;
84 private final Map<String, String> data;
85 private final String readmeContent;
86 // private BundleService bundleService;
87 private String jsonContent;
90 public AboutHttpServlet() {
92 this.data = new HashMap<>();
93 this.collectStaticData();
94 this.readmeContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(README_FILE));
95 this.jsonContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(JSON_FILE));
96 this.BUNDLESTATE_LUT = new HashMap<>();
97 this.BUNDLESTATE_LUT.put(Bundle.UNINSTALLED, "uninstalled");
98 this.BUNDLESTATE_LUT.put(Bundle.INSTALLED, "installed");
99 this.BUNDLESTATE_LUT.put(Bundle.RESOLVED, "resolved");
100 this.BUNDLESTATE_LUT.put(Bundle.STARTING, "starting");
101 this.BUNDLESTATE_LUT.put(Bundle.STOPPING, "stopping");
102 this.BUNDLESTATE_LUT.put(Bundle.ACTIVE, "active");
106 protected String getGroupIdOrDefault(String def) {
107 String symbolicName = this.getManifestValue("Bundle-SymbolicName");
108 if (symbolicName != null) {
109 int idx = symbolicName.indexOf(this.artifactId);
111 return symbolicName.substring(0, idx - 1);
117 // public void setBundleService(BundleService bundleService) {
118 // this.bundleService = bundleService;
122 * collect static versioning data
124 private void collectStaticData() {
125 PomPropertiesFile props = this.getPomProperties();
126 final String ccsdkVersion = this.getPomParentVersion();
127 final String mdsalVersion = SystemInfo.getMdSalVersion(UNKNOWN);
128 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
129 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(mdsalVersion, UNKNOWN));
130 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
131 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
132 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
133 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, SystemInfo.getOnapVersion(UNKNOWN));
134 this.data.put(PLACEHOLDER_MDSAL_VERSION, mdsalVersion);
135 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
136 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
140 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
142 String uri = req.getRequestURI().substring(URI_PRE.length());
143 LOG.debug("request for {}", uri);
144 if (uri.length() <= 0 || uri.equals("/")) {
145 ContentType ctype = this.detectContentType(req, ContentType.MARKDOWN);
147 this.collectData(ctype);
149 String content = this.render(ctype);
150 byte[] output = content != null ? content.getBytes() : new byte[0];
152 resp.setStatus(HttpServletResponse.SC_OK);
153 resp.setContentLength(output.length);
154 resp.setContentType(ctype.getMimeType());
155 ServletOutputStream os = null;
157 os = resp.getOutputStream();
159 } catch (IOException e) {
160 LOG.warn("problem writing response for {}: {}", uri, e);
165 } catch (IOException e) {
166 LOG.warn("problem closing response stream: {}", e);
172 this.doGetFile(uri, resp);
177 * load git.commit.id from jar /META-INF/git.properties
181 private String getGitHash(String def) {
182 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
183 if (content == null) {
186 String lines[] = content.split("\n");
187 for (String line : lines) {
188 if (line.startsWith("git.commit.id")) {
189 def = line.substring("git.commit.id=".length());
196 private String getResourceFileContent(String filename) {
197 LOG.debug("try ti get content of {}", filename);
198 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
202 * collect dynamic data for about.md
204 private void collectData(ContentType ctype) {
205 LOG.info("collecting dynamic data");
207 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
208 this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles(ctype));
209 } catch (Exception e) {
210 LOG.warn("problem collecting system data: {}", e);
215 * get value for key out of /META-INF/MANIFEST.MF
220 protected String getManifestValue(String key) {
221 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
227 manifest = new Manifest(url.openStream());
228 Attributes attr = manifest.getMainAttributes();
229 return attr.getValue(key);
230 } catch (IOException e) {
231 LOG.warn("problem reading manifest: {}", e);
238 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
242 private PomPropertiesFile getPomProperties() {
243 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
244 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
245 PomPropertiesFile propfile;
250 propfile = new PomPropertiesFile(url.openStream());
252 } catch (Exception e) {
253 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
259 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
263 private String getPomParentVersion() {
264 LOG.info("try to get pom parent version");
265 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
266 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
272 pomfile = new PomFile(url.openStream());
273 return pomfile.getParentVersion();
274 } catch (Exception e) {
275 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
280 private String getDevicemanagerBundles(ContentType ctype) {
281 Bundle thisbundle = FrameworkUtil.getBundle(this.getClass());
282 BundleContext context = thisbundle == null ? null : thisbundle.getBundleContext();
283 if (context == null) {
284 LOG.debug("no bundle context available");
285 return ctype==ContentType.MARKDOWN?"":"[]";
287 Bundle[] bundles = context.getBundles();
288 if (bundles == null || bundles.length <= 0) {
289 LOG.debug("no bundles found");
290 return ctype==ContentType.MARKDOWN?NO_DEVICEMANAGERS_RUNNING_MESSAGE:"[]";
292 LOG.debug("found {} bundles", bundles.length);
293 MarkdownTable table = new MarkdownTable();
294 table.setHeader(new String[] {"Bundle-Id", "Version", "Symbolic-Name", "Status"});
296 for (Bundle bundle : bundles) {
297 name = bundle.getSymbolicName();
298 if (!(name.contains("devicemanager") && name.contains("provider"))) {
301 if (name.equals("org.onap.ccsdk.features.sdnr.wt.sdnr-wt-devicemanager-provider")) {
304 table.addRow(new String[] {String.valueOf(bundle.getBundleId()), bundle.getVersion().toString(), name,
305 BUNDLESTATE_LUT.getOrDefault(bundle.getState(), "unknown")});
308 return ctype==ContentType.MARKDOWN?table.toMarkDown():table.toJson();
312 * get file by uri from resources and write out to response stream
317 private void doGetFile(String uri, HttpServletResponse resp) {
318 String content = this.getResourceFileContent(uri);
319 if (content == null) {
321 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
322 } catch (IOException e) {
323 LOG.debug("unable to send error response : {}", e);
326 byte[] data = content.getBytes();
327 resp.setStatus(HttpServletResponse.SC_OK);
328 resp.setContentType(this.getContentType(uri));
330 resp.getOutputStream().write(data);
331 } catch (IOException e) {
332 LOG.debug("unable to send data : {}", e);
339 * create http response contentType by filename
344 private String getContentType(String filename) {
345 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
353 return "image/" + ext;
355 return "application/json";
367 * render this.readmeContent with this.data
373 private String render(ContentType ctype) {
374 return this.render(ctype, null);
378 * render content with this.data
383 private String render(ContentType ctype, String content) {
384 if (content == null) {
385 content = ctype==ContentType.MARKDOWN? this.readmeContent:this.jsonContent;
387 if (content == null) {
390 for (Entry<String, String> entry : this.data.entrySet()) {
391 if (entry.getValue() != null && content.contains(entry.getKey())) {
392 content = content.replace(entry.getKey(), entry.getValue());
399 public void setClusterSize(String value) {
400 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);
403 private ContentType detectContentType(HttpServletRequest req, ContentType def) {
404 String accept = req.getHeader(HttpHeaders.ACCEPT);
405 if (accept != null) {
406 if (accept.equals(MIMETYPE_JSON)) {
407 return ContentType.JSON;
408 } else if (accept.equals(MIMETYPE_MARKDOWN)) {
409 return ContentType.MARKDOWN;
415 private enum ContentType {
416 MARKDOWN(MIMETYPE_MARKDOWN), JSON(MIMETYPE_JSON);
418 private String mimeType;
420 ContentType(String mimeType) {
421 this.mimeType = mimeType;
424 String getMimeType() {
425 return this.mimeType;