2 * ============LICENSE_START=======================================================
3 * feature-state-management
4 * ================================================================================
5 * Copyright (C) 2017 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.Observer;
25 import java.util.Properties;
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 * 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
48 // get an instance of logger
49 private static final Logger logger =
50 LoggerFactory.getLogger(StateManagementFeature.class);
52 private DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null;
53 private StateManagement stateManagement = null;
55 /**************************/
56 /* 'FeatureAPI' interface */
57 /**************************/
59 public StateManagementFeature(){
60 if(logger.isDebugEnabled()){
61 logger.debug("StateManagementFeature() constructor");
66 public void globalInit(String[] args, String configDir)
68 // Initialization code associated with 'PolicyContainer'
69 if(logger.isDebugEnabled()){
70 logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
75 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir);
79 if(logger.isDebugEnabled()){
80 logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
82 logger.error("DroolsPDPIntegrityMonitor.init()", e);
85 initializeProperties(configDir);
87 //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
89 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
90 stateManagement = droolsPdpIntegrityMonitor.getStateManager();
91 if(logger.isDebugEnabled()){
92 logger.debug("StateManagementFeature.globalInit(): "
93 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
95 if(stateManagement == null){
96 if(logger.isDebugEnabled()){
97 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
100 } catch (Exception e1) {
101 if(logger.isDebugEnabled()){
102 logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
103 + " initialization failed with exception:", e1);
105 logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
106 + "to get DroolsPDPIntegrityMonitor instance:", e1);
114 public void addObserver(Observer stateChangeObserver) {
115 if(logger.isDebugEnabled()){
116 logger.debug("StateManagementFeature.addObserver() entry\n"
117 + "StateManagementFeature.addObserver(): "
118 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
120 stateManagement.addObserver(stateChangeObserver);
121 if(logger.isDebugEnabled()){
122 logger.debug("StateManagementFeature.addObserver() exit");
130 public String getAdminState() {
131 return stateManagement.getAdminState();
138 public String getOpState() {
139 return stateManagement.getOpState();
146 public String getAvailStatus() {
147 return stateManagement.getAvailStatus();
154 public String getStandbyStatus() {
155 return stateManagement.getStandbyStatus();
162 public String getStandbyStatus(String resourceName) {
163 return stateManagement.getStandbyStatus(resourceName);
170 public void disableFailed(String resourceName) throws Exception {
171 stateManagement.disableFailed(resourceName);
179 public void disableFailed() throws Exception {
180 stateManagement.disableFailed();
187 public void promote() throws Exception {
188 stateManagement.promote();
195 public void demote() throws Exception {
196 stateManagement.demote();
203 public String getResourceName() {
204 return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
212 public boolean lock(){
214 stateManagement.lock();
216 logger.error("StateManagementFeature.lock() failed with exception: {}", e);
227 public boolean unlock(){
229 stateManagement.unlock();
231 logger.error("StateManagementFeature.unlock() failed with exception: {}", e);
242 public boolean isLocked(){
243 return StateManagement.LOCKED.equals(stateManagement.getAdminState());
247 public int getSequenceNumber() {
252 * Read in the properties and initialize the StateManagementProperties.
254 private static void initializeProperties(String configDir)
256 //Get the state management properties
259 PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
260 StateManagementProperties.initProperties(pIm);
261 logger.info("initializeProperties: resourceName= {}", StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
262 } catch (IOException e1) {
263 logger.error("initializeProperties", e1);