75233376f56e340693c4f7482f26b08c5a69cef8
[policy/drools-pdp.git] /
1 /*-
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
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 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;
33
34 /**
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.
39  *
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.
43  */
44
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);
50
51     private DroolsPdpIntegrityMonitor droolsPdpIntegrityMonitor = null;
52     private StateManagement stateManagement = null;
53
54     public StateManagementFeature() {
55         logger.debug("StateManagementFeature() constructor");
56     }
57
58     @Override
59     public void globalInit(String[] args, String configDir) {
60         // Initialization code associated with 'PolicyContainer'
61         logger.debug("StateManagementFeature.globalInit({}) entry", configDir);
62
63         try {
64             droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.init(configDir);
65         } catch (Exception e) {
66             logger.debug("DroolsPDPIntegrityMonitor initialization exception: ", e);
67             logger.error("DroolsPDPIntegrityMonitor.init()", e);
68         }
69
70         initializeProperties(configDir);
71
72         //At this point the DroolsPDPIntegrityMonitor instance must exist. Let's check it.
73         try {
74             droolsPdpIntegrityMonitor = DroolsPdpIntegrityMonitor.getInstance();
75             stateManagement = droolsPdpIntegrityMonitor.getStateManager();
76
77             if (stateManagement == null) {
78                 logger.debug("StateManagementFeature.globalInit(): stateManagement is NULL!");
79             } else {
80                 logger.debug("StateManagementFeature.globalInit(): "
81                         + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
82             }
83         } catch (Exception e1) {
84             logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
85                     + " initialization failed with exception:", e1);
86             logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
87                     + "to get DroolsPDPIntegrityMonitor instance:", e1);
88         }
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public void addObserver(StateChangeNotifier stateChangeObserver) {
96         logger.debug("StateManagementFeature.addObserver() entry\n"
97                 + "StateManagementFeature.addObserver(): "
98                 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
99
100         stateManagement.addObserver(stateChangeObserver);
101
102         logger.debug("StateManagementFeature.addObserver() exit");
103     }
104
105     /**
106      * {@inheritDoc}.
107      */
108     @Override
109     public String getAdminState() {
110         return stateManagement.getAdminState();
111     }
112
113     /**
114      * {@inheritDoc}.
115      */
116     @Override
117     public String getOpState() {
118         return stateManagement.getOpState();
119     }
120
121     /**
122      * {@inheritDoc}.
123      */
124     @Override
125     public String getAvailStatus() {
126         return stateManagement.getAvailStatus();
127     }
128
129     /**
130      * {@inheritDoc}.
131      */
132     @Override
133     public String getStandbyStatus() {
134         return stateManagement.getStandbyStatus();
135     }
136
137     /**
138      * {@inheritDoc}.
139      */
140     @Override
141     public String getStandbyStatus(String resourceName) {
142         return stateManagement.getStandbyStatus(resourceName);
143     }
144
145     /**
146      * {@inheritDoc}.
147      */
148     @Override
149     public void disableFailed(String resourceName) throws Exception {
150         stateManagement.disableFailed(resourceName);
151
152     }
153
154     /**
155      * {@inheritDoc}.
156      */
157     @Override
158     public void disableFailed() throws Exception {
159         stateManagement.disableFailed();
160     }
161
162     /**
163      * {@inheritDoc}.
164      */
165     @Override
166     public void promote() throws Exception {
167         stateManagement.promote();
168     }
169
170     /**
171      * {@inheritDoc}.
172      */
173     @Override
174     public void demote() throws Exception {
175         stateManagement.demote();
176     }
177
178     /**
179      * {@inheritDoc}.
180      */
181     @Override
182     public String getResourceName() {
183         return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
184     }
185
186     /**
187      * {@inheritDoc}.
188      *
189      * @return true if locked or false if failed
190      */
191     @Override
192     public boolean lock() {
193         try {
194             stateManagement.lock();
195         } catch (Exception e) {
196             logger.error("StateManagementFeature.lock() failed with exception", e);
197             return false;
198         }
199         return true;
200     }
201
202     /**
203      * {@inheritDoc}.
204      *
205      * @throws Exception exception
206      */
207     @Override
208     public boolean unlock() {
209         try {
210             stateManagement.unlock();
211         } catch (Exception e) {
212             logger.error("StateManagementFeature.unlock() failed with exception", e);
213             return false;
214         }
215         return true;
216     }
217
218     /**
219      * {@inheritDoc}.
220      *
221      * @throws Exception exception
222      */
223     @Override
224     public boolean isLocked() {
225         return StateManagement.LOCKED.equals(stateManagement.getAdminState());
226     }
227
228     @Override
229     public int getSequenceNumber() {
230         return StateManagementFeatureApiConstants.SEQ_NUM;
231     }
232
233     /**
234      * Read in the properties and initialize the StateManagementProperties.
235      */
236     private static void initializeProperties(String configDir) {
237         //Get the state management properties
238         try {
239             Properties props =
240                     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 }