4a325a447f8510a8ca5b746f25bd55adf68416a6
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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      * Constructs the object.
42      */
43     public Simulators() {
44         super();
45     }
46
47     /**
48      * Invokes the given functions to start the simulators. Destroys <i>all</i> of the
49      * simulators if any fail to start.
50      *
51      * @param builders functions to invoke to build the simulators
52      */
53     public void start(SimulatorBuilder... builders) {
54         try {
55             for (SimulatorBuilder builder : builders) {
56                 servers.add(builder.build());
57             }
58         } catch (InterruptedException e) {
59             logger.warn("interrupted building the simulators");
60             destroy();
61             Thread.currentThread().interrupt();
62             throw new SimulatorException(e);
63         }
64     }
65
66     /**
67      * Stops all of the simulators.
68      */
69     public void destroy() {
70         for (HttpServletServer server : servers) {
71             try {
72                 server.shutdown();
73             } catch (RuntimeException e) {
74                 logger.warn("error stopping simulator {}", server.getName(), e);
75             }
76         }
77
78         servers.clear();
79     }
80
81     @FunctionalInterface
82     public static interface SimulatorBuilder {
83         public HttpServletServer build() throws InterruptedException;
84     }
85 }