Hotfix for compilation errors
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / ApiRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019 AT&T Intellectual Property. 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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.rest;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29 import org.onap.policy.api.main.parameters.RestServerParameters;
30 import org.onap.policy.api.main.rest.aaf.AafApiFilter;
31 import org.onap.policy.common.capabilities.Startable;
32 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
33 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
34 import org.onap.policy.models.tosca.serialization.simple.ToscaServiceTemplateMessageBodyHandler;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39 /**
40  * Class to manage life cycle of api rest server.
41  *
42  */
43 public class ApiRestServer implements Startable {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class);
46
47     private List<HttpServletServer> servers = new ArrayList<>();
48
49     private RestServerParameters restServerParameters;
50
51     /**
52      * Constructor for instantiating ApiRestServer.
53      *
54      * @param restServerParameters the rest server parameters
55      */
56     public ApiRestServer(final RestServerParameters restServerParameters) {
57         this.restServerParameters = restServerParameters;
58     }
59
60     /**
61      * {@inheritDoc}.
62      */
63     @Override
64     public boolean start() {
65         try {
66             servers = HttpServletServer.factory.build(getServerProperties());
67             for (HttpServletServer server : servers) {
68                 if (server.isAaf()) {
69                     server.addFilterClass(null, AafApiFilter.class.getCanonicalName());
70                 }
71                 server.start();
72             }
73         } catch (final Exception exp) {
74             LOGGER.error("Failed to start api http server", exp);
75             return false;
76         }
77         return true;
78     }
79
80     /**
81      * Creates the server properties object using restServerParameters.
82      *
83      * @return the properties object
84      */
85     private Properties getServerProperties() {
86         final Properties props = new Properties();
87         final String svcpfx =
88                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
89
90         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
91         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
92         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
93                         Integer.toString(restServerParameters.getPort()));
94         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
95                         ApiRestController.class.getCanonicalName());
96         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
97         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
98         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
99                         restServerParameters.getUserName());
100         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
101                         restServerParameters.getPassword());
102         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
103                         String.valueOf(restServerParameters.isHttps()));
104         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
105                         String.valueOf(restServerParameters.isAaf()));
106         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
107                         ToscaServiceTemplateMessageBodyHandler.class.getName());
108
109         return props;
110     }
111
112     /**
113      * {@inheritDoc}.
114      */
115     @Override
116     public boolean stop() {
117         for (final HttpServletServer server : servers) {
118             try {
119                 server.stop();
120             } catch (final Exception exp) {
121                 LOGGER.error("Failed to stop api http server", exp);
122             }
123         }
124         return true;
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public void shutdown() {
132         stop();
133     }
134
135     /**
136      * {@inheritDoc}.
137      */
138     @Override
139     public boolean isAlive() {
140         return !servers.isEmpty();
141     }
142
143     /**
144      * {@inheritDoc}.
145      */
146     @Override
147     public String toString() {
148         final StringBuilder builder = new StringBuilder();
149         builder.append("ApiRestServer [servers=");
150         builder.append(servers);
151         builder.append("]");
152         return builder.toString();
153     }
154
155 }