2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.sync;
27 import java.lang.Thread.UncaughtExceptionHandler;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.TimeUnit;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
36 import org.onap.aai.sparky.sync.SyncControllerImpl.SyncActions;
37 import org.onap.aai.sparky.sync.enumeration.OperationState;
38 import org.onap.aai.sparky.sync.enumeration.SynchronizerState;
39 import org.springframework.context.ApplicationListener;
40 import org.springframework.context.event.ApplicationContextEvent;
42 import com.google.common.util.concurrent.ThreadFactoryBuilder;
44 public class SyncControllerService implements ApplicationListener<ApplicationContextEvent> {
46 private SyncControllerRegistry syncControllerRegistry;
47 private ExecutorService runonceSyncExecutor;
48 private ScheduledExecutorService periodicSyncExecutor;
49 private boolean syncStarted;
51 private static final Logger LOG =
52 LoggerFactory.getInstance().getLogger(SyncControllerService.class);
54 private class SyncControllerTask implements Runnable {
56 private SyncController controller;
58 public SyncControllerTask(SyncController controller) {
59 this.controller = controller;
67 if (controller.getState() == SynchronizerState.IDLE) {
70 * This is a blocking-call, but would be nicer if it was async internally within the
71 * controller but at the moment, that's not the way it works.
74 if (controller.performAction(SyncActions.SYNCHRONIZE) != OperationState.OK) {
76 LOG.info(AaiUiMsgs.INFO_GENERIC,
77 controller.getControllerName() + " is not idle, sync attempt has been skipped.");
81 LOG.info(AaiUiMsgs.INFO_GENERIC,
82 controller.getControllerName() + " is not idle, sync attempt has been skipped.");
85 } catch (Exception exception) {
86 LOG.error(AaiUiMsgs.ERROR_GENERIC,
87 "Error while attempting synchronization. Error = " + exception.getMessage());
94 public SyncControllerService(SyncControllerRegistry syncControllerRegistry, int numRunOnceWorkers,
95 int numPeriodicWorkers) {
96 this.syncControllerRegistry = syncControllerRegistry;
97 this.syncStarted = false;
99 UncaughtExceptionHandler uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
102 public void uncaughtException(Thread thread, Throwable exc) {
103 LOG.error(AaiUiMsgs.ERROR_GENERIC, thread.getName() + ": " + exc);
107 runonceSyncExecutor = Executors.newFixedThreadPool(numRunOnceWorkers,
108 new ThreadFactoryBuilder().setNameFormat("RunonceSyncWorker-%d")
109 .setUncaughtExceptionHandler(uncaughtExceptionHandler).build());
112 periodicSyncExecutor = Executors.newScheduledThreadPool(numPeriodicWorkers,
113 new ThreadFactoryBuilder().setNameFormat("PeriodicSyncWorker-%d")
114 .setUncaughtExceptionHandler(uncaughtExceptionHandler).build());
118 public SyncControllerRegistry getSyncControllerRegistry() {
119 return syncControllerRegistry;
122 public void startSync() {
124 long syncInitialDelayInMs = 0;
126 for (SyncController controller : syncControllerRegistry.getControllers()) {
128 syncInitialDelayInMs = controller.getDelayInMs();
130 if (!controller.isPeriodicSyncEnabled()) {
132 if (controller.isRunOnceSyncEnabled()) {
133 LOG.info(AaiUiMsgs.INFO_GENERIC, controller.getControllerName() + " is enabled.");
134 runonceSyncExecutor.submit(new SyncControllerTask(controller));
136 LOG.info(AaiUiMsgs.INFO_GENERIC, controller.getControllerName() + " is disabled.");
142 * Do both. We'll take one instance of the SyncController and wrap the object instance into
143 * two SyncControllerTasks. The responsibility for preventing a conflicting sync should live
144 * in the SyncController instance. If a sync is underway when the periodic sync kicks in,
145 * then it will be ignored by the SyncController which is already underway.
147 * The SyncController instance itself would then also be stateful such that it would know
148 * the last time it ran, and the next time it is supposed to run, the number times a sync
152 if (controller.isRunOnceSyncEnabled()) {
153 LOG.info(AaiUiMsgs.INFO_GENERIC,
154 controller.getControllerName() + " run-once sync is enabled.");
155 runonceSyncExecutor.submit(new SyncControllerTask(controller));
157 LOG.info(AaiUiMsgs.INFO_GENERIC,
158 controller.getControllerName() + " run-once sync is disabled.");
162 * The controller knows it's configuredfrequency and we can just ask it to tell us what the
163 * delay and frequency needs to be, rather than trying to calculate the configured frequency
164 * per controller which "could" be different for each controller.
167 if (controller.isPeriodicSyncEnabled()) {
169 LOG.info(AaiUiMsgs.INFO_GENERIC,
170 controller.getControllerName() + " periodic sync is enabled and scheduled to start @ "
171 + controller.getNextSyncTime());
173 periodicSyncExecutor.scheduleAtFixedRate(new SyncControllerTask(controller),
174 controller.getDelayInMs(), controller.getSyncFrequencyInMs(), TimeUnit.MILLISECONDS);
178 LOG.info(AaiUiMsgs.INFO_GENERIC,
179 controller.getControllerName() + " periodic sync is disabled.");
189 public void shutdown() {
191 if (runonceSyncExecutor != null) {
192 runonceSyncExecutor.shutdown();
195 if (periodicSyncExecutor != null) {
196 periodicSyncExecutor.shutdown();
199 if (syncControllerRegistry != null) {
200 for (SyncController controller : syncControllerRegistry.getControllers()) {
201 controller.shutdown();
208 public synchronized void onApplicationEvent(ApplicationContextEvent arg0) {
211 * Start sync service processing when spring-context-initialization has finished