added file to test ApiRunttimeException.java
[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.simple.serialization.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                         String.join(",", LegacyApiRestController.class.getCanonicalName(),
96                                          ApiRestController.class.getCanonicalName()));
97         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
98         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
99         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
100                         restServerParameters.getUserName());
101         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
102                         restServerParameters.getPassword());
103         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
104                         String.valueOf(restServerParameters.isHttps()));
105         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
106                         String.valueOf(restServerParameters.isAaf()));
107         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
108                         ToscaServiceTemplateMessageBodyHandler.class.getName());
109
110         return props;
111     }
112
113     /**
114      * {@inheritDoc}.
115      */
116     @Override
117     public boolean stop() {
118         for (final HttpServletServer server : servers) {
119             try {
120                 server.stop();
121             } catch (final Exception exp) {
122                 LOGGER.error("Failed to stop api http server", exp);
123             }
124         }
125         return true;
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public void shutdown() {
133         stop();
134     }
135
136     /**
137      * {@inheritDoc}.
138      */
139     @Override
140     public boolean isAlive() {
141         return !servers.isEmpty();
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public String toString() {
149         final StringBuilder builder = new StringBuilder();
150         builder.append("ApiRestServer [servers=");
151         builder.append(servers);
152         builder.append("]");
153         return builder.toString();
154     }
155
156 }