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;
24 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.jar.Attributes;
32 import java.util.jar.Manifest;
34 import javax.servlet.ServletException;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServlet;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 //import org.apache.karaf.bundle.core.BundleInfo;
41 //import org.apache.karaf.bundle.core.BundleService;
42 import org.onap.ccsdk.features.sdnr.wt.common.Resources;
43 import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
44 import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
45 import org.osgi.framework.Bundle;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.FrameworkUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class AboutHttpServlet extends HttpServlet {
56 private static final long serialVersionUID = 1L;
57 private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
58 private static final String UNKNOWN = "unknown";
59 private static final String METAINF_MAVEN = "/META-INF/maven/";
60 private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";
62 private static final String URI_PRE = "/about";
63 private static final String RES_BASEPATH = "about/";
65 private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
66 private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
67 private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
68 private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
69 private static final String PLACEHOLDER_ODLUX_REVISION = "{odlux-revision}";
70 private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
71 private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
72 private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
73 private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
74 private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
75 private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
76 private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
77 private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}";
78 private static final String README_FILE = "README.md";
80 private final String groupId = "org.onap.ccsdk.features.sdnr.wt";
81 private final String artifactId = "sdnr-wt-data-provider-provider";
83 private final Map<String, String> data;
84 private final String readmeContent;
85 // private BundleService bundleService;
88 public AboutHttpServlet() {
90 this.data = new HashMap<>();
91 this.collectStaticData();
92 this.readmeContent = this.render(this.getResourceFileContent(README_FILE));
93 //BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
97 // public void setBundleService(BundleService bundleService) {
98 // this.bundleService = bundleService;
102 * collect static versioning data
104 private void collectStaticData() {
105 PomPropertiesFile props = this.getPomProperties();
106 final String ccsdkVersion = this.getPomParentVersion();
107 this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
108 this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(ccsdkVersion, UNKNOWN));
109 this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
110 this.data.put(PLACEHOLDER_ODLUX_REVISION, this.getPomProperty("odlux.buildno"));
111 this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
112 this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
113 this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, "2.0.0-SNAPSHOT");
114 this.data.put(PLACEHOLDER_MDSAL_VERSION, SystemInfo.getMdSalVersion(UNKNOWN));
115 this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
116 this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
120 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
122 String uri = req.getRequestURI().substring(URI_PRE.length());
123 LOG.debug("request for {}", uri);
124 if (uri.length() <= 0 || uri.equals("/")) {
128 String content = this.render();
129 byte[] output = content != null ? content.getBytes() : new byte[0];
131 resp.setStatus(HttpServletResponse.SC_OK);
132 resp.setContentLength(output.length);
133 resp.setContentType("text/plain");
134 ServletOutputStream os = null;
136 os = resp.getOutputStream();
138 } catch (IOException e) {
139 LOG.warn("problem writing response for {}: {}", uri, e);
144 } catch (IOException e) {
145 LOG.warn("problem closing response stream: {}", e);
151 this.doGetFile(uri, resp);
156 * load git.commit.id from jar /META-INF/git.properties
160 private String getGitHash(String def) {
161 String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
162 if (content == null) {
165 String lines[] = content.split("\n");
166 for (String line : lines) {
167 if (line.startsWith("git.commit.id")) {
168 def = line.substring("git.commit.id=".length());
175 private String getResourceFileContent(String filename) {
176 LOG.debug("try ti get content of {}", filename);
177 return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
181 * collect dynamic data for about.md
183 private void collectData() {
184 LOG.info("collecting dynamic data");
186 this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
187 this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles());
188 } catch (Exception e) {
189 LOG.warn("problem collecting system data: {}", e);
194 * get value for key out of /META-INF/MANIFEST.MF
199 private String getManifestValue(String key) {
200 URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
206 manifest = new Manifest(url.openStream());
207 Attributes attr = manifest.getMainAttributes();
208 return attr.getValue(key);
209 } catch (IOException e) {
210 LOG.warn("problem reading manifest: {}", e);
217 * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
221 private PomPropertiesFile getPomProperties() {
222 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
223 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
224 PomPropertiesFile propfile;
229 propfile = new PomPropertiesFile(url.openStream());
231 } catch (Exception e) {
232 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
238 * get value for key out of /META-INF/maven/groupId/artifactId/pom.xml in properties section
243 private String getPomProperty(String key) {
244 LOG.info("try to get pom property for {}", key);
245 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
246 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
252 pomfile = new PomFile(url.openStream());
253 return pomfile.getProperty(key);
254 } catch (Exception e) {
255 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
261 * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
265 private String getPomParentVersion() {
266 LOG.info("try to get pom parent version");
267 URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
268 METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
274 pomfile = new PomFile(url.openStream());
275 return pomfile.getParentVersion();
276 } catch (Exception e) {
277 LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
282 private String getDevicemanagerBundles() {
283 // if(this.bundleService==null) {
284 // LOG.debug("no bundle service available");
288 // List<String> ids = new ArrayList<String>();
289 // List<Bundle> bundles = bundleService.selectBundles("0", ids , true);
290 // if(bundles==null || bundles.size()<=0) {
291 // LOG.debug("no bundles found");
294 // LOG.debug("found {} bundles",bundles.size());
295 // MarkdownTable table = new MarkdownTable();
296 // for(Bundle bundle:bundles) {
297 // BundleInfo info = this.bundleService.getInfo(bundle);
298 // table.addRow(new String[] {String.valueOf(info.getBundleId()),info.getVersion(),info.getName(),info.getState().toString()});
300 // return table.toMarkDown();
305 * get file by uri from resources and write out to response stream
310 private void doGetFile(String uri, HttpServletResponse resp) {
311 String content = this.getResourceFileContent(uri);
312 if (content == null) {
314 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
315 } catch (IOException e) {
316 LOG.debug("unable to send error response : {}", e);
319 byte[] data = content.getBytes();
320 resp.setStatus(HttpServletResponse.SC_OK);
321 resp.setContentType(this.getContentType(uri));
323 resp.getOutputStream().write(data);
324 } catch (IOException e) {
325 LOG.debug("unable to send data : {}", e);
332 * create http response contentType by filename
337 private String getContentType(String filename) {
338 String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
346 return "image/" + ext;
348 return "application/json";
360 * render this.readmeContent with this.data
364 private String render() {
365 return this.render(null);
369 * render content with this.data
374 private String render(String content) {
375 if (content == null) {
376 content = this.readmeContent;
378 if (content == null) {
381 for (Entry<String, String> entry : this.data.entrySet()) {
382 if (entry.getValue() != null && content.contains(entry.getKey())) {
383 content = content.replace(entry.getKey(), entry.getValue());
390 public void setClusterSize(String value) {
391 this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);