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