3f2ca7224858ad158396d085f885498c611af581
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.apex.services.onappf.rest;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26
27 import org.onap.policy.apex.services.onappf.parameters.RestServerParameters;
28 import org.onap.policy.common.capabilities.Startable;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
30 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
31 import org.onap.policy.common.gson.GsonMessageBodyHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Class to manage life cycle of services-onappf rest server.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class ApexStarterRestServer implements Startable {
41     private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class);
42
43     private List<HttpServletServer> servers = new ArrayList<>();
44
45     private RestServerParameters restServerParameters;
46
47     /**
48      * Constructor for instantiating ApexStarterRestServer.
49      *
50      * @param restServerParameters the rest server parameters
51      */
52     public ApexStarterRestServer(final RestServerParameters restServerParameters) {
53         this.restServerParameters = restServerParameters;
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     public boolean start() {
61         try {
62             servers = HttpServletServer.factory.build(getServerProperties());
63             for (final HttpServletServer server : servers) {
64                 if (server.isAaf()) {
65                     server.addFilterClass(null, ApexStarterAafFilter.class.getName());
66                 }
67                 server.start();
68             }
69         } catch (final Exception exp) {
70             LOGGER.error("Failed to start services-onappf http server", exp);
71             return false;
72         }
73         return true;
74     }
75
76     /**
77      * Creates the server properties object using restServerParameters.
78      *
79      * @return the properties object
80      */
81     private Properties getServerProperties() {
82         final Properties props = new Properties();
83         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
84
85         final String svcpfx =
86                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
87
88         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
89         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
90                 Integer.toString(restServerParameters.getPort()));
91         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
92                 HealthCheckRestControllerV1.class.getName());
93         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
94         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
95         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
96                 restServerParameters.getUserName());
97         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
98                 restServerParameters.getPassword());
99         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
100                 String.valueOf(restServerParameters.isHttps()));
101         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
102                 String.valueOf(restServerParameters.isAaf()));
103         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
104                 GsonMessageBodyHandler.class.getName());
105         return props;
106     }
107
108     /**
109      * {@inheritDoc}.
110      */
111     @Override
112     public boolean stop() {
113         for (final HttpServletServer server : servers) {
114             try {
115                 server.stop();
116             } catch (final Exception exp) {
117                 LOGGER.error("Failed to stop services-onappf http server", exp);
118             }
119         }
120         return true;
121     }
122
123     /**
124      * {@inheritDoc}.
125      */
126     @Override
127     public void shutdown() {
128         stop();
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
135     public boolean isAlive() {
136         return !servers.isEmpty();
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public String toString() {
144         final StringBuilder builder = new StringBuilder();
145         builder.append("ApexStarterRestServer [servers=");
146         builder.append(servers);
147         builder.append("]");
148         return builder.toString();
149     }
150 }