66b9006a9f4ca5b48e11a20f9850a70335d63f7c
[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.util.Map;
24 import java.util.Properties;
25 import lombok.Getter;
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;
41
42 /**
43  * Services used by the ControlLoopEventManager.
44  */
45 @Getter
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";
50
51     public final ActorService actorService = new ActorService();
52
53     // assume we're using a stub until proven otherwise
54     public final OperationHistoryDataManager dataManager;
55
56     /**
57      * Constructs the object. Configures and starts the actor service. Initializes
58      * {@link #dataManager}, to a "real" data manager, if guards are enabled.
59      *
60      * @param configFileName configuration file name
61      */
62     public EventManagerServices(String configFileName) {
63         // configure and start actor services
64         Properties props = startActorService(configFileName);
65
66         if (isGuardEnabled()) {
67             // guards are enabled - use a real data manager
68             dataManager = makeDataManager(props);
69         } else {
70             // guards are disabled - use a stub data manager
71             dataManager = new OperationHistoryDataManagerStub();
72         }
73     }
74
75     /**
76      * Configures and starts the actor service.
77      *
78      * @param configFileName configuration file name
79      * @return the properties that were loaded from the configuration file
80      */
81     public Properties startActorService(String configFileName) {
82         try {
83             Properties props = SystemPersistenceConstants.getManager().getProperties(configFileName);
84
85             Map<String, Object> parameters = PropertyObjectUtils.toObject(props, ACTOR_SERVICE_PROPERTIES);
86             PropertyObjectUtils.compressLists(parameters);
87
88             actorService.configure(parameters);
89             actorService.start();
90
91             return props;
92
93         } catch (RuntimeException e) {
94             logger.error("cannot configure/start actor service");
95             throw new IllegalStateException(e);
96         }
97     }
98
99     /**
100      * Determines if guards are enabled.
101      *
102      * @return {@code true} if guards are enabled, {@code false} otherwise
103      */
104     public boolean isGuardEnabled() {
105         try {
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");
110                 return false;
111             }
112
113             GuardConfig config = (GuardConfig) guard.getCurrentConfig();
114             if (config.isDisabled()) {
115                 logger.warn("guard disabled");
116                 return false;
117             }
118
119             if (!guard.isAlive()) {
120                 logger.warn("guard actor is not running");
121                 return false;
122             }
123
124             return true;
125
126         } catch (RuntimeException e) {
127             logger.warn("cannot check 'disabled' property in GUARD actor - assuming disabled", e);
128             return false;
129         }
130     }
131
132     /**
133      * Makes and starts the data manager.
134      *
135      * @param props properties with which to configure the data manager
136      * @return a new data manager
137      */
138     public OperationHistoryDataManagerImpl makeDataManager(Properties props) {
139         try {
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());
146             }
147
148             OperationHistoryDataManagerImpl mgr = new OperationHistoryDataManagerImpl(params);
149             mgr.start();
150
151             return mgr;
152
153         } catch (RuntimeException e) {
154             logger.error("cannot start operation history data manager");
155             actorService.stop();
156             throw e;
157         }
158     }
159 }