5d487e0b9cd8ff6aa427450679291135e320f65c
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureConfigPusher.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.base.Optional;
11 import com.google.common.collect.LinkedHashMultimap;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.LinkedHashSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.concurrent.TimeUnit;
19 import org.apache.karaf.features.Feature;
20 import org.apache.karaf.features.FeaturesService;
21 import org.opendaylight.controller.config.persist.api.ConfigPusher;
22 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /*
27  * Simple class to push configs to the config subsystem from Feature's configfiles
28  */
29 public class FeatureConfigPusher {
30     private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
31     private static final int MAX_RETRIES = 100;
32     private static final int RETRY_PAUSE_MILLIS = 1;
33
34     private FeaturesService featuresService = null;
35     private ConfigPusher pusher = null;
36
37     /*
38      * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
39      * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
40      * chains.  Also, preserves the *original* Feature chain for which we pushed the config.
41      * (which is handy for logging).
42      */
43     private final Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
44
45     /*
46      * LinkedHashMultimap to track which configs we pushed for each Feature installation
47      * For future use
48      */
49     private final LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
50
51     /*
52      * @param p - ConfigPusher to push ConfigSnapshotHolders
53      */
54     public FeatureConfigPusher(final ConfigPusher p, final FeaturesService f) {
55         pusher = p;
56         featuresService = f;
57     }
58     /*
59      * Push config files from Features to config subsystem
60      * @param features - list of Features to extract config files from recursively and push
61      * to the config subsystem
62      *
63      * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
64      * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
65      * (Ususally because it was not yet installed)
66      */
67     public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features)
68             throws Exception {
69         LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
70         for (Feature feature : features) {
71             Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
72             if (!configSnapShots.isEmpty()) {
73                 pushedFeatures.putAll(feature, configSnapShots);
74             }
75         }
76         return pushedFeatures;
77     }
78
79     private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
80         // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
81         if (feature.getName().contains("-condition-")) {
82             LOG.debug("Ignoring conditional feature {}", feature);
83             return Collections.emptySet();
84         }
85         // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
86         if ("test-dependencies".equals(feature.getName())) {
87             LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
88             return Collections.emptySet();
89         }
90
91         if (!isInstalled(feature)) {
92             return Collections.emptySet();
93         }
94         // FIXME Workaround for BUG-2836, features service returns null for feature:
95         // standard-condition-webconsole_0_0_0, 3.0.1
96         if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
97             LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(),
98                 feature.getVersion());
99             return Collections.emptySet();
100         }
101
102         ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
103         Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
104         if (!configs.isEmpty()) {
105             configs = pushConfig(configs, feature);
106             feature2configs.putAll(feature, configs);
107         }
108         return configs;
109     }
110
111     private boolean isInstalled(final Feature feature) throws InterruptedException {
112         for (int retries = 0; retries < MAX_RETRIES; retries++) {
113             try {
114                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
115                 if (installedFeatures.contains(feature)) {
116                     return true;
117                 }
118
119                 LOG.info("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})",
120                     feature.getName(), feature.getVersion(), retries);
121             } catch (final Exception e) {
122                 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
123             }
124
125             TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
126         }
127         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",
128             MAX_RETRIES, feature.getName(), feature.getVersion());
129         return false;
130     }
131
132     private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
133             final Feature feature) throws InterruptedException {
134         Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
135         configsToPush.removeAll(pushedConfigs);
136         if (!configsToPush.isEmpty()) {
137
138             // Ignore features that are present in persisted current config
139             final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
140             if (currentCfgPusher.isPresent() &&
141                     currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
142                 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
143                         configsToPush, feature.getId(), currentCfgPusher.get());
144             } else {
145                 pusher.pushConfigs(new ArrayList<>(configsToPush));
146             }
147
148             pushedConfigs.addAll(configsToPush);
149         }
150         Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
151         configsPushed.retainAll(configs);
152         return configsPushed;
153     }
154 }

©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.