8f5e4e3cac57f0940ea46bde7602337880390fac
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-state-management
4  * ================================================================================
5  * Copyright (C) 2017-2021 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.drools.statemanagement;
22
23 import java.io.IOException;
24 import org.onap.policy.common.im.AllSeemsWellException;
25 import org.onap.policy.common.im.IntegrityMonitorException;
26 import org.onap.policy.common.im.StateChangeNotifier;
27 import org.onap.policy.common.im.StateManagement;
28 import org.onap.policy.common.im.StateManagementException;
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;
34
35 /**
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.
40  *
41  * <p>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.
44  */
45
46 public class StateManagementFeature implements StateManagementFeatureApi,
47     PolicySessionFeatureApi, PolicyEngineFeatureApi {
48     // get an instance of logger
49     private static final Logger logger =
50             LoggerFactory.getLogger(StateManagementFeature.class);
51
52     private DroolsPdpIntegrityMonitor droolsPdpIntegrityMonitor = null;
53     private StateManagement stateManagement = null;
54
55     public StateManagementFeature() {
56         logger.debug("StateManagementFeature() constructor");
57     }
58
59     @Override
60     public void globalInit(String[] args, String configDir) {
61         // Initialization code associated with 'PolicyContainer'
62         logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
63
64         try {
65             droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.init(configDir);
66         } catch (Exception e) {
67             logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
68             logger.error("DroolsPDPIntegrityMonitor.init()", e);
69         }
70
71         initializeProperties(configDir);
72
73         //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
74         try {
75             droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.getInstance();
76             stateManagement = droolsPdpIntegrityMonitor.getStateManager();
77
78             if (stateManagement == null) {
79                 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
80             } else {
81                 logger.debug("StateManagementFeature.globalInit(): "
82                         + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
83             }
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);
89         }
90     }
91
92     /**
93      * {@inheritDoc}.
94      */
95     @Override
96     public void addObserver(StateChangeNotifier stateChangeObserver) {
97         logger.debug("StateManagementFeature.addObserver() entry\n"
98                 + "StateManagementFeature.addObserver(): "
99                 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
100
101         stateManagement.addObserver(stateChangeObserver);
102
103         logger.debug("StateManagementFeature.addObserver() exit");
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public String getAdminState() {
111         return stateManagement.getAdminState();
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public String getOpState() {
119         return stateManagement.getOpState();
120     }
121
122     /**
123      * {@inheritDoc}.
124      */
125     @Override
126     public String getAvailStatus() {
127         return stateManagement.getAvailStatus();
128     }
129
130     /**
131      * {@inheritDoc}.
132      */
133     @Override
134     public String getStandbyStatus() {
135         return stateManagement.getStandbyStatus();
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public String getStandbyStatus(String resourceName) {
143         return stateManagement.getStandbyStatus(resourceName);
144     }
145
146     /**
147      * {@inheritDoc}.
148      */
149     @Override
150     public void disableFailed(String resourceName) throws StateManagementException {
151         stateManagement.disableFailed(resourceName);
152
153     }
154
155     /**
156      * {@inheritDoc}.
157      */
158     @Override
159     public void disableFailed() throws StateManagementException {
160         stateManagement.disableFailed();
161     }
162
163     /**
164      * {@inheritDoc}.
165      */
166     @Override
167     public void promote() throws IntegrityMonitorException {
168         stateManagement.promote();
169     }
170
171     /**
172      * {@inheritDoc}.
173      */
174     @Override
175     public void demote() throws StateManagementException {
176         stateManagement.demote();
177     }
178
179     /**
180      * {@inheritDoc}.
181      */
182     @Override
183     public String getResourceName() {
184         return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
185     }
186
187     /**
188      * {@inheritDoc}.
189      *
190      * @return true if locked or false if failed
191      */
192     @Override
193     public boolean lock() {
194         try {
195             stateManagement.lock();
196         } catch (Exception e) {
197             logger.error("StateManagementFeature.lock() failed with exception", e);
198             return false;
199         }
200         return true;
201     }
202
203     /**
204      * {@inheritDoc}.
205      *
206      * @throws Exception exception
207      */
208     @Override
209     public boolean unlock() {
210         try {
211             stateManagement.unlock();
212         } catch (Exception e) {
213             logger.error("StateManagementFeature.unlock() failed with exception", e);
214             return false;
215         }
216         return true;
217     }
218
219     /**
220      * {@inheritDoc}.
221      *
222      * @throws Exception exception
223      */
224     @Override
225     public boolean isLocked() {
226         return StateManagement.LOCKED.equals(stateManagement.getAdminState());
227     }
228
229     @Override
230     public int getSequenceNumber() {
231         return StateManagementFeatureApiConstants.SEQ_NUM;
232     }
233
234     /**
235      * Read in the properties and initialize the StateManagementProperties.
236      */
237     private static void initializeProperties(String configDir) {
238         //Get the state management properties
239         try {
240             var props = PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
241             StateManagementProperties.initProperties(props);
242             logger.info("initializeProperties: resourceName= {}",
243                     StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
244         } catch (IOException e1) {
245             logger.error("initializeProperties", e1);
246         }
247     }
248
249     @Override
250     public void allSeemsWell(String key, Boolean asw, String msg)
251             throws AllSeemsWellException {
252
253         droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg);
254
255     }
256 }