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.osgi.framework.Bundle;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.FrameworkUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class AboutHttpServlet extends HttpServlet {
52 private static final long serialVersionUID = 1L;
53 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
54 private static final String UNKNOWN = "unknown";
55 private static final String METAINF_MAVEN = "/META-INF/maven/";
56 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
58 private static final String URI_PRE = "/about";
59 private static final String RES_BASEPATH = "about/";
61 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
62 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
63 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
64 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
65 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
66 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
67 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
68 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
69 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
70 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
71 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
72 private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}";
73 private static final String README_FILE = "README.md";
74 private static final String NO_DEVICEMANAGERS_RUNNING_MESSAGE = null;
76 private final String groupId = this.getGroupIdOrDefault("org.onap.ccsdk.features.sdnr.wt");
77 private final String artifactId = "sdnr-wt-data-provider-provider";
79 private final Map<Integer,String> BUNDLESTATE_LUT;
80 private final Map<String, String> data;
81 private final String readmeContent;
82 // private BundleService bundleService;
85 public AboutHttpServlet() {
87 this.data = new HashMap<>();
88 this.collectStaticData();
89 this.readmeContent = this.render(this.getResourceFileContent(README_FILE));
90 this.BUNDLESTATE_LUT = new HashMap<>();
91 this.BUNDLESTATE_LUT.put(Bundle.UNINSTALLED, "uninstalled");
92 this.BUNDLESTATE_LUT.put(Bundle.INSTALLED, "installed");
93 this.BUNDLESTATE_LUT.put(Bundle.RESOLVED, "resolved");
94 this.BUNDLESTATE_LUT.put(Bundle.STARTING, "starting");
95 this.BUNDLESTATE_LUT.put(Bundle.STOPPING, "stopping");
96 this.BUNDLESTATE_LUT.put(Bundle.ACTIVE, "active");
100 protected String getGroupIdOrDefault(String def) {
101 String symbolicName = this.getManifestValue("Bundle-SymbolicName");
102 if(symbolicName!=null) {
103 int idx = symbolicName.indexOf(this.artifactId);
105 return symbolicName.substring(0, idx-1);
111 // public void setBundleService(BundleService bundleService) {
112 // this.bundleService = bundleService;
116 * collect static versioning data
118 private void collectStaticData() {
119 PomPropertiesFile props = this.getPomProperties();
120 final String ccsdkVersion = this.getPomParentVersion();
121 final String mdsalVersion = SystemInfo.getMdSalVersion(UNKNOWN);
122 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
123 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(mdsalVersion, UNKNOWN));
124 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
125 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
126 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
127 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, SystemInfo.getOnapVersion(UNKNOWN));
128 this.data.put(PLACEHOLDER_MDSAL_VERSION, mdsalVersion);
129 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
130 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
134 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
136 String uri = req.getRequestURI().substring(URI_PRE.length());
137 LOG.debug("request for {}", uri);
138 if (uri.length() <= 0 || uri.equals("/")) {
142 String content = this.render();
143 byte[] output = content != null ? content.getBytes() : new byte[0];
145 resp.setStatus(HttpServletResponse.SC_OK);
146 resp.setContentLength(output.length);
147 resp.setContentType("text/plain");
148 ServletOutputStream os = null;
150 os = resp.getOutputStream();
152 } catch (IOException e) {
153 LOG.warn("problem writing response for {}: {}", uri, e);
158 } catch (IOException e) {
159 LOG.warn("problem closing response stream: {}", e);
165 this.doGetFile(uri, resp);
170 * load git.commit.id from jar /META-INF/git.properties
174 private String getGitHash(String def) {
175 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
176 if (content == null) {
179 String lines[] = content.split("\n");
180 for (String line : lines) {
181 if (line.startsWith("git.commit.id")) {
182 def = line.substring("git.commit.id=".length());
189 private String getResourceFileContent(String filename) {
190 LOG.debug("try ti get content of {}", filename);
191 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
195 * collect dynamic data for about.md
197 private void collectData() {
198 LOG.info("collecting dynamic data");
200 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
201 this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles());
202 } catch (Exception e) {
203 LOG.warn("problem collecting system data: {}", e);
208 * get value for key out of /META-INF/MANIFEST.MF
213 protected String getManifestValue(String key) {
214 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
220 manifest = new Manifest(url.openStream());
221 Attributes attr = manifest.getMainAttributes();
222 return attr.getValue(key);
223 } catch (IOException e) {
224 LOG.warn("problem reading manifest: {}", e);
231 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
235 private PomPropertiesFile getPomProperties() {
236 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
237 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
238 PomPropertiesFile propfile;
243 propfile = new PomPropertiesFile(url.openStream());
245 } catch (Exception e) {
246 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
252 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
257 private String getPomProperty(String key) {
258 LOG.info("try to get pom property for {}", key);
259 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
260 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
266 pomfile = new PomFile(url.openStream());
267 return pomfile.getProperty(key);
268 } catch (Exception e) {
269 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
275 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
279 private String getPomParentVersion() {
280 LOG.info("try to get pom parent version");
281 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
282 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
288 pomfile = new PomFile(url.openStream());
289 return pomfile.getParentVersion();
290 } catch (Exception e) {
291 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
296 private String getDevicemanagerBundles() {
297 Bundle thisbundle = FrameworkUtil.getBundle(this.getClass());
298 BundleContext context = thisbundle ==null?null:thisbundle.getBundleContext();
299 if (context == null) {
300 LOG.debug("no bundle context available");
303 Bundle[] bundles = context.getBundles();
304 if (bundles == null || bundles.length <= 0) {
305 LOG.debug("no bundles found");
306 return NO_DEVICEMANAGERS_RUNNING_MESSAGE;
308 LOG.debug("found {} bundles", bundles.length);
309 MarkdownTable table = new MarkdownTable();
310 table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"});
312 for (Bundle bundle : bundles) {
313 name = bundle.getSymbolicName();
314 if(!(name.contains("devicemanager") && name.contains("provider"))) {
317 if(name.equals("org.onap.ccsdk.features.sdnr.wt.sdnr-wt-devicemanager-provider")) {
320 table.addRow(new String[] {String.valueOf(bundle.getBundleId()), bundle.getVersion().toString(), name,
321 BUNDLESTATE_LUT.getOrDefault(bundle.getState(),"unknown")});
324 return table.toMarkDown();
328 * get file by uri from resources and write out to response stream
333 private void doGetFile(String uri, HttpServletResponse resp) {
334 String content = this.getResourceFileContent(uri);
335 if (content == null) {
337 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
338 } catch (IOException e) {
339 LOG.debug("unable to send error response : {}", e);
342 byte[] data = content.getBytes();
343 resp.setStatus(HttpServletResponse.SC_OK);
344 resp.setContentType(this.getContentType(uri));
346 resp.getOutputStream().write(data);
347 } catch (IOException e) {
348 LOG.debug("unable to send data : {}", e);
355 * create http response contentType by filename
360 private String getContentType(String filename) {
361 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
369 return "image/" + ext;
371 return "application/json";
383 * render this.readmeContent with this.data
387 private String render() {
388 return this.render(null);
392 * render content with this.data
397 private String render(String content) {
398 if (content == null) {
399 content = this.readmeContent;
401 if (content == null) {
404 for (Entry<String, String> entry : this.data.entrySet()) {
405 if (entry.getValue() != null && content.contains(entry.getKey())) {
406 content = content.replace(entry.getKey(), entry.getValue());
413 public void setClusterSize(String value) {
414 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);