cb74911ffaa0233c51e1741a137cc471383cde76
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.http;
23
24 import java.io.IOException;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.MarkdownTable;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.FrameworkUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class ReadyHttpServlet extends HttpServlet {
37
38     /**
39      *
40      */
41     private static final long serialVersionUID = 1L;
42     private static final Logger LOG = LoggerFactory.getLogger(ReadyHttpServlet.class);
43     private static boolean status;
44
45     @Override
46     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
47
48         if (isReady() && this.getBundleStatesNotActiveCount()==0) {
49             resp.setStatus(HttpServletResponse.SC_OK);
50         } else {
51
52             try {
53                 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
54             } catch (IOException | IllegalStateException e) {
55                 LOG.warn("unable to write out 404 res not found: {}", e);
56             }
57         }
58     }
59
60     private static boolean isReady() {
61         return status;
62     }
63
64     public static void setStatus(boolean s) {
65         status = s;
66         LOG.info("status is set to ready: {}", status);
67     }
68
69     private int getBundleStatesNotActiveCount() {
70         Bundle thisbundle = FrameworkUtil.getBundle(this.getClass());
71         BundleContext context = thisbundle ==null?null:thisbundle.getBundleContext();
72         if (context == null) {
73             LOG.debug("no bundle context available");
74             return 0;
75         }
76         Bundle[] bundles = context.getBundles();
77         if (bundles == null || bundles.length <= 0) {
78             LOG.debug("no bundles found");
79             return 0;
80         }
81         LOG.debug("found {} bundles", bundles.length);
82         MarkdownTable table = new MarkdownTable();
83         table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"});
84         int cntNotActive=0;
85         for (Bundle bundle : bundles) {
86
87             if(bundle.getState()!=Bundle.ACTIVE) {
88                 cntNotActive++;
89             }
90
91         }
92         return cntNotActive;
93     }
94
95 }