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;
24 import java.util.Properties;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.common.utils.properties.PropertyObjectUtils;
28 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
29 import org.onap.policy.controlloop.actor.guard.DecisionOperator;
30 import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider;
31 import org.onap.policy.controlloop.actor.guard.GuardConfig;
32 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
33 import org.onap.policy.controlloop.actorserviceprovider.Util;
34 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
35 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerImpl;
36 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams;
37 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
38 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Services used by the ControlLoopEventManager.
46 public class EventManagerServices {
47 public static final Logger logger = LoggerFactory.getLogger(EventManagerServices.class);
48 public static final String ACTOR_SERVICE_PROPERTIES = "actor.service";
49 public static final String DATA_MANAGER_PROPERTIES = "operation.history";
51 public final ActorService actorService = new ActorService();
53 // assume we're using a stub until proven otherwise
54 public final OperationHistoryDataManager dataManager;
57 * Constructs the object. Configures and starts the actor service. Initializes
58 * {@link #dataManager}, to a "real" data manager, if guards are enabled.
60 * @param configFileName configuration file name
62 public EventManagerServices(String configFileName) {
63 // configure and start actor services
64 Properties props = startActorService(configFileName);
66 if (isGuardEnabled()) {
67 // guards are enabled - use a real data manager
68 dataManager = makeDataManager(props);
70 // guards are disabled - use a stub data manager
71 dataManager = new OperationHistoryDataManagerStub();
76 * Configures and starts the actor service.
78 * @param configFileName configuration file name
79 * @return the properties that were loaded from the configuration file
81 public Properties startActorService(String configFileName) {
83 Properties props = SystemPersistenceConstants.getManager().getProperties(configFileName);
85 Map<String, Object> parameters = PropertyObjectUtils.toObject(props, ACTOR_SERVICE_PROPERTIES);
86 PropertyObjectUtils.compressLists(parameters);
88 actorService.configure(parameters);
93 } catch (RuntimeException e) {
94 logger.error("cannot configure/start actor service");
95 throw new IllegalStateException(e);
100 * Determines if guards are enabled.
102 * @return {@code true} if guards are enabled, {@code false} otherwise
104 public boolean isGuardEnabled() {
106 DecisionOperator guard = (DecisionOperator) getActorService().getActor(GuardActorServiceProvider.NAME)
107 .getOperator(DecisionOperation.NAME);
108 if (!guard.isConfigured()) {
109 logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled");
113 GuardConfig config = (GuardConfig) guard.getCurrentConfig();
114 if (config.isDisabled()) {
115 logger.warn("guard disabled");
119 if (!guard.isAlive()) {
120 logger.warn("guard actor is not running");
126 } catch (RuntimeException e) {
127 logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled", e);
133 * Makes and starts the data manager.
135 * @param props properties with which to configure the data manager
136 * @return a new data manager
138 public OperationHistoryDataManagerImpl makeDataManager(Properties props) {
140 Map<String, Object> parameters = PropertyObjectUtils.toObject(props, DATA_MANAGER_PROPERTIES);
141 OperationHistoryDataManagerParams params = Util.translate(DATA_MANAGER_PROPERTIES, parameters,
142 OperationHistoryDataManagerParams.class);
143 ValidationResult result = params.validate(DATA_MANAGER_PROPERTIES);
144 if (!result.isValid()) {
145 throw new IllegalArgumentException("invalid data manager properties:\n" + result.getResult());
148 OperationHistoryDataManagerImpl mgr = new OperationHistoryDataManagerImpl(params);
153 } catch (RuntimeException e) {
154 logger.error("cannot start operation history data manager");