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

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