Fix models due to sonar changes in common
[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.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
27
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;
35
36 /**
37  * Class to manage life cycle of DMaaP Simulator rest server.
38  */
39 public class DmaapSimRestServer implements Startable {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimRestServer.class);
42
43     private List<HttpServletServer> servers = new ArrayList<>();
44
45     private RestServerParameters restServerParameters;
46
47     /**
48      * Constructor for instantiating DmaapSimRestServer.
49      *
50      * @param restServerParameters the rest server parameters
51      */
52     public DmaapSimRestServer(final RestServerParameters restServerParameters) {
53         this.restServerParameters = restServerParameters;
54     }
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     public boolean start() {
61         try {
62             servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties());
63             for (final HttpServletServer server : servers) {
64                 server.start();
65             }
66         } catch (final Exception exp) {
67             LOGGER.error("Failed to start DMaaP simulator http server", exp);
68             return false;
69         }
70         return true;
71     }
72
73     /**
74      * Creates the server properties object using restServerParameters.
75      *
76      * @return the properties object
77      */
78     private Properties getServerProperties() {
79         final Properties props = new Properties();
80         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName());
81
82         final String svcpfx =
83                 PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName();
84
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());
94         return props;
95     }
96
97     /**
98      * {@inheritDoc}.
99      */
100     @Override
101     public boolean stop() {
102         for (final HttpServletServer server : servers) {
103             try {
104                 server.stop();
105             } catch (final Exception exp) {
106                 LOGGER.error("Failed to stop DMaaP simulator http server", exp);
107             }
108         }
109         return true;
110     }
111
112     /**
113      * {@inheritDoc}.
114      */
115     @Override
116     public void shutdown() {
117         stop();
118     }
119
120     /**
121      * {@inheritDoc}.
122      */
123     @Override
124     public boolean isAlive() {
125         return !servers.isEmpty();
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public String toString() {
133         final StringBuilder builder = new StringBuilder();
134         builder.append("DmaapSimRestServer [servers=");
135         builder.append(servers);
136         builder.append("]");
137         return builder.toString();
138     }
139
140 }