3fbedcb1f2fc0effe4668bc5b0b1292913b8323c
[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.eventmanager;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Map;
27 import java.util.Properties;
28 import lombok.Getter;
29 import org.onap.policy.common.parameters.ValidationResult;
30 import org.onap.policy.common.utils.properties.PropertyObjectUtils;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider;
33 import org.onap.policy.controlloop.actor.guard.GuardConfig;
34 import org.onap.policy.controlloop.actor.guard.GuardOperation;
35 import org.onap.policy.controlloop.actor.guard.GuardOperator;
36 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
37 import org.onap.policy.controlloop.actorserviceprovider.Util;
38 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
39 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerImpl;
40 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams;
41 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Services used by the ControlLoopEventManager.
47  */
48 @Getter
49 public class EventManagerServices {
50     public static final Logger logger = LoggerFactory.getLogger(EventManagerServices.class);
51     public static final String ACTOR_SERVICE_PROPERTIES = "actor.service";
52     public static final String DATA_MANAGER_PROPERTIES = "operation.history";
53
54     public final ActorService actorService = new ActorService();
55
56     // assume we're using a stub until proven otherwise
57     public final OperationHistoryDataManager dataManager;
58
59     /**
60      * Constructs the object. Configures and starts the actor service. Initializes
61      * {@link #dataManager}, to a "real" data manager, if guards are enabled.
62      *
63      * @param configFileName configuration file name
64      */
65     public EventManagerServices(String configFileName) {
66         // configure and start actor services
67         Properties props = startActorService(configFileName);
68
69         if (isGuardEnabled()) {
70             // guards are enabled - use a real data manager
71             dataManager = makeDataManager(props);
72         } else {
73             // guards are disabled - use a stub data manager
74             dataManager = new OperationHistoryDataManagerStub();
75         }
76     }
77
78     /**
79      * Configures and starts the actor service.
80      *
81      * @param configFileName configuration file name
82      * @return the properties that were loaded from the configuration file
83      */
84     public Properties startActorService(String configFileName) {
85         try (InputStream inpstr = openConfigFile(configFileName)) {
86             Properties props = new Properties();
87             props.load(inpstr);
88
89             Map<String, Object> parameters = PropertyObjectUtils.toObject(props, ACTOR_SERVICE_PROPERTIES);
90             PropertyObjectUtils.compressLists(parameters);
91
92             actorService.configure(parameters);
93             actorService.start();
94
95             return props;
96
97         } catch (RuntimeException | IOException e) {
98             logger.error("cannot configure/start actor service");
99             throw new IllegalStateException(e);
100         }
101     }
102
103     /**
104      * Opens the config file.
105      *
106      * @param configFileName configuration file name
107      * @return the file's input stream
108      * @throws FileNotFoundException if the file cannot be found
109      */
110     private InputStream openConfigFile(String configFileName) throws FileNotFoundException {
111         InputStream inpstr = ResourceUtils.getResourceAsStream(configFileName);
112         if (inpstr == null) {
113             throw new FileNotFoundException(configFileName);
114         }
115
116         return inpstr;
117     }
118
119     /**
120      * Determines if guards are enabled.
121      *
122      * @return {@code true} if guards are enabled, {@code false} otherwise
123      */
124     public boolean isGuardEnabled() {
125         try {
126             GuardOperator guard = (GuardOperator) getActorService().getActor(GuardActorServiceProvider.NAME)
127                             .getOperator(GuardOperation.NAME);
128             if (!guard.isConfigured()) {
129                 logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled");
130                 return false;
131             }
132
133             GuardConfig config = (GuardConfig) guard.getCurrentConfig();
134             if (config.isDisabled()) {
135                 logger.warn("guard disabled");
136                 return false;
137             }
138
139             if (!guard.isAlive()) {
140                 logger.warn("guard actor is not running");
141                 return false;
142             }
143
144             return true;
145
146         } catch (RuntimeException e) {
147             logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled", e);
148             return false;
149         }
150     }
151
152     /**
153      * Makes and starts the data manager.
154      *
155      * @param props properties with which to configure the data manager
156      * @return a new data manager
157      */
158     public OperationHistoryDataManagerImpl makeDataManager(Properties props) {
159         try {
160             Map<String, Object> parameters = PropertyObjectUtils.toObject(props, DATA_MANAGER_PROPERTIES);
161             OperationHistoryDataManagerParams params = Util.translate(DATA_MANAGER_PROPERTIES, parameters,
162                             OperationHistoryDataManagerParams.class);
163             ValidationResult result = params.validate(DATA_MANAGER_PROPERTIES);
164             if (!result.isValid()) {
165                 throw new IllegalArgumentException("invalid data manager properties:\n" + result.getResult());
166             }
167
168             OperationHistoryDataManagerImpl mgr = new OperationHistoryDataManagerImpl(params);
169             mgr.start();
170
171             return mgr;
172
173         } catch (RuntimeException e) {
174             logger.error("cannot start operation history data manager");
175             actorService.stop();
176             throw e;
177         }
178     }
179 }