2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import java.util.Properties;
29 import org.onap.policy.common.parameters.ValidationResult;
30 import org.onap.policy.common.utils.resources.ResourceUtils;
31 import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider;
32 import org.onap.policy.controlloop.actor.guard.GuardConfig;
33 import org.onap.policy.controlloop.actor.guard.GuardOperation;
34 import org.onap.policy.controlloop.actor.guard.GuardOperator;
35 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
36 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
38 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerImpl;
39 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams;
40 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
41 import org.onap.policy.controlloop.utils.ControlLoopUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Services used by the ControlLoopEventManager.
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";
54 public final ActorService actorService = new ActorService();
56 // assume we're using a stub until proven otherwise
57 public final OperationHistoryDataManager dataManager;
60 * Constructs the object. Configures and starts the actor service. Initializes
61 * {@link #dataManager}, to a "real" data manager, if guards are enabled.
63 * @param configFileName configuration file name
65 public EventManagerServices(String configFileName) {
66 // configure and start actor services
67 Properties props = startActorService(configFileName);
69 if (isGuardEnabled()) {
70 // guards are enabled - use a real data manager
71 dataManager = makeDataManager(props);
73 // guards are disabled - use a stub data manager
74 dataManager = new OperationHistoryDataManagerStub();
79 * Configures and starts the actor service.
81 * @param configFileName configuration file name
82 * @return the properties that were loaded from the configuration file
84 public Properties startActorService(String configFileName) {
85 try (InputStream inpstr = openConfigFile(configFileName)) {
86 Properties props = new Properties();
89 Map<String, Object> parameters = ControlLoopUtils.toObject(props, ACTOR_SERVICE_PROPERTIES);
90 ControlLoopUtils.compressLists(parameters);
92 actorService.configure(parameters);
97 } catch (RuntimeException | IOException e) {
98 logger.error("cannot configure/start actor service");
99 throw new IllegalStateException(e);
104 * Opens the config file.
106 * @param configFileName configuration file name
107 * @return the file's input stream
108 * @throws FileNotFoundException if the file cannot be found
110 private InputStream openConfigFile(String configFileName) throws FileNotFoundException {
111 InputStream inpstr = ResourceUtils.getResourceAsStream(configFileName);
112 if (inpstr == null) {
113 throw new FileNotFoundException(configFileName);
120 * Determines if guards are enabled.
122 * @return {@code true} if guards are enabled, {@code false} otherwise
124 public boolean isGuardEnabled() {
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");
133 GuardConfig config = (GuardConfig) guard.getCurrentConfig();
134 if (config.isDisabled()) {
135 logger.warn("guard disabled");
139 if (!guard.isAlive()) {
140 logger.warn("guard actor is not running");
146 } catch (RuntimeException e) {
147 logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled", e);
153 * Makes and starts the data manager.
155 * @param props properties with which to configure the data manager
156 * @return a new data manager
158 public OperationHistoryDataManagerImpl makeDataManager(Properties props) {
160 Map<String, Object> parameters = ControlLoopUtils.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());
168 OperationHistoryDataManagerImpl mgr = new OperationHistoryDataManagerImpl(params);
173 } catch (RuntimeException e) {
174 logger.error("cannot start operation history data manager");