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;
12 import java.util.LinkedHashSet;
13 import java.util.List;
14 import org.apache.felix.utils.version.VersionRange;
15 import org.apache.felix.utils.version.VersionTable;
16 import org.apache.karaf.features.Dependency;
17 import org.apache.karaf.features.Feature;
18 import org.apache.karaf.features.FeaturesService;
19 import org.osgi.framework.Version;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
25 * its underlying ConfigFileInfo's and those of its children recursively
27 * Delegates the the contained feature and provides additional methods.
29 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
30 private static final Logger LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
31 private FeaturesService featuresService= null;
33 protected ChildAwareFeatureWrapper(Feature f) {
34 // Don't use without a feature service
38 * @param f Feature to wrap
39 * @param s FeaturesService to look up dependencies
41 ChildAwareFeatureWrapper(Feature f, FeaturesService s) throws Exception {
42 super(s.getFeature(f.getName(), f.getVersion()));
43 Preconditions.checkNotNull(s, "FeatureWrapper requires non-null FeatureService in constructor");
44 this.featuresService = s;
47 protected FeaturesService getFeaturesService() {
48 return featuresService;
52 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
53 * from the underlying Feature Config files and those of its children recursively
55 public LinkedHashSet <? extends ChildAwareFeatureWrapper> getChildFeatures() throws Exception {
56 List<Dependency> dependencies = feature.getDependencies();
57 LinkedHashSet <ChildAwareFeatureWrapper> childFeatures = new LinkedHashSet<ChildAwareFeatureWrapper>();
58 if(dependencies != null) {
59 for(Dependency dependency: dependencies) {
60 Feature fi = extractFeatureFromDependency(dependency);
62 if(featuresService.getFeature(fi.getName(), fi.getVersion()) == null) {
63 LOG.warn("Feature: {}, {} is missing from features service. Skipping", fi.getName(), fi.getVersion());
65 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi,featuresService);
66 childFeatures.add(wrappedFeature);
75 public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
76 LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<FeatureConfigSnapshotHolder>();
77 for(ChildAwareFeatureWrapper c: getChildFeatures()) {
78 for(FeatureConfigSnapshotHolder h: c.getFeatureConfigSnapshotHolders()) {
79 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(h.getFileInfo());
80 if(featureConfigSnapshotHolder.isPresent()) {
81 snapShotHolders.add(featureConfigSnapshotHolder.get());
85 snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
86 return snapShotHolders;
89 protected Feature extractFeatureFromDependency(Dependency dependency) throws Exception {
90 Feature[] features = featuresService.listFeatures();
91 VersionRange range = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION.equals(dependency.getVersion())
92 ? VersionRange.ANY_VERSION : new VersionRange(dependency.getVersion(), true, true);
94 for(Feature f: features) {
95 if (f.getName().equals(dependency.getName())) {
96 Version v = VersionTable.getVersion(f.getVersion());
97 if (range.contains(v) &&
98 (fi == null || VersionTable.getVersion(fi.getVersion()).compareTo(v) < 0)) {