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