add rest serve and distribution framework
[multicloud/framework.git] / artifactbroker / main / src / main / java / org / onap / policy / distribution / main / rest / DistributionRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. 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.distribution.main.rest;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26
27 import org.onap.policy.common.capabilities.Startable;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.distribution.main.parameters.RestServerParameters;
32
33 /**
34  * Class to manage life cycle of distribution rest server.
35  *
36  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
37  */
38 public class DistributionRestServer implements Startable {
39
40     private static final String SEPARATOR = ".";
41     private static final String HTTP_SERVER_SERVICES = "http.server.services";
42     private static final Logger LOGGER = FlexLogger.getLogger(DistributionRestServer.class);
43
44     private List<HttpServletServer> servers = new ArrayList<>();
45
46     private RestServerParameters restServerParameters;
47
48     /**
49      * Constructor for instantiating DistributionRestServer.
50      *
51      * @param restServerParameters the rest server parameters
52      */
53     public DistributionRestServer(final RestServerParameters restServerParameters) {
54         this.restServerParameters = restServerParameters;
55     }
56
57     /**
58      * {@inheritDoc}.
59      */
60     @Override
61     public boolean start() {
62         try {
63             servers = HttpServletServer.factory.build(getServerProperties());
64             for (final HttpServletServer server : servers) {
65                 server.start();
66             }
67         } catch (final Exception exp) {
68             LOGGER.error("Failed to start distribution http server", exp);
69             return false;
70         }
71         return true;
72     }
73
74     /**
75      * Creates the server properties object using restServerParameters.
76      *
77      * @return the properties object
78      */
79     private Properties getServerProperties() {
80         final Properties props = new Properties();
81         props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName());
82         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host",
83                 restServerParameters.getHost());
84         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port",
85                 Integer.toString(restServerParameters.getPort()));
86         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses",
87                 DistributionRestController.class.getCanonicalName());
88         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false");
89         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true");
90         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName",
91                 restServerParameters.getUserName());
92         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password",
93                 restServerParameters.getPassword());
94         props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".https",
95                 String.valueOf(restServerParameters.isHttps()));
96         return props;
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
103     public boolean stop() {
104         for (final HttpServletServer server : servers) {
105             try {
106                 server.stop();
107             } catch (final Exception exp) {
108                 LOGGER.error("Failed to stop distribution http server", exp);
109             }
110         }
111         return true;
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public void shutdown() {
119         stop();
120     }
121
122     /**
123      * {@inheritDoc}.
124      */
125     @Override
126     public boolean isAlive() {
127         return !servers.isEmpty();
128     }
129
130     /**
131      * {@inheritDoc}.
132      */
133     @Override
134     public String toString() {
135         final StringBuilder builder = new StringBuilder();
136         builder.append("DistributionRestServer [servers=");
137         builder.append(servers);
138         builder.append("]");
139         return builder.toString();
140     }
141
142 }