Remove yang-test
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / ChildAwareFeatureWrapper.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.configpusherfeature.internal;
9
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 java.util.Set;
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;
23
24 /*
25  * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
26  * its underlying ConfigFileInfo's and those of its children recursively
27  *
28  * Delegates the the contained feature and provides additional methods.
29  */
30 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
31     private static final Logger LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
32     private FeaturesService featuresService = null;
33
34     protected ChildAwareFeatureWrapper(final Feature feature) {
35         // Don't use without a feature service
36     }
37
38     /* Constructor.
39
40      * @param feature Feature to wrap
41      * @param featuresService FeaturesService to look up dependencies
42      */
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;
47     }
48
49     protected FeaturesService getFeaturesService() {
50         return featuresService;
51     }
52
53     /*
54      * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
55      * from the underlying Feature Config files and those of its children recursively
56      */
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);
63                 if (fi != null) {
64                     if (featuresService.getFeature(fi.getName(), fi.getVersion()) == null) {
65                         LOG.warn("Feature: {}, {} is missing from features service. Skipping", fi.getName(), fi
66                                 .getVersion());
67                     } else {
68                         ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi, featuresService);
69                         childFeatures.add(wrappedFeature);
70                     }
71                 }
72             }
73         }
74         return childFeatures;
75     }
76
77     @Override
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());
86                 }
87             }
88         }
89         snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
90         return snapShotHolders;
91     }
92
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;
97         Feature fi = null;
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)) {
103                     fi = f;
104                     break;
105                 }
106             }
107         }
108         return fi;
109     }
110 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.