Added HTTPS and CADI/AAF Support for PDP-X
[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.JacksonHandler;
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 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             servers = HttpServletServer.factory.build(getServerProperties());
63             for (final HttpServletServer server : servers) {
64                 if (server.isAaf()) {
65                     server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
66                 }
67                 server.start();
68             }
69         } catch (final Exception exp) {
70             LOGGER.error("Failed to start xacml pdp 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(HTTP_SERVER_SERVICES, restServerParameters.getName());
84         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
85                 restServerParameters.getHost());
86         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
87                 Integer.toString(restServerParameters.getPort()));
88         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
89                 XacmlPdpRestController.class.getCanonicalName());
90         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
91         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
92         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
93                 restServerParameters.getUserName());
94         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
95                 restServerParameters.getPassword());
96         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
97                 String.valueOf(restServerParameters.isHttps()));
98         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
99                 String.valueOf(restServerParameters.isAaf()));
100         return props;
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
107     public boolean stop() {
108         for (final HttpServletServer server : servers) {
109             try {
110                 server.shutdown();
111             } catch (final Exception exp) {
112                 LOGGER.error("Failed to stop xacml pdp http server", exp);
113             }
114         }
115         return true;
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public void shutdown() {
123         stop();
124     }
125
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public boolean isAlive() {
131         return !servers.isEmpty();
132     }
133
134     /**
135      * {@inheritDoc}.
136      */
137     @Override
138     public String toString() {
139         final StringBuilder builder = new StringBuilder();
140         builder.append("XacmlPdpRestServer [servers=");
141         builder.append(servers);
142         builder.append("]");
143         return builder.toString();
144     }
145
146 }