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