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;
15 import org.apache.felix.utils.version.VersionRange;
16 import org.apache.felix.utils.version.VersionTable;
17 import org.apache.karaf.features.Dependency;
18 import org.apache.karaf.features.Feature;
19 import org.apache.karaf.features.FeaturesService;
20 import org.osgi.framework.Version;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
26 * its underlying ConfigFileInfo's and those of its children recursively
28 * Delegates the the contained feature and provides additional methods.
30 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
31 private static final Logger LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
32 private FeaturesService featuresService = null;
34 protected ChildAwareFeatureWrapper(final Feature feature) {
35 // Don't use without a feature service
40 * @param feature Feature to wrap
41 * @param featuresService FeaturesService to look up dependencies
43 ChildAwareFeatureWrapper(final Feature feature, final FeaturesService featuresService) throws Exception {
44 super(featuresService.getFeature(feature.getName(), feature.getVersion()));
45 Preconditions.checkNotNull(featuresService, "FeatureWrapper requires non-null FeatureService in constructor");
46 this.featuresService = featuresService;
49 protected FeaturesService getFeaturesService() {
50 return featuresService;
54 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
55 * from the underlying Feature Config files and those of its children recursively
57 public Set<? extends ChildAwareFeatureWrapper> getChildFeatures() throws Exception {
58 List<Dependency> dependencies = feature.getDependencies();
59 Set<ChildAwareFeatureWrapper> childFeatures = new LinkedHashSet<>();
60 if (dependencies != null) {
61 for (Dependency dependency : dependencies) {
62 Feature fi = extractFeatureFromDependency(dependency);
64 if (featuresService.getFeature(fi.getName(), fi.getVersion()) == null) {
65 LOG.warn("Feature: {}, {} is missing from features service. Skipping", fi.getName(), fi
68 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi, featuresService);
69 childFeatures.add(wrappedFeature);
78 public Set<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
79 Set<FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
80 for (ChildAwareFeatureWrapper c : getChildFeatures()) {
81 for (FeatureConfigSnapshotHolder h : c.getFeatureConfigSnapshotHolders()) {
82 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder =
83 getFeatureConfigSnapshotHolder(h.getFileInfo());
84 if (featureConfigSnapshotHolder.isPresent()) {
85 snapShotHolders.add(featureConfigSnapshotHolder.get());
89 snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
90 return snapShotHolders;
93 protected Feature extractFeatureFromDependency(final Dependency dependency) throws Exception {
94 Feature[] features = featuresService.listFeatures();
95 VersionRange range = dependency.hasVersion() ? new VersionRange(dependency.getVersion(), true, true)
96 : VersionRange.ANY_VERSION;
98 for (Feature f : features) {
99 if (f.getName().equals(dependency.getName())) {
100 Version version = VersionTable.getVersion(f.getVersion());
101 if (range.contains(version) && (fi == null || VersionTable.getVersion(fi.getVersion())
102 .compareTo(version) < 0)) {