2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.configpusherfeature.internal;
10 import com.google.common.collect.LinkedHashMultimap;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.TimeUnit;
17 import org.apache.karaf.features.Feature;
18 import org.apache.karaf.features.FeatureEvent;
19 import org.apache.karaf.features.FeatureEvent.EventType;
20 import org.apache.karaf.features.FeaturesService;
21 import org.opendaylight.controller.config.persist.api.ConfigPusher;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class ConfigPushingRunnable implements Runnable {
26 private static final Logger LOG = LoggerFactory.getLogger(ConfigPushingRunnable.class);
27 private static final int POLL_TIME = 1;
28 private BlockingQueue<FeatureEvent> queue;
29 private FeatureConfigPusher configPusher;
31 public ConfigPushingRunnable(final ConfigPusher configPusher, final FeaturesService featuresService,
32 final BlockingQueue<FeatureEvent> featureEvents) {
33 queue = featureEvents;
34 this.configPusher = new FeatureConfigPusher(configPusher, featuresService);
38 @SuppressWarnings("IllegalCatch")
40 List<Feature> toInstall = new ArrayList<>();
41 FeatureEvent event = null;
42 boolean interrupted = false;
46 if (toInstall.isEmpty()) {
49 event = queue.poll(POLL_TIME, TimeUnit.MILLISECONDS);
51 if (event != null && event.getFeature() != null) {
52 processFeatureEvent(event, toInstall);
54 } else if (toInstall.isEmpty()) {
55 LOG.error("ConfigPushingRunnable - exiting");
58 } catch (final InterruptedException e) {
59 LOG.error("ConfigPushingRunnable - interrupted");
61 } catch (final Exception e) {
62 LOG.error("Exception while processing features {} event {}", toInstall, event, e);
67 protected void processFeatureEvent(final FeatureEvent event, final List<Feature> toInstall) throws Exception {
68 if (event.getType() == EventType.FeatureInstalled) {
69 toInstall.add(event.getFeature());
70 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> result = configPusher.pushConfigs(toInstall);
71 toInstall.removeAll(result.keySet());
72 } else if (event.getType() == EventType.FeatureUninstalled) {
73 toInstall.remove(event.getFeature());
77 protected void logPushResult(final LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> results) {
78 for (Feature f : results.keySet()) {
79 LOG.info("Pushed configs for feature {} {}", f, results.get(f));