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