2 * ============LICENSE_START=======================================================
3 * feature-state-management
4 * ================================================================================
5 * Copyright (C) 2017-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.drools.statemanagement;
23 import java.io.IOException;
24 import java.util.Properties;
25 import org.onap.policy.common.im.AllSeemsWellException;
26 import org.onap.policy.common.im.StateChangeNotifier;
27 import org.onap.policy.common.im.StateManagement;
28 import org.onap.policy.drools.core.PolicySessionFeatureApi;
29 import org.onap.policy.drools.features.PolicyEngineFeatureApi;
30 import org.onap.policy.drools.utils.PropertyUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * If this feature is supported, there is a single instance of it.
36 * It adds persistence to Drools sessions, but it is also intertwined with
37 * active/standby state management and IntegrityMonitor. For now, they are
38 * all treated as a single feature, but it would be nice to separate them.
40 * <p>The bulk of the code here was once in other classes, such as
41 * 'PolicyContainer' and 'Main'. It was moved here as part of making this
42 * a separate optional feature.
45 public class StateManagementFeature implements StateManagementFeatureApi,
46 PolicySessionFeatureApi, PolicyEngineFeatureApi {
47 // get an instance of logger
48 private static final Logger logger =
49 LoggerFactory.getLogger(StateManagementFeature.class);
51 private DroolsPdpIntegrityMonitor droolsPdpIntegrityMonitor = null;
52 private StateManagement stateManagement = null;
54 public StateManagementFeature() {
55 logger.debug("StateManagementFeature() constructor");
59 public void globalInit(String[] args, String configDir) {
60 // Initialization code associated with 'PolicyContainer'
61 logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
64 droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.init(configDir);
65 } catch (Exception e) {
66 logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
67 logger.error("DroolsPDPIntegrityMonitor.init()", e);
70 initializeProperties(configDir);
72 //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
74 droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.getInstance();
75 stateManagement = droolsPdpIntegrityMonitor.getStateManager();
77 if (stateManagement == null) {
78 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
81 logger.debug("StateManagementFeature.globalInit(): "
82 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
84 } catch (Exception e1) {
85 logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
86 + " initialization failed with exception:", e1);
87 logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
88 + "to get DroolsPDPIntegrityMonitor instance:", e1);
96 public void addObserver(StateChangeNotifier stateChangeObserver) {
97 logger.debug("StateManagementFeature.addObserver() entry\n"
98 + "StateManagementFeature.addObserver(): "
99 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
101 stateManagement.addObserver(stateChangeObserver);
103 logger.debug("StateManagementFeature.addObserver() exit");
110 public String getAdminState() {
111 return stateManagement.getAdminState();
118 public String getOpState() {
119 return stateManagement.getOpState();
126 public String getAvailStatus() {
127 return stateManagement.getAvailStatus();
134 public String getStandbyStatus() {
135 return stateManagement.getStandbyStatus();
142 public String getStandbyStatus(String resourceName) {
143 return stateManagement.getStandbyStatus(resourceName);
150 public void disableFailed(String resourceName) throws Exception {
151 stateManagement.disableFailed(resourceName);
159 public void disableFailed() throws Exception {
160 stateManagement.disableFailed();
167 public void promote() throws Exception {
168 stateManagement.promote();
175 public void demote() throws Exception {
176 stateManagement.demote();
183 public String getResourceName() {
184 return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
190 * @return true if locked or false if failed
193 public boolean lock() {
195 stateManagement.lock();
196 } catch (Exception e) {
197 logger.error("StateManagementFeature.lock() failed with exception", e);
206 * @throws Exception exception
209 public boolean unlock() {
211 stateManagement.unlock();
212 } catch (Exception e) {
213 logger.error("StateManagementFeature.unlock() failed with exception", e);
222 * @throws Exception exception
225 public boolean isLocked() {
226 return StateManagement.LOCKED.equals(stateManagement.getAdminState());
230 public int getSequenceNumber() {
231 return StateManagementFeatureApiConstants.SEQ_NUM;
235 * Read in the properties and initialize the StateManagementProperties.
237 private static void initializeProperties(String configDir) {
238 //Get the state management properties
241 PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
242 StateManagementProperties.initProperties(props);
243 logger.info("initializeProperties: resourceName= {}",
244 StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
245 } catch (IOException e1) {
246 logger.error("initializeProperties", e1);
251 public void allSeemsWell(String key, Boolean asw, String msg)
252 throws AllSeemsWellException {
254 droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg);