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