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 java.util.LinkedHashSet;
11 import java.util.List;
13 import javax.xml.bind.JAXBException;
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;
24 import com.google.common.base.Preconditions;
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 LOGGER = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
34 private FeaturesService featuresService= null;
36 protected ChildAwareFeatureWrapper(Feature f) {
37 // Don't use without a feature service
41 * @param f Feature to wrap
42 * @param s FeaturesService to look up dependencies
44 ChildAwareFeatureWrapper(Feature f, FeaturesService s) throws Exception {
45 super(s.getFeature(f.getName(), f.getVersion()));
46 Preconditions.checkNotNull(s, "FeatureWrapper requires non-null FeatureService in constructor");
47 this.featuresService = s;
50 protected FeaturesService getFeaturesService() {
51 return featuresService;
55 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
56 * from the underlying Feature Config files and those of its children recursively
58 public LinkedHashSet <? extends ChildAwareFeatureWrapper> getChildFeatures() throws Exception {
59 List<Dependency> dependencies = feature.getDependencies();
60 LinkedHashSet <ChildAwareFeatureWrapper> childFeatures = new LinkedHashSet<ChildAwareFeatureWrapper>();
61 if(dependencies != null) {
62 for(Dependency dependency: dependencies) {
63 Feature fi = extractFeatureFromDependency(dependency);
65 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi,featuresService);
66 childFeatures.add(wrappedFeature);
73 public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
74 LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<FeatureConfigSnapshotHolder>();
75 for(ChildAwareFeatureWrapper c: getChildFeatures()) {
76 for(FeatureConfigSnapshotHolder h: c.getFeatureConfigSnapshotHolders()) {
77 FeatureConfigSnapshotHolder f;
79 f = new FeatureConfigSnapshotHolder(h,this);
80 snapShotHolders.add(f);
81 } catch (JAXBException e) {
82 LOGGER.debug("{} is not a config subsystem config file",h.getFileInfo().getFinalname());
86 snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
87 return snapShotHolders;
90 protected Feature extractFeatureFromDependency(Dependency dependency) throws Exception {
91 Feature[] features = featuresService.listFeatures();
92 VersionRange range = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION.equals(dependency.getVersion())
93 ? VersionRange.ANY_VERSION : new VersionRange(dependency.getVersion(), true, true);
95 for(Feature f: features) {
96 if (f.getName().equals(dependency.getName())) {
97 Version v = VersionTable.getVersion(f.getVersion());
98 if (range.contains(v) &&
99 (fi == null || VersionTable.getVersion(fi.getVersion()).compareTo(v) < 0)) {