Merge "Remove raw references to Map in XSQL"
[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.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;
22
23 /*
24  * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
25  * its underlying ConfigFileInfo's and those of its children recursively
26  *
27  * Delegates the the contained feature and provides additional methods.
28  */
29 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
30     private static final Logger LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
31     private FeaturesService featuresService= null;
32
33     protected ChildAwareFeatureWrapper(Feature f) {
34         // Don't use without a feature service
35     }
36
37     /*
38      * @param f Feature to wrap
39      * @param s FeaturesService to look up dependencies
40      */
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;
45     }
46
47     protected FeaturesService getFeaturesService() {
48         return featuresService;
49     }
50
51     /*
52      * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
53      * from the underlying Feature Config files and those of its children recursively
54      */
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);
61                 if(fi != null){
62                     ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi,featuresService);
63                     childFeatures.add(wrappedFeature);
64                 }
65             }
66         }
67         return childFeatures;
68     }
69
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;
75                 try {
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());
80                 }
81             }
82         }
83         snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
84         return snapShotHolders;
85     }
86
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);
91         Feature fi = null;
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)) {
97                     fi = f;
98                     break;
99                 }
100             }
101         }
102         return fi;
103     }
104
105 }