7d5ba512dea51e0088cf2afcafd66e3bd0563d22
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-state-management
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.Observer;
25 import java.util.Properties;
26
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;
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             }
81             else {
82                 logger.debug("StateManagementFeature.globalInit(): "
83                         + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
84             }
85         } catch (Exception e1) {
86             logger.debug("StateManagementFeature.globalInit(): DroolsPDPIntegrityMonitor"
87                     + " initialization failed with exception:", e1);
88             logger.error("DroolsPDPIntegrityMonitor.init(): StateManagementFeature startup failed "
89                     + "to get DroolsPDPIntegrityMonitor instance:", e1);
90         }
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public void addObserver(Observer stateChangeObserver) {
98         logger.debug("StateManagementFeature.addObserver() entry\n"
99                 + "StateManagementFeature.addObserver(): "
100                 + "stateManagement.getAdminState(): {}", stateManagement.getAdminState());
101
102         stateManagement.addObserver(stateChangeObserver);
103
104         logger.debug("StateManagementFeature.addObserver() exit");
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public String getAdminState() {
112         return stateManagement.getAdminState();
113     }
114
115     /**
116      * {@inheritDoc}.
117      */
118     @Override
119     public String getOpState() {
120         return stateManagement.getOpState();
121     }
122
123     /**
124      * {@inheritDoc}.
125      */
126     @Override
127     public String getAvailStatus() {
128         return stateManagement.getAvailStatus();
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
135     public String getStandbyStatus() {
136         return stateManagement.getStandbyStatus();
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public String getStandbyStatus(String resourceName) {
144         return stateManagement.getStandbyStatus(resourceName);
145     }
146
147     /**
148      * {@inheritDoc}.
149      */
150     @Override
151     public void disableFailed(String resourceName) throws Exception {
152         stateManagement.disableFailed(resourceName);
153
154     }
155
156     /**
157      * {@inheritDoc}.
158      */
159     @Override
160     public void disableFailed() throws Exception {
161         stateManagement.disableFailed();
162     }
163
164     /**
165      * {@inheritDoc}.
166      */
167     @Override
168     public void promote() throws Exception {
169         stateManagement.promote();
170     }
171
172     /**
173      * {@inheritDoc}.
174      */
175     @Override
176     public void demote() throws Exception {
177         stateManagement.demote();
178     }
179
180     /**
181      * {@inheritDoc}.
182      */
183     @Override
184     public String getResourceName() {
185         return StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME);
186     }
187
188     /**
189      * {@inheritDoc}.
190      *
191      * @return true if locked or false if failed
192      */
193     @Override
194     public boolean lock() {
195         try {
196             stateManagement.lock();
197         } catch (Exception e) {
198             logger.error("StateManagementFeature.lock() failed with exception: {}", e);
199             return false;
200         }
201         return true;
202     }
203
204     /**
205      * {@inheritDoc}.
206      *
207      * @throws Exception exception
208      */
209     @Override
210     public boolean unlock() {
211         try {
212             stateManagement.unlock();
213         } catch (Exception e) {
214             logger.error("StateManagementFeature.unlock() failed with exception: {}", e);
215             return false;
216         }
217         return true;
218     }
219
220     /**
221      * {@inheritDoc}.
222      *
223      * @throws Exception exception
224      */
225     @Override
226     public boolean isLocked() {
227         return StateManagement.LOCKED.equals(stateManagement.getAdminState());
228     }
229
230     @Override
231     public int getSequenceNumber() {
232         return StateManagementFeatureApiConstants.SEQ_NUM;
233     }
234
235     /**
236      * Read in the properties and initialize the StateManagementProperties.
237      */
238     private static void initializeProperties(String configDir) {
239         //Get the state management properties
240         try {
241             Properties props =
242                     PropertyUtil.getProperties(configDir + "/feature-state-management.properties");
243             StateManagementProperties.initProperties(props);
244             logger.info("initializeProperties: resourceName= {}",
245                     StateManagementProperties.getProperty(StateManagementProperties.NODE_NAME));
246         } catch (IOException e1) {
247             logger.error("initializeProperties", e1);
248         }
249     }
250
251     @Override
252     public void allSeemsWell(String key, Boolean asw, String msg)
253             throws AllSeemsWellException {
254
255         droolsPdpIntegrityMonitor.allSeemsWell(key, asw, msg);
256
257     }
258 }