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.Preconditions;
11 import java.util.LinkedHashSet;
12 import java.util.List;
13 import javax.xml.bind.JAXBException;
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 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi,featuresService);
63 childFeatures.add(wrappedFeature);
70 public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
71 LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<FeatureConfigSnapshotHolder>();
72 for(ChildAwareFeatureWrapper c: getChildFeatures()) {
73 for(FeatureConfigSnapshotHolder h: c.getFeatureConfigSnapshotHolders()) {
74 FeatureConfigSnapshotHolder f;
76 f = new FeatureConfigSnapshotHolder(h,this);
77 snapShotHolders.add(f);
78 } catch (JAXBException e) {
79 LOG.debug("{} is not a config subsystem config file",h.getFileInfo().getFinalname());
83 snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
84 return snapShotHolders;
87 protected Feature extractFeatureFromDependency(Dependency dependency) throws Exception {
88 Feature[] features = featuresService.listFeatures();
89 VersionRange range = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION.equals(dependency.getVersion())
90 ? VersionRange.ANY_VERSION : new VersionRange(dependency.getVersion(), true, true);
92 for(Feature f: features) {
93 if (f.getName().equals(dependency.getName())) {
94 Version v = VersionTable.getVersion(f.getVersion());
95 if (range.contains(v) &&
96 (fi == null || VersionTable.getVersion(fi.getVersion()).compareTo(v) < 0)) {