2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.dmaap.rest;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
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.http.server.HttpServletServerFactoryInstance;
31 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
32 import org.onap.policy.models.sim.dmaap.parameters.RestServerParameters;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Class to manage life cycle of DMaaP Simulator rest server.
39 public class DmaapSimRestServer implements Startable {
41 private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimRestServer.class);
43 private List<HttpServletServer> servers = new ArrayList<>();
45 private RestServerParameters restServerParameters;
48 * Constructor for instantiating DmaapSimRestServer.
50 * @param restServerParameters the rest server parameters
52 public DmaapSimRestServer(final RestServerParameters restServerParameters) {
53 this.restServerParameters = restServerParameters;
60 public boolean start() {
62 servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
63 for (final HttpServletServer server : servers) {
66 } catch (final Exception exp) {
67 LOGGER.error("Failed to start DMaaP simulator http server", exp);
74 * Creates the server properties object using restServerParameters.
76 * @return the properties object
78 private Properties getServerProperties() {
79 final Properties props = new Properties();
80 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
83 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
85 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
86 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
87 Integer.toString(restServerParameters.getPort()));
88 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
89 String.join(",", DmaapSimRestControllerV1.class.getName()));
90 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
91 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true");
92 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
93 CambriaMessageBodyHandler.class.getName() + "," + JsonMessageBodyHandler.class.getName());
101 public boolean stop() {
102 for (final HttpServletServer server : servers) {
105 } catch (final Exception exp) {
106 LOGGER.error("Failed to stop DMaaP simulator http server", exp);
116 public void shutdown() {
124 public boolean isAlive() {
125 return !servers.isEmpty();
132 public String toString() {
133 final StringBuilder builder = new StringBuilder();
134 builder.append("DmaapSimRestServer [servers=");
135 builder.append(servers);
137 return builder.toString();