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.AllSeemsWellException;
28 import org.onap.policy.common.im.StateManagement;
29 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
30 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
31 import org.onap.policy.drools.utils.PropertyUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * If this feature is supported, there is a single instance of it.
37 * It adds persistence to Drools sessions, but it is also intertwined with
38 * active/standby state management and IntegrityMonitor. For now, they are
39 * all treated as a single feature, but it would be nice to separate them.
41 * The bulk of the code here was once in other classes, such as
42 * 'PolicyContainer' and 'Main'. It was moved here as part of making this
43 * a separate optional feature.
46 public class StateManagementFeature implements StateManagementFeatureAPI,
47 PolicySessionFeatureAPI, PolicyEngineFeatureAPI
49 // get an instance of logger
50 private static final Logger logger =
51 LoggerFactory.getLogger(StateManagementFeature.class);
53 private DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor = null;
54 private StateManagement stateManagement = null;
56 /**************************/
57 /* 'FeatureAPI' interface */
58 /**************************/
60 public StateManagementFeature(){
61 logger.debug("StateManagementFeature() constructor");
65 public void globalInit(String[] args, String configDir)
67 // Initialization code associated with 'PolicyContainer'
68 logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
72 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.init(configDir);
76 logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
77 logger.error("DroolsPDPIntegrityMonitor.init()", e);
80 initializeProperties(configDir);
82 //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
84 droolsPdpIntegrityMonitor = DroolsPDPIntegrityMonitor.getInstance();
85 stateManagement = droolsPdpIntegrityMonitor.getStateManager();
87 logger.debug("StateManagementFeature.globalInit(): "
88 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
90 if(stateManagement == null){
91 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
93 } catch (Exception e1) {
94 logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
95 + " initialization failed with exception:", e1);
96 logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
97 + "to get DroolsPDPIntegrityMonitor instance:", e1);
105 public void addObserver(Observer stateChangeObserver) {
106 logger.debug("StateManagementFeature.addObserver() entry\n"
107 + "StateManagementFeature.addObserver(): "
108 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
110 stateManagement.addObserver(stateChangeObserver);
112 logger.debug("StateManagementFeature.addObserver() exit");
119 public String getAdminState() {
120 return stateManagement.getAdminState();
127 public String getOpState() {
128 return stateManagement.getOpState();
135 public String getAvailStatus() {
136 return stateManagement.getAvailStatus();
143 public String getStandbyStatus() {
144 return stateManagement.getStandbyStatus();
151 public String getStandbyStatus(String resourceName) {
152 return stateManagement.getStandbyStatus(resourceName);
159 public void disableFailed(String resourceName) throws Exception {
160 stateManagement.disableFailed(resourceName);
168 public void disableFailed() throws Exception {
169 stateManagement.disableFailed();
176 public void promote() throws Exception {
177 stateManagement.promote();
184 public void demote() throws Exception {
185 stateManagement.demote();
192 public String getResourceName() {
193 return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
201 public boolean lock(){
203 stateManagement.lock();
205 logger.error("StateManagementFeature.lock() failed with exception: {}", e);
216 public boolean unlock(){
218 stateManagement.unlock();
220 logger.error("StateManagementFeature.unlock() failed with exception: {}", e);
231 public boolean isLocked(){
232 return StateManagement.LOCKED.equals(stateManagement.getAdminState());
236 public int getSequenceNumber() {
241 * Read in the properties and initialize the StateManagementProperties.
243 private static void initializeProperties(String configDir)
245 //Get the state management properties
248 PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
249 StateManagementProperties.initProperties(pIm);
250 logger.info("initializeProperties: resourceName= {}", StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
251 } catch (IOException e1) {
252 logger.error("initializeProperties", e1);
257 public void allSeemsWell(String key, Boolean asw, String msg)
258 throws AllSeemsWellException {
260 droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg);