6853cc9dce9b3611a278f0c168ae475b1d7359ac
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / rest / DmaapSimRestServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.sim.dmaap.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.endpoints.properties.PolicyEndPointProperties;
30 import org.onap.policy.models.sim.dmaap.parameters.RestServerParameters;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Class to manage life cycle of DMaaP Simulator rest server.
36  */
37 public class DmaapSimRestServer implements Startable {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimRestServer.class);
40
41     private List<HttpServletServer> servers = new ArrayList<>();
42
43     private RestServerParameters restServerParameters;
44
45     /**
46      * Constructor for instantiating DmaapSimRestServer.
47      *
48      * @param restServerParameters the rest server parameters
49      */
50     public DmaapSimRestServer(final RestServerParameters restServerParameters) {
51         this.restServerParameters = restServerParameters;
52     }
53
54     /**
55      * {@inheritDoc}.
56      */
57     @Override
58     public boolean start() {
59         try {
60             servers = HttpServletServer.factory.build(getServerProperties());
61             for (final HttpServletServer server : servers) {
62                 server.start();
63             }
64         } catch (final Exception exp) {
65             LOGGER.error("Failed to start DMaaP simulator http server", exp);
66             return false;
67         }
68         return true;
69     }
70
71     /**
72      * Creates the server properties object using restServerParameters.
73      *
74      * @return the properties object
75      */
76     private Properties getServerProperties() {
77         final Properties props = new Properties();
78         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
79
80         final String svcpfx =
81                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
82
83         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
84         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
85                 Integer.toString(restServerParameters.getPort()));
86         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
87                 String.join(",", DmaapSimRestControllerV1.class.getName()));
88         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
89         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
90         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
91                 CambriaMessageBodyHandler.class.getName() + "," + JsonMessageBodyHandler.class.getName());
92         return props;
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public boolean stop() {
100         for (final HttpServletServer server : servers) {
101             try {
102                 server.stop();
103             } catch (final Exception exp) {
104                 LOGGER.error("Failed to stop DMaaP simulator http server", exp);
105             }
106         }
107         return true;
108     }
109
110     /**
111      * {@inheritDoc}.
112      */
113     @Override
114     public void shutdown() {
115         stop();
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public boolean isAlive() {
123         return !servers.isEmpty();
124     }
125
126     /**
127      * {@inheritDoc}.
128      */
129     @Override
130     public String toString() {
131         final StringBuilder builder = new StringBuilder();
132         builder.append("DmaapSimRestServer [servers=");
133         builder.append(servers);
134         builder.append("]");
135         return builder.toString();
136     }
137
138 }