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.drools.statemanagement.StateManagementFeatureAPI;
28 import org.onap.policy.common.im.StandbyStatusException;
29 import org.onap.policy.common.im.StateManagement;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
33 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
34 import org.onap.policy.drools.utils.PropertyUtil;
37 * If this feature is supported, there is a single instance of it.
38 * It adds persistence to Drools sessions, but it is also intertwined with
39 * active/standby state management and IntegrityMonitor. For now, they are
40 * all treated as a single feature, but it would be nice to separate them.
42 * The bulk of the code here was once in other classes, such as
43 * 'PolicyContainer' and 'Main'. It was moved here as part of making this
44 * a separate optional feature.
47 public class StateManagementFeature implements StateManagementFeatureAPI,
48 PolicySessionFeatureAPI, PolicyEngineFeatureAPI
50 // get an instance of logger
51 private static final Logger logger =
52 LoggerFactory.getLogger(StateManagementFeature.class);
54 private DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null;
55 private StateManagement stateManagement = null;
57 /**************************/
58 /* 'FeatureAPI' interface */
59 /**************************/
61 public StateManagementFeature(){
62 if(logger.isDebugEnabled()){
63 logger.debug("StateManagementFeature() constructor");
68 public void globalInit(String args[], String configDir)
70 // Initialization code associated with 'PolicyContainer'
71 if(logger.isDebugEnabled()){
72 logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
77 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir);
81 if(logger.isDebugEnabled()){
82 logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
84 logger.error("DroolsPDPIntegrityMonitor.init()", e);
87 initializeProperties(configDir);
89 //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
91 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
92 stateManagement = droolsPdpIntegrityMonitor.getStateManager();
93 if(logger.isDebugEnabled()){
94 logger.debug("StateManagementFeature.globalInit(): "
95 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
97 if(stateManagement == null){
98 if(logger.isDebugEnabled()){
99 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
102 } catch (Exception e1) {
104 if(logger.isDebugEnabled()){
105 logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
106 + " initialization failed with exception:", e1);
108 logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
109 + "to get DroolsPDPIntegrityMonitor instance:", e1);
110 e1.printStackTrace();
118 public void addObserver(Observer stateChangeObserver) {
119 if(logger.isDebugEnabled()){
120 logger.debug("StateManagementFeature.addObserver() entry\n"
121 + "StateManagementFeature.addObserver(): "
122 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
124 stateManagement.addObserver(stateChangeObserver);
125 if(logger.isDebugEnabled()){
126 logger.debug("StateManagementFeature.addObserver() exit");
134 public String getAdminState() {
135 return stateManagement.getAdminState();
142 public String getOpState() {
143 return stateManagement.getOpState();
150 public String getAvailStatus() {
151 return stateManagement.getAvailStatus();
158 public String getStandbyStatus() {
159 return stateManagement.getStandbyStatus();
166 public String getStandbyStatus(String resourceName) {
167 return stateManagement.getStandbyStatus(resourceName);
174 public void disableFailed(String resourceName) throws Exception {
175 stateManagement.disableFailed(resourceName);
183 public void disableFailed() throws Exception {
184 stateManagement.disableFailed();
191 public void promote() throws StandbyStatusException, Exception {
192 stateManagement.promote();
199 public void demote() throws Exception {
200 stateManagement.demote();
207 public String getResourceName() {
208 return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
216 public boolean lock(){
218 stateManagement.lock();
220 logger.error("StateManagementFeature.lock() failed with exception: {}", e);
231 public boolean unlock(){
233 stateManagement.unlock();
235 logger.error("StateManagementFeature.unlock() failed with exception: {}", e);
246 public boolean isLocked(){
247 String admin = stateManagement.getAdminState();
248 if(admin.equals(StateManagement.LOCKED)){
256 public int getSequenceNumber() {
261 * Read in the properties and initialize the StateManagementProperties.
263 private static void initializeProperties(String configDir)
265 //Get the state management properties
268 PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
269 StateManagementProperties.initProperties(pIm);
270 logger.info("initializeProperties: resourceName= {}", StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
271 } catch (IOException e1) {
272 logger.error("initializeProperties", e1);