Add PDP Group Deploy and Delete REST API stubs
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PapRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
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.endpoints.properties.PolicyEndPointProperties;
31 import org.onap.policy.common.gson.GsonMessageBodyHandler;
32 import org.onap.policy.pap.main.parameters.RestServerParameters;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Class to manage life cycle of PAP rest server.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40  */
41 public class PapRestServer implements Startable {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(PapRestServer.class);
44
45     private List<HttpServletServer> servers = new ArrayList<>();
46
47     private RestServerParameters restServerParameters;
48
49     /**
50      * Constructor for instantiating PapRestServer.
51      *
52      * @param restServerParameters the rest server parameters
53      */
54     public PapRestServer(final RestServerParameters restServerParameters) {
55         this.restServerParameters = restServerParameters;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public boolean start() {
63         try {
64             servers = HttpServletServer.factory.build(getServerProperties());
65             for (final HttpServletServer server : servers) {
66                 if (server.isAaf()) {
67                     server.addFilterClass(null, PapAafFilter.class.getCanonicalName());
68                 }
69                 server.start();
70             }
71         } catch (final Exception exp) {
72             LOGGER.error("Failed to start pap http server", exp);
73             return false;
74         }
75         return true;
76     }
77
78     /**
79      * Creates the server properties object using restServerParameters.
80      *
81      * @return the properties object
82      */
83     private Properties getServerProperties() {
84         final Properties props = new Properties();
85         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
86
87         final String svcpfx =
88                         PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
89
90         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
91         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
92                         Integer.toString(restServerParameters.getPort()));
93         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
94                         String.join(",", HealthCheckRestControllerV1.class.getCanonicalName(),
95                                         StatisticsRestControllerV1.class.getCanonicalName(),
96                                         PdpGroupDeployControllerV1.class.getCanonicalName(),
97                                         PdpGroupDeleteControllerV1.class.getCanonicalName()));
98         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
99         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
100         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX,
101                         restServerParameters.getUserName());
102         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX,
103                         restServerParameters.getPassword());
104         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX,
105                         String.valueOf(restServerParameters.isHttps()));
106         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX,
107                         String.valueOf(restServerParameters.isAaf()));
108         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
109                         GsonMessageBodyHandler.class.getName());
110         return props;
111     }
112
113     /**
114      * {@inheritDoc}.
115      */
116     @Override
117     public boolean stop() {
118         for (final HttpServletServer server : servers) {
119             try {
120                 server.stop();
121             } catch (final Exception exp) {
122                 LOGGER.error("Failed to stop pap http server", exp);
123             }
124         }
125         return true;
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public void shutdown() {
133         stop();
134     }
135
136     /**
137      * {@inheritDoc}.
138      */
139     @Override
140     public boolean isAlive() {
141         return !servers.isEmpty();
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public String toString() {
149         final StringBuilder builder = new StringBuilder();
150         builder.append("PapRestServer [servers=");
151         builder.append(servers);
152         builder.append("]");
153         return builder.toString();
154     }
155
156 }