Fix checkstyle issues to enforce it
[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
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.felix.utils.version.VersionRange;
18 import org.apache.felix.utils.version.VersionTable;
19 import org.apache.karaf.features.Dependency;
20 import org.apache.karaf.features.Feature;
21 import org.apache.karaf.features.FeaturesService;
22 import org.osgi.framework.Version;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
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 LOG = LoggerFactory.getLogger(ChildAwareFeatureWrapper.class);
34     private FeaturesService featuresService = null;
35
36     protected ChildAwareFeatureWrapper(final Feature feature) {
37         // Don't use without a feature service
38     }
39
40     /* Constructor.
41
42      * @param feature Feature to wrap
43      * @param featuresService FeaturesService to look up dependencies
44      */
45     ChildAwareFeatureWrapper(final Feature feature, final FeaturesService featuresService) throws Exception {
46         super(featuresService.getFeature(feature.getName(), feature.getVersion()));
47         Preconditions.checkNotNull(featuresService, "FeatureWrapper requires non-null FeatureService in constructor");
48         this.featuresService = featuresService;
49     }
50
51     protected FeaturesService getFeaturesService() {
52         return featuresService;
53     }
54
55     /*
56      * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
57      * from the underlying Feature Config files and those of its children recursively
58      */
59     public Set<? extends ChildAwareFeatureWrapper> getChildFeatures() throws Exception {
60         List<Dependency> dependencies = feature.getDependencies();
61         Set<ChildAwareFeatureWrapper> childFeatures = new LinkedHashSet<>();
62         if (dependencies != null) {
63             for (Dependency dependency : dependencies) {
64                 Feature fi = extractFeatureFromDependency(dependency);
65                 if (fi != null) {
66                     if (featuresService.getFeature(fi.getName(), fi.getVersion()) == null) {
67                         LOG.warn("Feature: {}, {} is missing from features service. Skipping", fi.getName(), fi
68                                 .getVersion());
69                     } else {
70                         ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(fi, featuresService);
71                         childFeatures.add(wrappedFeature);
72                     }
73                 }
74             }
75         }
76         return childFeatures;
77     }
78
79     @Override
80     public Set<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
81         Set<FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
82         for (ChildAwareFeatureWrapper c : getChildFeatures()) {
83             for (FeatureConfigSnapshotHolder h : c.getFeatureConfigSnapshotHolders()) {
84                 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder =
85                         getFeatureConfigSnapshotHolder(h.getFileInfo());
86                 if (featureConfigSnapshotHolder.isPresent()) {
87                     snapShotHolders.add(featureConfigSnapshotHolder.get());
88                 }
89             }
90         }
91         snapShotHolders.addAll(super.getFeatureConfigSnapshotHolders());
92         return snapShotHolders;
93     }
94
95     protected Feature extractFeatureFromDependency(final Dependency dependency) throws Exception {
96         Feature[] features = featuresService.listFeatures();
97         VersionRange range = org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION.equals(dependency
98                 .getVersion()) ? VersionRange.ANY_VERSION : new VersionRange(dependency.getVersion(), true, true);
99         Feature fi = null;
100         for (Feature f : features) {
101             if (f.getName().equals(dependency.getName())) {
102                 Version version = VersionTable.getVersion(f.getVersion());
103                 if (range.contains(version) && (fi == null || VersionTable.getVersion(fi.getVersion())
104                         .compareTo(version) < 0)) {
105                     fi = f;
106                     break;
107                 }
108             }
109         }
110         return fi;
111     }
112 }