Clean up checkstyle declaration
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / ApiRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.api.main.rest;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import org.onap.policy.api.main.parameters.RestServerParameters;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31
32 /**
33  * Class to manage life cycle of api rest server.
34  *
35  */
36 public class ApiRestServer implements Startable {
37
38     private static final String SEPARATOR = ".";
39     private static final String HTTP_SERVER_SERVICES = "http.server.services";
40     private static final Logger LOGGER = FlexLogger.getLogger(ApiRestServer.class);
41
42     private List<HttpServletServer> servers = new ArrayList<>();
43
44     private RestServerParameters restServerParameters;
45
46     /**
47      * Constructor for instantiating ApiRestServer.
48      *
49      * @param restServerParameters the rest server parameters
50      */
51     public ApiRestServer(final RestServerParameters restServerParameters) {
52         this.restServerParameters = restServerParameters;
53     }
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public boolean start() {
60         try {
61             servers = HttpServletServer.factory.build(getServerProperties());
62             for (final HttpServletServer server : servers) {
63                 server.start();
64             }
65         } catch (final Exception exp) {
66             LOGGER.error("Failed to start api http server", exp);
67             return false;
68         }
69         return true;
70     }
71
72     /**
73      * Creates the server properties object using restServerParameters.
74      *
75      * @return the properties object
76      */
77     private Properties getServerProperties() {
78         final Properties props = new Properties();
79         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
80         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
81                 restServerParameters.getHost());
82         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
83                 Integer.toString(restServerParameters.getPort()));
84         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
85                 ApiRestController.class.getCanonicalName());
86         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
87         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
88         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
89                 restServerParameters.getUserName());
90         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
91                 restServerParameters.getPassword());
92         return props;
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public boolean stop() {
100         for (final HttpServletServer server : servers) {
101             try {
102                 server.stop();
103             } catch (final Exception exp) {
104                 LOGGER.error("Failed to stop api http server", exp);
105             }
106         }
107         return true;
108     }
109
110     /**
111      * {@inheritDoc}.
112      */
113     @Override
114     public void shutdown() {
115         stop();
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public boolean isAlive() {
123         return !servers.isEmpty();
124     }
125
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public String toString() {
131         final StringBuilder builder = new StringBuilder();
132         builder.append("ApiRestServer [servers=");
133         builder.append(servers);
134         builder.append("]");
135         return builder.toString();
136     }
137
138 }