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