aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/http/about/AboutHttpServlet.java
blob: cd75ca2a846171ee372a2c96fbeeeb1f6ce8a1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpHeaders;
import org.onap.ccsdk.features.sdnr.wt.common.Resources;
import org.onap.ccsdk.features.sdnr.wt.common.file.PomFile;
import org.onap.ccsdk.features.sdnr.wt.common.file.PomPropertiesFile;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AboutHttpServlet extends HttpServlet {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(AboutHttpServlet.class);
    private static final String UNKNOWN = "unknown";
    private static final String METAINF_MAVEN = "/META-INF/maven/";
    private static final String EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE = "unable to read inner pom file: {}";

    private static final String URI_PRE = "/about";
    private static final String RES_BASEPATH = "about/";

    private static final String PLACEHOLDER_ONAP_RELEASENAME = "{release-name}";
    private static final String PLACEHOLDER_ONAP_RELEASEVERSION = "{release-version}";
    private static final String PLACEHOLDER_ODL_RELEASENAME = "{odl-version}";
    private static final String PLACEHOLDER_BUILD_TIMESTAMP = "{build-time}";
    private static final String PLACEHOLDER_PACKAGE_GITHASH = "{package-githash}";
    private static final String PLACEHOLDER_PACAKGE_VERSION = "{package-version}";
    private static final String PLACEHOLDER_CCSDK_VERSION = "{ccsdk-version}";
    private static final String PLACEHOLDER_CLUSTER_SIZE = "{cluster-size}";
    private static final String PLACEHOLDER_MDSAL_VERSION = "{mdsal-version}";
    private static final String PLACEHOLDER_YANGTOOLS_VERSION = "{yangtools-version}";
    private static final String PLACEHOLDER_KARAF_INFO = "{karaf-info}";
    private static final String PLACEHOLDER_DEVICEMANAGER_TABLE = "{devicemanagers}";
    private static final String README_FILE = "README.md";
    private static final String JSON_FILE = "README.json";
    private static final String NO_DEVICEMANAGERS_RUNNING_MESSAGE = null;
    private static final String MIMETYPE_JSON = "application/json";
    private static final String MIMETYPE_MARKDOWN = "text/markdown";

    private final String groupId = this.getGroupIdOrDefault("org.onap.ccsdk.features.sdnr.wt");
    private final String artifactId = "sdnr-wt-data-provider-provider";

    private final Map<Integer, String> BUNDLESTATE_LUT;
    private final Map<String, String> data;
    private final String readmeContent;
    private final String jsonContent;


    public AboutHttpServlet() {

        this.data = new HashMap<>();
        this.collectStaticData();
        this.readmeContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(README_FILE));
        this.jsonContent = this.render(ContentType.MARKDOWN, this.getResourceFileContent(JSON_FILE));
        this.BUNDLESTATE_LUT = new HashMap<>();
        this.BUNDLESTATE_LUT.put(Bundle.UNINSTALLED, "uninstalled");
        this.BUNDLESTATE_LUT.put(Bundle.INSTALLED, "installed");
        this.BUNDLESTATE_LUT.put(Bundle.RESOLVED, "resolved");
        this.BUNDLESTATE_LUT.put(Bundle.STARTING, "starting");
        this.BUNDLESTATE_LUT.put(Bundle.STOPPING, "stopping");
        this.BUNDLESTATE_LUT.put(Bundle.ACTIVE, "active");

    }

    protected String getGroupIdOrDefault(String def) {
        String symbolicName = this.getManifestValue("Bundle-SymbolicName");
        if (symbolicName != null) {
            int idx = symbolicName.indexOf(this.artifactId);
            if (idx > 0) {
                return symbolicName.substring(0, idx - 1);
            }
        }
        return def;
    }

    /**
     * collect static versioning data
     */
    private void collectStaticData() {
        PomPropertiesFile props = this.getPomProperties();
        final String ccsdkVersion = this.getPomParentVersion();
        final String mdsalVersion = SystemInfo.getMdSalVersion(UNKNOWN);
        this.data.put(PLACEHOLDER_ONAP_RELEASENAME, ODLVersionLUT.getONAPReleaseName(ccsdkVersion, UNKNOWN));
        this.data.put(PLACEHOLDER_ODL_RELEASENAME, ODLVersionLUT.getOdlVersion(mdsalVersion, UNKNOWN));
        this.data.put(PLACEHOLDER_BUILD_TIMESTAMP, props != null ? props.getBuildDate().toString() : "");
        this.data.put(PLACEHOLDER_PACAKGE_VERSION, this.getManifestValue("Bundle-Version"));
        this.data.put(PLACEHOLDER_CCSDK_VERSION, ccsdkVersion);
        this.data.put(PLACEHOLDER_ONAP_RELEASEVERSION, SystemInfo.getOnapVersion(UNKNOWN));
        this.data.put(PLACEHOLDER_MDSAL_VERSION, mdsalVersion);
        this.data.put(PLACEHOLDER_YANGTOOLS_VERSION, SystemInfo.getYangToolsVersion(UNKNOWN));
        this.data.put(PLACEHOLDER_PACKAGE_GITHASH, this.getGitHash(UNKNOWN));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uri = req.getRequestURI().substring(URI_PRE.length());
        LOG.debug("request for {}", uri);
        if (uri.length() <= 0 || uri.equals("/")) {
            ContentType ctype = this.detectContentType(req, ContentType.MARKDOWN);
            // collect data
            this.collectData(ctype);
            // render readme
            String content = this.render(ctype);
            byte[] output = content != null ? content.getBytes() : new byte[0];
            // output
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentLength(output.length);
            resp.setContentType(ctype.getMimeType());
            try (ServletOutputStream os = resp.getOutputStream()) {
                os.write(output);
            } catch (IOException e) {
                LOG.warn("problem writing response for {}: {}", uri, e);
            }
        } else {
            this.doGetFile(uri, resp);
        }
    }

    /**
     * load git.commit.id from jar /META-INF/git.properties
     *
     * @param def
     */
    private String getGitHash(String def) {
        String content = Resources.getFileContent(AboutHttpServlet.class, "/META-INF/git.properties");
        if (content == null) {
            return def;
        }
        String[] lines = content.split("\n");
        for (String line : lines) {
            if (line.startsWith("git.commit.id")) {
                def = line.substring("git.commit.id=".length());
                break;
            }
        }
        return def;
    }

    private String getResourceFileContent(String filename) {
        LOG.debug("try ti get content of {}", filename);
        return Resources.getFileContent(AboutHttpServlet.class, RES_BASEPATH + filename);
    }

    /**
     * collect dynamic data for about.md
     */
    private void collectData(ContentType ctype) {
        LOG.info("collecting dynamic data");
        try {
            this.data.put(PLACEHOLDER_KARAF_INFO, SystemInfo.get());
            this.data.put(PLACEHOLDER_DEVICEMANAGER_TABLE, this.getDevicemanagerBundles(ctype));
        } catch (Exception e) {
            LOG.warn("problem collecting system data: ", e);
        }
    }

    /**
     * get value for key out of /META-INF/MANIFEST.MF
     *
     * @param key
     * @return
     */
    protected String getManifestValue(String key) {
        URL url = Resources.getUrlForRessource(AboutHttpServlet.class, "/META-INF/MANIFEST.MF");
        if (url == null) {
            return null;
        }
        Manifest manifest;
        try {
            manifest = new Manifest(url.openStream());
            Attributes attr = manifest.getMainAttributes();
            return attr.getValue(key);
        } catch (IOException e) {
            LOG.warn("problem reading manifest: ", e);
        }
        return null;

    }

    /**
     * get object representation of /META-INF/maven/groupId/artifactId/pom.properties
     *
     * @return
     */
    private PomPropertiesFile getPomProperties() {
        URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
                METAINF_MAVEN + groupId + "/" + artifactId + "/pom.properties");
        PomPropertiesFile propfile;
        if (url == null) {
            return null;
        }
        try {
            propfile = new PomPropertiesFile(url.openStream());
            return propfile;
        } catch (Exception e) {
            LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
        }
        return null;
    }

    /**
     * get parent pom version out of /META-INF/maven/groupId/artifactId/pom.xml
     *
     * @return
     */
    private String getPomParentVersion() {
        LOG.info("try to get pom parent version");
        URL url = Resources.getUrlForRessource(AboutHttpServlet.class,
                METAINF_MAVEN + groupId + "/" + artifactId + "/pom.xml");
        if (url == null) {
            return null;
        }
        PomFile pomfile;
        try {
            pomfile = new PomFile(url.openStream());
            return pomfile.getParentVersion();
        } catch (Exception e) {
            LOG.warn(EXCEPTION_FORMAT_UNABLE_TO_READ_INNER_POMFILE, e);
        }
        return null;
    }

    private String getDevicemanagerBundles(ContentType ctype) {
        Bundle thisbundle = FrameworkUtil.getBundle(this.getClass());
        BundleContext context = thisbundle == null ? null : thisbundle.getBundleContext();
        if (context == null) {
            LOG.debug("no bundle context available");
            return ctype == ContentType.MARKDOWN ? "" : "[]";
        }
        Bundle[] bundles = context.getBundles();
        if (bundles == null || bundles.length <= 0) {
            LOG.debug("no bundles found");
            return ctype == ContentType.MARKDOWN ? NO_DEVICEMANAGERS_RUNNING_MESSAGE : "[]";
        }
        LOG.debug("found {} bundles", bundles.length);
        MarkdownTable table = new MarkdownTable();
        table.setHeader(new String[] {"Bundle-Id", "Version", "Symbolic-Name", "Status"});
        String name;
        for (Bundle bundle : bundles) {
            name = bundle.getSymbolicName();
            if (!(name.contains("devicemanager") && name.contains("provider"))) {
                continue;
            }
            if (name.equals("org.onap.ccsdk.features.sdnr.wt.sdnr-wt-devicemanager-provider")) {
                continue;
            }
            table.addRow(new String[] {String.valueOf(bundle.getBundleId()), bundle.getVersion().toString(), name,
                    BUNDLESTATE_LUT.getOrDefault(bundle.getState(), UNKNOWN)});

        }
        return ctype == ContentType.MARKDOWN ? table.toMarkDown() : table.toJson();
    }

    /**
     * get file by uri from resources and write out to response stream
     *
     * @param uri
     * @param resp
     */
    private void doGetFile(String uri, HttpServletResponse resp) {
        String content = this.getResourceFileContent(uri);
        if (content == null) {
            try {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            } catch (IOException e) {
                LOG.debug("unable to send error response : {}", e);
            }
        } else {
            byte[] data = content.getBytes();
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentType(this.getContentType(uri));
            try {
                resp.getOutputStream().write(data);
            } catch (IOException e) {
                LOG.debug("unable to send data: ", e);
            }
        }

    }

    /**
     * create http response contentType by filename
     *
     * @param filename
     * @return
     */
    private String getContentType(String filename) {
        String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
        switch (ext) {
            case "jpg":
            case "jpeg":
            case "svg":
            case "png":
            case "gif":
            case "bmp":
                return "image/" + ext;
            case "json":
                return MIMETYPE_JSON;
            case "html":
            case "htm":
                return "text/html";
            case "txt":
            case "md":
            default:
                return "text/plain";
        }
    }

    /**
     * render this.readmeContent with this.data
     *
     * @param ctype
     *
     * @return
     */
    private String render(ContentType ctype) {
        return this.render(ctype, null);
    }

    /**
     * render content with this.data
     *
     * @param content
     * @return
     */
    private String render(ContentType ctype, String content) {
        if (content == null) {
            content = ctype == ContentType.MARKDOWN ? this.readmeContent : this.jsonContent;
        }
        if (content == null) {
            return null;
        }
        for (Entry<String, String> entry : this.data.entrySet()) {
            if (entry.getValue() != null && content.contains(entry.getKey())) {
                content = content.replace(entry.getKey(), entry.getValue());
            }
        }

        return content;
    }

    public void setClusterSize(String value) {
        this.data.put(PLACEHOLDER_CLUSTER_SIZE, value);
    }

    private ContentType detectContentType(HttpServletRequest req, ContentType def) {
        String accept = req.getHeader(HttpHeaders.ACCEPT);
        if (accept != null) {
            if (accept.equals(MIMETYPE_JSON)) {
                return ContentType.JSON;
            } else if (accept.equals(MIMETYPE_MARKDOWN)) {
                return ContentType.MARKDOWN;
            }
        }
        return def;
    }

    private enum ContentType {
        MARKDOWN(MIMETYPE_MARKDOWN), JSON(MIMETYPE_JSON);

        private String mimeType;

        ContentType(String mimeType) {
            this.mimeType = mimeType;
        }

        String getMimeType() {
            return this.mimeType;
        }
    }
}