Fix api due to sonar changes in common
[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.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 /**
41  * Class to manage life cycle of api rest server.
42  *
43  */
44 public class ApiRestServer implements Startable {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(ApiRestServer.class);
47
48     private List<HttpServletServer> servers = new ArrayList<>();
49
50     private RestServerParameters restServerParameters;
51
52     /**
53      * Constructor for instantiating ApiRestServer.
54      *
55      * @param restServerParameters the rest server parameters
56      */
57     public ApiRestServer(final RestServerParameters restServerParameters) {
58         this.restServerParameters = restServerParameters;
59     }
60
61     /**
62      * {@inheritDoc}.
63      */
64     @Override
65     public boolean start() {
66         try {
67             servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
68             for (HttpServletServer server : servers) {
69                 if (server.isAaf()) {
70                     server.addFilterClass(null, AafApiFilter.class.getName());
71                 }
72                 server.start();
73             }
74         } catch (final Exception exp) {
75             LOGGER.error("Failed to start api http server", exp);
76             return false;
77         }
78         return true;
79     }
80
81     /**
82      * Creates the server properties object using restServerParameters.
83      *
84      * @return the properties object
85      */
86     private Properties getServerProperties() {
87         final Properties props = new Properties();
88         final String svcpfx =
89                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
90
91         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
92         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
93         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
94                         Integer.toString(restServerParameters.getPort()));
95         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
96                         String.join(",", LegacyApiRestController.class.getName(),
97                                          ApiRestController.class.getName()));
98         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
99         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
100         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
101                         restServerParameters.getUserName());
102         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
103                         restServerParameters.getPassword());
104         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
105                         String.valueOf(restServerParameters.isHttps()));
106         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
107                         String.valueOf(restServerParameters.isAaf()));
108         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
109                         GsonMessageBodyHandler.class.getName());
110
111         return props;
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public boolean stop() {
119         for (final HttpServletServer server : servers) {
120             try {
121                 server.stop();
122             } catch (final Exception exp) {
123                 LOGGER.error("Failed to stop api http server", exp);
124             }
125         }
126         return true;
127     }
128
129     /**
130      * {@inheritDoc}.
131      */
132     @Override
133     public void shutdown() {
134         stop();
135     }
136
137     /**
138      * {@inheritDoc}.
139      */
140     @Override
141     public boolean isAlive() {
142         return !servers.isEmpty();
143     }
144
145     /**
146      * {@inheritDoc}.
147      */
148     @Override
149     public String toString() {
150         final StringBuilder builder = new StringBuilder();
151         builder.append("ApiRestServer [servers=");
152         builder.append(servers);
153         builder.append("]");
154         return builder.toString();
155     }
156
157 }