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