85c6f3c6e0dce312847be37d351e9eb35b70ccbc
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PapRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
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.pap.main.rest;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27
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.onap.policy.pap.main.parameters.RestServerParameters;
33 import org.onap.policy.pap.main.rest.depundep.PdpGroupDeleteControllerV1;
34 import org.onap.policy.pap.main.rest.depundep.PdpGroupDeployControllerV1;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Class to manage life cycle of PAP rest server.
40  *
41  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42  */
43 public class PapRestServer implements Startable {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PapRestServer.class);
46
47     private List<HttpServletServer> servers = new ArrayList<>();
48
49     private RestServerParameters restServerParameters;
50
51     /**
52      * Constructor for instantiating PapRestServer.
53      *
54      * @param restServerParameters the rest server parameters
55      */
56     public PapRestServer(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 (final HttpServletServer server : servers) {
68                 if (server.isAaf()) {
69                     server.addFilterClass(null, PapAafFilter.class.getName());
70                 }
71                 server.start();
72             }
73         } catch (final Exception exp) {
74             LOGGER.error("Failed to start pap 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         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
88
89         final String svcpfx =
90                         PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
91
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(",", HealthCheckRestControllerV1.class.getName(),
97                                         StatisticsRestControllerV1.class.getName(),
98                                         PdpGroupDeployControllerV1.class.getName(),
99                                         PdpGroupDeleteControllerV1.class.getName(),
100                                         PdpGroupStateChangeControllerV1.class.getName(),
101                                         PdpGroupQueryControllerV1.class.getName(),
102                                         PdpGroupHealthCheckControllerV1.class.getName()));
103         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
104         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
105         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
106                         restServerParameters.getUserName());
107         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
108                         restServerParameters.getPassword());
109         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
110                         String.valueOf(restServerParameters.isHttps()));
111         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
112                         String.valueOf(restServerParameters.isAaf()));
113         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
114                         GsonMessageBodyHandler.class.getName());
115         return props;
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public boolean stop() {
123         for (final HttpServletServer server : servers) {
124             try {
125                 server.stop();
126             } catch (final Exception exp) {
127                 LOGGER.error("Failed to stop pap http server", exp);
128             }
129         }
130         return true;
131     }
132
133     /**
134      * {@inheritDoc}.
135      */
136     @Override
137     public void shutdown() {
138         stop();
139     }
140
141     /**
142      * {@inheritDoc}.
143      */
144     @Override
145     public boolean isAlive() {
146         return !servers.isEmpty();
147     }
148
149     /**
150      * {@inheritDoc}.
151      */
152     @Override
153     public String toString() {
154         final StringBuilder builder = new StringBuilder();
155         builder.append("PapRestServer [servers=");
156         builder.append(servers);
157         builder.append("]");
158         return builder.toString();
159     }
160
161 }