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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.dmaap.rest;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
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;
35 * Class to manage life cycle of DMaaP Simulator rest server.
37 public class DmaapSimRestServer implements Startable {
39 private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimRestServer.class);
41 private List<HttpServletServer> servers = new ArrayList<>();
43 private RestServerParameters restServerParameters;
46 * Constructor for instantiating DmaapSimRestServer.
48 * @param restServerParameters the rest server parameters
50 public DmaapSimRestServer(final RestServerParameters restServerParameters) {
51 this.restServerParameters = restServerParameters;
58 public boolean start() {
60 servers = HttpServletServer.factory.build(getServerProperties());
61 for (final HttpServletServer server : servers) {
64 } catch (final Exception exp) {
65 LOGGER.error("Failed to start DMaaP simulator http server", exp);
72 * Creates the server properties object using restServerParameters.
74 * @return the properties object
76 private Properties getServerProperties() {
77 final Properties props = new Properties();
78 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
81 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
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());
99 public boolean stop() {
100 for (final HttpServletServer server : servers) {
103 } catch (final Exception exp) {
104 LOGGER.error("Failed to stop DMaaP simulator http server", exp);
114 public void shutdown() {
122 public boolean isAlive() {
123 return !servers.isEmpty();
130 public String toString() {
131 final StringBuilder builder = new StringBuilder();
132 builder.append("DmaapSimRestServer [servers=");
133 builder.append(servers);
135 return builder.toString();