284365021529b67ad6bc03a97f55ba4e52b0609e
[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.apache.karaf.bundle.core.BundleInfo;
30 import org.apache.karaf.bundle.core.BundleService;
31 import org.apache.karaf.bundle.core.BundleState;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.about.MarkdownTable;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ReadyHttpServlet extends HttpServlet {
40
41     /**
42      *
43      */
44     private static final long serialVersionUID = 1L;
45     private static final Logger LOG = LoggerFactory.getLogger(ReadyHttpServlet.class);
46     private static boolean status;
47
48
49     private BundleService bundleService = null;
50
51     public void setBundleService(BundleService bundleService) {
52         this.bundleService  = bundleService;
53     }
54     @Override
55     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
56
57         if (isReady() && this.getBundleStatesReady()) {
58             resp.setStatus(HttpServletResponse.SC_OK);
59         } else {
60
61             try {
62                 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
63             } catch (IOException | IllegalStateException e) {
64                 LOG.warn("unable to write out 404 res not found: {}", e);
65             }
66         }
67     }
68
69     private static boolean isReady() {
70         return status;
71     }
72
73     public static void setStatus(boolean s) {
74         status = s;
75         LOG.info("status is set to ready: {}", status);
76     }
77
78     private boolean getBundleStatesReady() {
79         Bundle thisbundle = FrameworkUtil.getBundle(this.getClass());
80         BundleContext context = thisbundle ==null?null:thisbundle.getBundleContext();
81         if (context == null) {
82             LOG.debug("no bundle context available");
83             return true;
84         }
85         Bundle[] bundles = context.getBundles();
86         if (bundles == null || bundles.length <= 0) {
87             LOG.debug("no bundles found");
88             return true;
89         }
90         LOG.debug("found {} bundles", bundles.length);
91         MarkdownTable table = new MarkdownTable();
92         table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"});
93         int cntNotActive=0;
94
95         for (Bundle bundle : bundles) {
96             if(this.bundleService!=null) {
97                 BundleInfo info = this.bundleService.getInfo(bundle);
98                 if(info.getState()==BundleState.Active ) {
99                     continue;
100                 }
101                 if(info.getState()==BundleState.Resolved ) {
102                     if(!this.isBundleImportant(bundle.getSymbolicName())) {
103                         LOG.trace("ignore not important bundle {} with state {}",bundle.getSymbolicName(),info.getState());
104                         continue;
105                     }
106                 }
107
108                 LOG.trace("bundle {} is in state {}",bundle.getSymbolicName(),info.getState());
109             }
110             else {
111                 LOG.warn("bundle service is null");
112             }
113             cntNotActive++;
114         }
115
116         return cntNotActive==0;
117     }
118
119     private boolean isBundleImportant(String symbolicName) {
120         symbolicName = symbolicName.toLowerCase();
121         if(symbolicName.contains("mdsal")) {
122             return true;
123         }
124         if(symbolicName.contains("netconf")) {
125             return true;
126         }
127         if(symbolicName.contains("ccsdk")) {
128             return true;
129         }
130         if(symbolicName.contains("devicemanager")) {
131             return true;
132         }
133         if(symbolicName.contains("restconf")) {
134             return true;
135         }
136
137         return false;
138     }
139
140 }