Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-onappf / src / main / java / org / onap / policy / apex / services / onappf / rest / ApexStarterRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.services.onappf.rest;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
30 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.onap.policy.common.gson.GsonMessageBodyHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Class to manage life cycle of services-onappf rest server.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 public class ApexStarterRestServer implements Startable {
42     private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class);
43
44     private List<HttpServletServer> servers = new ArrayList<>();
45
46     private RestServerParameters restServerParameters;
47
48     /**
49      * Constructor for instantiating ApexStarterRestServer.
50      *
51      * @param restServerParameters the rest server parameters
52      */
53     public ApexStarterRestServer(final RestServerParameters restServerParameters) {
54         this.restServerParameters = restServerParameters;
55     }
56
57     /**
58      * {@inheritDoc}.
59      */
60     @Override
61     public boolean start() {
62         try {
63             servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
64             for (final HttpServletServer server : servers) {
65                 if (server.isAaf()) {
66                     server.addFilterClass(null, ApexStarterAafFilter.class.getName());
67                 }
68                 server.start();
69             }
70         } catch (final Exception exp) {
71             LOGGER.error("Failed to start services-onappf http server", exp);
72             return false;
73         }
74         return true;
75     }
76
77     /**
78      * Creates the server properties object using restServerParameters.
79      *
80      * @return the properties object
81      */
82     private Properties getServerProperties() {
83         final Properties props = new Properties();
84         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
85
86         final String svcpfx =
87                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
88
89         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
90         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
91                 Integer.toString(restServerParameters.getPort()));
92         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
93                 HealthCheckRestControllerV1.class.getName());
94         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
95         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
96         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
97                 restServerParameters.getUserName());
98         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
99                 restServerParameters.getPassword());
100         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
101                 String.valueOf(restServerParameters.isHttps()));
102         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
103                 String.valueOf(restServerParameters.isAaf()));
104         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
105                 GsonMessageBodyHandler.class.getName());
106         return props;
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public boolean stop() {
114         for (final HttpServletServer server : servers) {
115             try {
116                 server.stop();
117             } catch (final Exception exp) {
118                 LOGGER.error("Failed to stop services-onappf http server", exp);
119             }
120         }
121         return true;
122     }
123
124     /**
125      * {@inheritDoc}.
126      */
127     @Override
128     public void shutdown() {
129         stop();
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public boolean isAlive() {
137         return !servers.isEmpty();
138     }
139
140     /**
141      * {@inheritDoc}.
142      */
143     @Override
144     public String toString() {
145         final StringBuilder builder = new StringBuilder();
146         builder.append("ApexStarterRestServer [servers=");
147         builder.append(servers);
148         builder.append("]");
149         return builder.toString();
150     }
151 }