Mechanical code cleanup (config)
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / ConfigPushingRunnable.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.configpusherfeature.internal;
9
10 import com.google.common.collect.LinkedHashMultimap;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.BlockingQueue;
14 import java.util.concurrent.TimeUnit;
15 import org.apache.karaf.features.Feature;
16 import org.apache.karaf.features.FeatureEvent;
17 import org.apache.karaf.features.FeatureEvent.EventType;
18 import org.apache.karaf.features.FeaturesService;
19 import org.opendaylight.controller.config.persist.api.ConfigPusher;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ConfigPushingRunnable implements Runnable {
24     private static final Logger LOG = LoggerFactory.getLogger(ConfigPushingRunnable.class);
25     private static final int POLL_TIME = 1;
26     private BlockingQueue<FeatureEvent> queue;
27     private FeatureConfigPusher configPusher;
28     public ConfigPushingRunnable(ConfigPusher p, FeaturesService f,BlockingQueue<FeatureEvent> q) {
29         queue = q;
30         configPusher = new FeatureConfigPusher(p, f);
31     }
32
33     @Override
34     public void run() {
35         List<Feature> toInstall = new ArrayList<>();
36         FeatureEvent event = null;
37         boolean interuppted = false;
38         while(true) {
39             try {
40                 if(!interuppted) {
41                     if(toInstall.isEmpty()) {
42                         event = queue.take();
43                     } else {
44                         event = queue.poll(POLL_TIME, TimeUnit.MILLISECONDS);
45                     }
46                     if(event != null && event.getFeature() !=null) {
47                         processFeatureEvent(event,toInstall);
48                     }
49                 } else if(toInstall.isEmpty()) {
50                     LOG.error("ConfigPushingRunnable - exiting");
51                     return;
52                 }
53             } catch (InterruptedException e) {
54                 LOG.error("ConfigPushingRunnable - interupted");
55                 interuppted = true;
56             } catch (Exception e) {
57                 LOG.error("Exception while processing features {} event {}", toInstall, event, e);
58             }
59         }
60     }
61
62     protected void processFeatureEvent(FeatureEvent event, List<Feature> toInstall) throws InterruptedException, Exception {
63         if(event.getType() == EventType.FeatureInstalled) {
64             toInstall.add(event.getFeature());
65             LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> result = configPusher.pushConfigs(toInstall);
66             toInstall.removeAll(result.keySet());
67         } else if(event.getType() == EventType.FeatureUninstalled) {
68             toInstall.remove(event.getFeature());
69         }
70     }
71
72     protected void logPushResult(LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> results) {
73         for(Feature f:results.keySet()) {
74             LOG.info("Pushed configs for feature {} {}",f,results.get(f));
75         }
76     }
77 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.