Xacml PDP Register/Unregister Changes
[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             //
72             // Update statistics manager on the policy types
73             //
74             XacmlPdpStatisticsManager.setTotalPolicyTypesCount(XacmlPdpApplicationManager.getPolicyTypeCount());
75
76             //
77             // Get the server properties
78             //
79             servers = HttpServletServer.factory.build(getServerProperties());
80             //
81             // Start all the servers
82             //
83             for (final HttpServletServer server : servers) {
84                 if (server.isAaf()) {
85                     server.addFilterClass(null, XacmlPdpAafFilter.class.getCanonicalName());
86                 }
87                 server.start();
88             }
89         } catch (final Exception exp) {
90             LOGGER.error("Failed to start xacml pdp http server", exp);
91             return false;
92         }
93         return true;
94     }
95
96     /**
97      * Creates the server properties object using restServerParameters.
98      *
99      * @return the properties object
100      */
101     private Properties getServerProperties() {
102         final Properties props = new Properties();
103         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
104         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
105                 restServerParameters.getHost());
106         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
107                 Integer.toString(restServerParameters.getPort()));
108         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
109                 XacmlPdpRestController.class.getCanonicalName());
110         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
111         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
112         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
113                 restServerParameters.getUserName());
114         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
115                 restServerParameters.getPassword());
116         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
117                 String.valueOf(restServerParameters.isHttps()));
118         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".aaf",
119                 String.valueOf(restServerParameters.isAaf()));
120         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".serialization.provider",
121                 GsonMessageBodyHandler.class.getName());
122         return props;
123     }
124
125     /**
126      * {@inheritDoc}.
127      */
128     @Override
129     public boolean stop() {
130         for (final HttpServletServer server : servers) {
131             try {
132                 server.shutdown();
133             } catch (final Exception exp) {
134                 LOGGER.error("Failed to stop xacml pdp http server", exp);
135             }
136         }
137         return true;
138     }
139
140     /**
141      * {@inheritDoc}.
142      */
143     @Override
144     public void shutdown() {
145         stop();
146     }
147
148     /**
149      * {@inheritDoc}.
150      */
151     @Override
152     public boolean isAlive() {
153         return !servers.isEmpty();
154     }
155
156     /**
157      * {@inheritDoc}.
158      */
159     @Override
160     public String toString() {
161         final StringBuilder builder = new StringBuilder();
162         builder.append("XacmlPdpRestServer [servers=");
163         builder.append(servers);
164         builder.append("]");
165         return builder.toString();
166     }
167
168 }