Remove deprecated DMAAP dependency
[policy/drools-applications.git] / controlloop / common / rules-test / src / main / java / org / onap / policy / controlloop / common / rules / test / Simulators.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.common.rules.test;
23
24 import java.util.LinkedList;
25 import java.util.List;
26 import lombok.AccessLevel;
27 import lombok.Getter;
28 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Simulator container.
34  */
35 public class Simulators {
36     private static final Logger logger = LoggerFactory.getLogger(Simulators.class);
37
38     @Getter(AccessLevel.PROTECTED)
39     private final List<HttpServletServer> servers = new LinkedList<>();
40
41     /**
42      * Invokes the given functions to start the simulators. Destroys <i>all</i> of the
43      * simulators if any fail to start.
44      *
45      * @param builders functions to invoke to build the simulators
46      */
47     public void start(SimulatorBuilder... builders) {
48         try {
49             for (SimulatorBuilder builder : builders) {
50                 servers.add(builder.build());
51             }
52         } catch (InterruptedException e) {
53             logger.warn("interrupted building the simulators");
54             destroy();
55             Thread.currentThread().interrupt();
56             throw new SimulatorException(e);
57         }
58     }
59
60     /**
61      * Stops all the simulators.
62      */
63     public void destroy() {
64         for (HttpServletServer server : servers) {
65             try {
66                 server.shutdown();
67             } catch (RuntimeException e) {
68                 logger.warn("error stopping simulator {}", server.getName(), e);
69             }
70         }
71
72         servers.clear();
73     }
74
75     @FunctionalInterface
76     public interface SimulatorBuilder {
77         HttpServletServer build() throws InterruptedException;
78     }
79 }