Fix xacml-pdp due to sonar changes in common
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2019 Nordix Foundation.
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.pdpx.main.rest;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
30 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
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 xacml pdp rest server.
37  *
38  */
39 public class XacmlPdpRestServer implements Startable {
40
41     private static final String SEPARATOR = ".";
42     private static final String HTTP_SERVER_SERVICES = "http.server.services";
43     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestServer.class);
44
45     private List<HttpServletServer> servers = new ArrayList<>();
46
47     private final RestServerParameters restServerParameters;
48
49     /**
50      * Constructor for instantiating XacmlPdpRestServer.
51      *
52      * @param restServerParameters the rest server parameters
53      */
54     public XacmlPdpRestServer(final RestServerParameters restServerParameters) {
55         this.restServerParameters = restServerParameters;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public boolean start() {
63         try {
64             LOGGER.info("Starting XacmlPdpRestServer...");
65
66             //
67             // Get the server properties
68             //
69             servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
70             //
71             // Start all the servers
72             //
73             for (final HttpServletServer server : servers) {
74                 if (server.isAaf()) {
75                     server.addFilterClass(null, XacmlPdpAafFilter.class.getName());
76                 }
77                 server.start();
78             }
79             LOGGER.info("servers are started");
80         } catch (final Exception exp) {
81             LOGGER.error("Failed to start xacml pdp http server", exp);
82             return false;
83         }
84         return true;
85     }
86
87     /**
88      * Creates the server properties object using restServerParameters.
89      *
90      * @return the properties object
91      */
92     private Properties getServerProperties() {
93         final Properties props = new Properties();
94         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
95         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
96                 restServerParameters.getHost());
97         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
98                 Integer.toString(restServerParameters.getPort()));
99         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
100                 XacmlPdpRestController.class.getName());
101         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
102         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
103         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
104                 restServerParameters.getUserName());
105         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
106                 restServerParameters.getPassword());
107         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
108                 String.valueOf(restServerParameters.isHttps()));
109         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
110                 String.valueOf(restServerParameters.isAaf()));
111         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
112                 GsonMessageBodyHandler.class.getName());
113         return props;
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
120     public boolean stop() {
121         for (final HttpServletServer server : servers) {
122             try {
123                 server.shutdown();
124             } catch (final Exception exp) {
125                 LOGGER.error("Failed to stop xacml pdp http server", exp);
126             }
127         }
128         return true;
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
135     public void shutdown() {
136         stop();
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public boolean isAlive() {
144         return !servers.isEmpty();
145     }
146
147     /**
148      * {@inheritDoc}.
149      */
150     @Override
151     public String toString() {
152         final StringBuilder builder = new StringBuilder();
153         builder.append("XacmlPdpRestServer [servers=");
154         builder.append(servers);
155         builder.append("]");
156         return builder.toString();
157     }
158
159 }