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