2 * ============LICENSE_START=======================================================
3 * feature-active-standby-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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.activestandby;
23 import java.io.IOException;
24 import java.util.HashMap;
26 import java.util.Properties;
27 import javax.persistence.EntityManagerFactory;
28 import javax.persistence.Persistence;
29 import org.onap.policy.common.im.MonitorTime;
30 import org.onap.policy.drools.core.PolicySessionFeatureApi;
31 import org.onap.policy.drools.features.PolicyEngineFeatureApi;
32 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
33 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
34 import org.onap.policy.drools.system.PolicyEngine;
35 import org.onap.policy.drools.system.PolicyEngineConstants;
36 import org.onap.policy.drools.utils.PropertyUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * If this feature is supported, there is a single instance of it.
42 * It adds persistence to Drools sessions, but it is also intertwined with
43 * active/standby state management and IntegrityMonitor. For now, they are
44 * all treated as a single feature, but it would be nice to separate them.
46 * <p>The bulk of the code here was once in other classes, such as
47 * 'PolicyContainer' and 'Main'. It was moved here as part of making this
48 * a separate optional feature.
50 public class ActiveStandbyFeature implements ActiveStandbyFeatureApi,
51 PolicySessionFeatureApi, PolicyEngineFeatureApi {
52 // get an instance of logger
53 private static final Logger logger =
54 LoggerFactory.getLogger(ActiveStandbyFeature.class);
56 private static DroolsPdp myPdp;
57 private static Object myPdpSync = new Object();
58 private static DroolsPdpsElectionHandler electionHandler;
60 private StateManagementFeatureApi stateManagementFeature;
62 public static final int SEQ_NUM = 1;
65 /*========================*/
66 /* 'FeatureAPI' interface */
67 /*========================*/
73 public int getSequenceNumber() {
81 public void globalInit(String[] args, String configDir) {
82 // This must come first since it initializes myPdp
83 initializePersistence(configDir);
85 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
86 if (feature.getResourceName().equals(myPdp.getPdpId())) {
87 logger.debug("ActiveStandbyFeature.globalInit: Found StateManagementFeature"
88 + " with resourceName: {}", myPdp.getPdpId());
89 stateManagementFeature = feature;
93 if (stateManagementFeature == null) {
94 logger.debug("ActiveStandbyFeature failed to initialize. "
95 + "Unable to get instance of StateManagementFeatureApi "
96 + "with resourceID: {}", myPdp.getPdpId());
97 logger.error("ActiveStandbyFeature failed to initialize. "
98 + "Unable to get instance of StateManagementFeatureApi "
99 + "with resourceID: {}", myPdp.getPdpId());
101 // Cannot add observer since stateManagementFeature is null
108 //Create an instance of the Observer
109 PmStandbyStateChangeNotifier pmNotifier = new PmStandbyStateChangeNotifier();
111 //Register the PMStandbyStateChangeNotifier Observer
112 stateManagementFeature.addObserver(pmNotifier);
113 logger.debug("ActiveStandbyFeature.globalInit() exit");
121 public boolean afterStart(PolicyEngine engine) {
122 // ASSERTION: engine == PolicyEngine.manager
123 PolicyEngineConstants.getManager().lock();
128 * Read in the persistence properties, determine whether persistence is
129 * enabled or disabled, and initialize persistence if enabled.
131 private static void initializePersistence(String configDir) {
132 //Get the Active Standby properties
134 Properties activeStandbyProperties =
135 PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties");
136 ActiveStandbyProperties.initProperties(activeStandbyProperties);
137 logger.info("initializePersistence: ActiveStandbyProperties success");
138 } catch (IOException e) {
139 logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e);
142 DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU");
143 String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
144 if (resourceName == null) {
145 throw new NullPointerException();
149 * In a JUnit test environment, one or more PDPs may already have been
150 * inserted in the DB, so we need to check for this.
152 DroolsPdp existingPdp = conn.getPdp(resourceName);
153 if (existingPdp != null) {
154 logger.info("Found existing PDP record, pdpId="
155 + existingPdp.getPdpId() + ", isDesignated="
156 + existingPdp.isDesignated() + ", updatedDate="
157 + existingPdp.getUpdatedDate());
161 synchronized (myPdpSync) {
164 myPdp = new DroolsPdpImpl(resourceName,false,4,MonitorTime.getInstance().getDate());
166 String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME);
167 if (siteName == null) {
170 siteName = siteName.trim();
172 myPdp.setSite(siteName);
173 if (electionHandler == null) {
174 electionHandler = new DroolsPdpsElectionHandler(conn,myPdp);
177 logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n");
182 * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to
183 * this method, so it can also be accessed from StandbyStateChangeNotifier
187 * @return connector object
189 public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) {
191 Map<String, Object> propMap = new HashMap<>();
192 propMap.put("javax.persistence.jdbc.driver", ActiveStandbyProperties
193 .getProperty(ActiveStandbyProperties.DB_DRIVER));
194 propMap.put("javax.persistence.jdbc.url",
195 ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_URL));
196 propMap.put("javax.persistence.jdbc.user", ActiveStandbyProperties
197 .getProperty(ActiveStandbyProperties.DB_USER));
198 propMap.put("javax.persistence.jdbc.password",
199 ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_PWD));
201 EntityManagerFactory emf = Persistence.createEntityManagerFactory(
203 return new JpaDroolsPdpsConnector(emf);
210 public String getPdpdNowActive() {
211 return electionHandler.getPdpdNowActive();
218 public String getPdpdLastActive() {
219 return electionHandler.getPdpdLastActive();
226 public String getResourceName() {
227 return myPdp.getPdpId();