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;
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;
39 public class ReadyHttpServlet extends HttpServlet {
44 private static final long serialVersionUID = 1L;
45 private static final Logger LOG = LoggerFactory.getLogger(ReadyHttpServlet.class);
46 private static boolean status;
49 private BundleService bundleService = null;
51 public void setBundleService(BundleService bundleService) {
52 this.bundleService = bundleService;
55 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
57 if (isReady() && this.getBundleStatesReady()) {
58 resp.setStatus(HttpServletResponse.SC_OK);
62 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
63 } catch (IOException | IllegalStateException e) {
64 LOG.warn("unable to write out 404 res not found: {}", e);
69 private static boolean isReady() {
73 public static void setStatus(boolean s) {
75 LOG.info("status is set to ready: {}", status);
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");
85 Bundle[] bundles = context.getBundles();
86 if (bundles == null || bundles.length <= 0) {
87 LOG.debug("no bundles found");
90 LOG.debug("found {} bundles", bundles.length);
91 MarkdownTable table = new MarkdownTable();
92 table.setHeader(new String[] {"Bundle-Id","Version","Symbolic-Name","Status"});
95 for (Bundle bundle : bundles) {
96 if(this.bundleService!=null) {
97 BundleInfo info = this.bundleService.getInfo(bundle);
98 if(info.getState()==BundleState.Active ) {
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());
108 LOG.trace("bundle {} is in state {}",bundle.getSymbolicName(),info.getState());
111 LOG.warn("bundle service is null");
116 return cntNotActive==0;
119 private boolean isBundleImportant(String symbolicName) {
120 symbolicName = symbolicName.toLowerCase();
121 if(symbolicName.contains("mdsal")) {
124 if(symbolicName.contains("netconf")) {
127 if(symbolicName.contains("ccsdk")) {
130 if(symbolicName.contains("devicemanager")) {
133 if(symbolicName.contains("restconf")) {