2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sparky.sync;
23 import java.lang.Thread.UncaughtExceptionHandler;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.TimeUnit;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.sync.SyncControllerImpl.SyncActions;
33 import org.onap.aai.sparky.sync.enumeration.OperationState;
34 import org.onap.aai.sparky.sync.enumeration.SynchronizerState;
35 import org.springframework.context.ApplicationListener;
36 import org.springframework.context.event.ApplicationContextEvent;
38 import com.google.common.util.concurrent.ThreadFactoryBuilder;
40 public class SyncControllerService implements ApplicationListener<ApplicationContextEvent> {
42 private SyncControllerRegistry syncControllerRegistry;
43 private ExecutorService runonceSyncExecutor;
44 private ScheduledExecutorService periodicSyncExecutor;
45 private boolean syncStarted;
47 private static final Logger LOG =
48 LoggerFactory.getInstance().getLogger(SyncControllerService.class);
50 private class SyncControllerTask implements Runnable {
52 private SyncController controller;
54 public SyncControllerTask(SyncController controller) {
55 this.controller = controller;
63 if (controller.getState() == SynchronizerState.IDLE) {
66 * This is a blocking-call, but would be nicer if it was async internally within the
67 * controller but at the moment, that's not the way it works.
70 if (controller.performAction(SyncActions.SYNCHRONIZE) != OperationState.OK) {
72 LOG.info(AaiUiMsgs.INFO_GENERIC,
73 controller.getControllerName() + " is not idle, sync attempt has been skipped.");
77 LOG.info(AaiUiMsgs.INFO_GENERIC,
78 controller.getControllerName() + " is not idle, sync attempt has been skipped.");
81 } catch (Exception exception) {
82 LOG.error(AaiUiMsgs.ERROR_GENERIC,
83 "Error while attempting synchronization. Error = " + exception.getMessage());
90 public SyncControllerService(SyncControllerRegistry syncControllerRegistry, int numRunOnceWorkers,
91 int numPeriodicWorkers) {
92 this.syncControllerRegistry = syncControllerRegistry;
93 this.syncStarted = false;
95 UncaughtExceptionHandler uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
98 public void uncaughtException(Thread thread, Throwable exc) {
99 LOG.error(AaiUiMsgs.ERROR_GENERIC, thread.getName() + ": " + exc);
103 runonceSyncExecutor = Executors.newFixedThreadPool(numRunOnceWorkers,
104 new ThreadFactoryBuilder().setNameFormat("RunonceSyncWorker-%d")
105 .setUncaughtExceptionHandler(uncaughtExceptionHandler).build());
108 periodicSyncExecutor = Executors.newScheduledThreadPool(numPeriodicWorkers,
109 new ThreadFactoryBuilder().setNameFormat("PeriodicSyncWorker-%d")
110 .setUncaughtExceptionHandler(uncaughtExceptionHandler).build());
114 public SyncControllerRegistry getSyncControllerRegistry() {
115 return syncControllerRegistry;
118 public void startSync() {
120 long syncInitialDelayInMs = 0;
122 for (SyncController controller : syncControllerRegistry.getControllers()) {
124 syncInitialDelayInMs = controller.getDelayInMs();
126 if (!controller.isPeriodicSyncEnabled()) {
128 if (controller.isRunOnceSyncEnabled()) {
129 LOG.info(AaiUiMsgs.INFO_GENERIC, controller.getControllerName() + " is enabled.");
130 runonceSyncExecutor.submit(new SyncControllerTask(controller));
132 LOG.info(AaiUiMsgs.INFO_GENERIC, controller.getControllerName() + " is disabled.");
138 * Do both. We'll take one instance of the SyncController and wrap the object instance into
139 * two SyncControllerTasks. The responsibility for preventing a conflicting sync should live
140 * in the SyncController instance. If a sync is underway when the periodic sync kicks in,
141 * then it will be ignored by the SyncController which is already underway.
143 * The SyncController instance itself would then also be stateful such that it would know
144 * the last time it ran, and the next time it is supposed to run, the number times a sync
148 if (controller.isRunOnceSyncEnabled()) {
149 LOG.info(AaiUiMsgs.INFO_GENERIC,
150 controller.getControllerName() + " run-once sync is enabled.");
151 runonceSyncExecutor.submit(new SyncControllerTask(controller));
153 LOG.info(AaiUiMsgs.INFO_GENERIC,
154 controller.getControllerName() + " run-once sync is disabled.");
158 * The controller knows it's configuredfrequency and we can just ask it to tell us what the
159 * delay and frequency needs to be, rather than trying to calculate the configured frequency
160 * per controller which "could" be different for each controller.
163 if (controller.isPeriodicSyncEnabled()) {
165 LOG.info(AaiUiMsgs.INFO_GENERIC,
166 controller.getControllerName() + " periodic sync is enabled and scheduled to start @ "
167 + controller.getNextSyncTime());
169 periodicSyncExecutor.scheduleAtFixedRate(new SyncControllerTask(controller),
170 controller.getDelayInMs(), controller.getSyncFrequencyInMs(), TimeUnit.MILLISECONDS);
174 LOG.info(AaiUiMsgs.INFO_GENERIC,
175 controller.getControllerName() + " periodic sync is disabled.");
185 public void shutdown() {
187 if (runonceSyncExecutor != null) {
188 runonceSyncExecutor.shutdown();
191 if (periodicSyncExecutor != null) {
192 periodicSyncExecutor.shutdown();
195 if (syncControllerRegistry != null) {
196 for (SyncController controller : syncControllerRegistry.getControllers()) {
197 controller.shutdown();
204 public synchronized void onApplicationEvent(ApplicationContextEvent arg0) {
207 * Start sync service processing when spring-context-initialization has finished