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.base.Optional;
11 import com.google.common.base.Preconditions;
13 import java.util.LinkedHashSet;
14 import java.util.List;
17 import org.apache.felix.utils.version.VersionRange;
18 import org.apache.felix.utils.version.VersionTable;
19 import org.apache.karaf.features.Dependency;
20 import org.apache.karaf.features.Feature;
21 import org.apache.karaf.features.FeaturesService;
22 import org.osgi.framework.Version;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
28 * its underlying ConfigFileInfo's and those of its children recursively
30 * Delegates the the contained feature and provides additional methods.
32 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
33 private static final Logger LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
34 private FeaturesService featuresService = null;
36 protected ChildAwareFeatureWrapper(final Feature feature) {
37 // Don't use without a feature service
42 * @param feature Feature to wrap
43 * @param featuresService FeaturesService to look up dependencies
45 ChildAwareFeatureWrapper(final Feature feature, final FeaturesService featuresService) throws Exception {
46 super(featuresService.getFeature(feature.getName(), feature.getVersion()));
47 Preconditions.checkNotNull(featuresService, "FeatureWrapper requires non-null FeatureService in constructor");
48 this.featuresService = featuresService;
51 protected FeaturesService getFeaturesService() {
52 return featuresService;
56 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
57 * from the underlying Feature Config files and those of its children recursively
59 public Set<? extends ChildAwareFeatureWrapper> getChildFeatures() throws Exception {
60 List<Dependency> dependencies = feature.getDependencies();
61 Set<ChildAwareFeatureWrapper> childFeatures = new LinkedHashSet<>();
62 if (dependencies != null) {
63 for (Dependency dependency : dependencies) {
64 Feature fi = extractFeatureFromDependency(dependency);
66 if (featuresService.getFeature(fi.getName(), fi.getVersion()) == null) {
67 LOG.warn("Feature: {}, {} is missing from features service. Skipping", fi.getName(), fi
70 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi, featuresService);
71 childFeatures.add(wrappedFeature);
80 public Set<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
81 Set<FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
82 for (ChildAwareFeatureWrapper c : getChildFeatures()) {
83 for (FeatureConfigSnapshotHolder h : c.getFeatureConfigSnapshotHolders()) {
84 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder =
85 getFeatureConfigSnapshotHolder(h.getFileInfo());
86 if (featureConfigSnapshotHolder.isPresent()) {
87 snapShotHolders.add(featureConfigSnapshotHolder.get());
91 snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
92 return snapShotHolders;
95 protected Feature extractFeatureFromDependency(final Dependency dependency) throws Exception {
96 Feature[] features = featuresService.listFeatures();
97 VersionRange range = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION.equals(dependency
98 .getVersion()) ? VersionRange.ANY_VERSION : new VersionRange(dependency.getVersion(), true, true);
100 for (Feature f : features) {
101 if (f.getName().equals(dependency.getName())) {
102 Version version = VersionTable.getVersion(f.getVersion());
103 if (range.contains(version) && (fi == null || VersionTable.getVersion(fi.getVersion())
104 .compareTo(version) < 0)) {