Tie XACML REST Decision
[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.nio.file.Paths;
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.gson.GsonMessageBodyHandler;
31 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
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 RestServerParameters restServerParameters;
48     private String applicationPath;
49
50     /**
51      * Constructor for instantiating XacmlPdpRestServer.
52      *
53      * @param restServerParameters the rest server parameters
54      */
55     public XacmlPdpRestServer(final RestServerParameters restServerParameters, final String applicationPath) {
56         this.restServerParameters = restServerParameters;
57         this.applicationPath = applicationPath;
58     }
59
60     /**
61      * {@inheritDoc}.
62      */
63     @Override
64     public boolean start() {
65         try {
66             //
67             // Initialize the applications - SEND PROPERTIES
68             //
69             XacmlPdpApplicationManager.initializeApplications(Paths.get(applicationPath));
70             //
71             // Update statistics manager on the policy types
72             //
73             XacmlPdpStatisticsManager.setTotalPolicyTypesCount(XacmlPdpApplicationManager.getPolicyTypeCount());
74             //
75             // Get the server properties
76             //
77             servers = HttpServletServer.factory.build(getServerProperties());
78             //
79             // Start all the servers
80             //
81             for (final HttpServletServer server : servers) {
82                 if (server.isAaf()) {
83                     server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
84                 }
85                 server.start();
86             }
87         } catch (final Exception exp) {
88             LOGGER.error("Failed to start xacml pdp http server", exp);
89             return false;
90         }
91         return true;
92     }
93
94     /**
95      * Creates the server properties object using restServerParameters.
96      *
97      * @return the properties object
98      */
99     private Properties getServerProperties() {
100         final Properties props = new Properties();
101         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
102         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
103                 restServerParameters.getHost());
104         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
105                 Integer.toString(restServerParameters.getPort()));
106         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
107                 XacmlPdpRestController.class.getCanonicalName());
108         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
109         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
110         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
111                 restServerParameters.getUserName());
112         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
113                 restServerParameters.getPassword());
114         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
115                 String.valueOf(restServerParameters.isHttps()));
116         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
117                 String.valueOf(restServerParameters.isAaf()));
118         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
119                 GsonMessageBodyHandler.class.getName());
120         return props;
121     }
122
123     /**
124      * {@inheritDoc}.
125      */
126     @Override
127     public boolean stop() {
128         for (final HttpServletServer server : servers) {
129             try {
130                 server.shutdown();
131             } catch (final Exception exp) {
132                 LOGGER.error("Failed to stop xacml pdp http server", exp);
133             }
134         }
135         return true;
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public void shutdown() {
143         stop();
144     }
145
146     /**
147      * {@inheritDoc}.
148      */
149     @Override
150     public boolean isAlive() {
151         return !servers.isEmpty();
152     }
153
154     /**
155      * {@inheritDoc}.
156      */
157     @Override
158     public String toString() {
159         final StringBuilder builder = new StringBuilder();
160         builder.append("XacmlPdpRestServer [servers=");
161         builder.append(servers);
162         builder.append("]");
163         return builder.toString();
164     }
165
166 }