Flesh out DMaaP simulator
[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  *  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
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.models.sim.dmaap.rest;
23
24 import java.util.List;
25 import java.util.Properties;
26 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
27 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
28 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
29 import org.onap.policy.common.gson.GsonMessageBodyHandler;
30 import org.onap.policy.common.utils.services.ServiceManagerContainer;
31 import org.onap.policy.models.sim.dmaap.parameters.RestServerParameters;
32
33 /**
34  * Class to manage life cycle of DMaaP Simulator rest server.
35  */
36 public class DmaapSimRestServer extends ServiceManagerContainer {
37
38     private final List<HttpServletServer> servers;
39
40     /**
41      * Constructor for instantiating DmaapSimRestServer.
42      *
43      * @param restServerParameters the rest server parameters
44      */
45     public DmaapSimRestServer(final RestServerParameters restServerParameters) {
46         this.servers = HttpServletServerFactoryInstance.getServerFactory()
47                         .build(getServerProperties(restServerParameters));
48
49         for (HttpServletServer server : this.servers) {
50             addAction("REST " + server.getName(), server::start, server::stop);
51         }
52     }
53
54     /**
55      * Creates a set of properties, suitable for building a REST server, from the
56      * parameters.
57      *
58      * @param restServerParameters parameters from which to build the properties
59      * @return a set of properties representing the given parameters
60      */
61     public static Properties getServerProperties(RestServerParameters restServerParameters) {
62         final Properties props = new Properties();
63         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
64
65         final String svcpfx =
66                         PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
67
68         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost());
69         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX,
70                         Integer.toString(restServerParameters.getPort()));
71         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX,
72                         DmaapSimRestControllerV1.class.getName());
73         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false");
74         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
75
76         props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
77                         String.join(",", CambriaMessageBodyHandler.class.getName(),
78                                         GsonMessageBodyHandler.class.getName(),
79                                         TextMessageBodyHandler.class.getName()));
80         return props;
81     }
82 }