Merge "Cleanup RpcRoutingStrategy definition"
[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 java.util.LinkedHashSet;
11 import java.util.List;
12
13 import javax.xml.bind.JAXBException;
14
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 import com.google.common.base.Preconditions;
25
26 /*
27  * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
28  * its underlying ConfigFileInfo's and those of its children recursively
29  *
30  * Delegates the the contained feature and provides additional methods.
31  */
32 public class ChildAwareFeatureWrapper extends AbstractFeatureWrapper implements Feature {
33     private static final Logger LOGGER = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
34     private FeaturesService featuresService= null;
35
36     protected ChildAwareFeatureWrapper(Feature f) {
37         // Don't use without a feature service
38     }
39
40     /*
41      * @param f Feature to wrap
42      * @param s FeaturesService to look up dependencies
43      */
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;
48     }
49
50     protected FeaturesService getFeaturesService() {
51         return featuresService;
52     }
53
54     /*
55      * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
56      * from the underlying Feature Config files and those of its children recursively
57      */
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);
64                 if(fi != null){
65                     ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi,featuresService);
66                     childFeatures.add(wrappedFeature);
67                 }
68             }
69         }
70         return childFeatures;
71     }
72
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;
78                 try {
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());
83                 }
84             }
85         }
86         snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
87         return snapShotHolders;
88     }
89
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);
94         Feature fi = null;
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)) {
100                         fi = f;
101                         break;
102                 }
103             }
104         }
105         return fi;
106     }
107
108 }