9e3806b13bc10d6fb91d9ed478594b211e40a10f
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / AbstractFeatureWrapper.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 com.google.common.io.Files;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import javax.xml.bind.JAXBException;
16 import javax.xml.stream.XMLStreamException;
17 import org.apache.karaf.features.BundleInfo;
18 import org.apache.karaf.features.Conditional;
19 import org.apache.karaf.features.ConfigFileInfo;
20 import org.apache.karaf.features.ConfigInfo;
21 import org.apache.karaf.features.Dependency;
22 import org.apache.karaf.features.Feature;
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
29  *
30  * Delegates the the contained feature and provides additional methods.
31  */
32 public class AbstractFeatureWrapper implements Feature {
33     private static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureWrapper.class);
34
35     protected static final String CONFIG_FILE_SUFFIX = "xml";
36
37     protected Feature feature = null;
38
39     protected AbstractFeatureWrapper() {
40         // prevent instantiation without Feature
41     }
42
43     /*
44      * @param f Feature to wrap
45      */
46     public AbstractFeatureWrapper(final Feature f) {
47         Preconditions.checkNotNull(f,"FeatureWrapper requires non-null Feature in constructor");
48         this.feature = f;
49     }
50
51     /*
52      * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
53      * from the underlying Feature Config files
54      */
55     public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
56         final LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
57         for(final ConfigFileInfo c: getConfigurationFiles()) {
58             // Skip non xml files
59             if(isConfigXMLFile(c.getFinalname())) {
60                 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(c);
61                 if(featureConfigSnapshotHolder.isPresent()) {
62                     snapShotHolders.add(featureConfigSnapshotHolder.get());
63                 }
64             }
65         }
66         return snapShotHolders;
67     }
68
69     private static boolean isConfigXMLFile(String fullName) {
70         return Files.getFileExtension(fullName).equals(CONFIG_FILE_SUFFIX);
71     }
72
73     protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
74         try {
75             return Optional.of(new FeatureConfigSnapshotHolder(c, this));
76         } catch (final JAXBException e) {
77             LOG.warn("Unable to parse configuration snapshot. Config from '{}' will be IGNORED. " +
78                             "Note that subsequent config files may fail due to this problem. " +
79                             "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
80                     c.getFinalname(), e);
81         } catch (final XMLStreamException e) {
82             // Files that cannot be loaded are ignored as non config subsystem files e.g. jetty.xml
83             LOG.debug("Unable to read configuration file '{}'. Not a configuration snapshot",
84                     c.getFinalname(), e);
85         }
86         return Optional.absent();
87     }
88
89     @Override
90     public int hashCode() {
91         final int prime = 31;
92         int result = 1;
93         result = prime * result + (feature == null ? 0 : feature.hashCode());
94         return result;
95     }
96
97     @Override
98     public boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (obj == null) {
103             return false;
104         }
105         if (getClass() != obj.getClass()) {
106             return false;
107         }
108         final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
109         if (feature == null) {
110             if (other.feature != null) {
111                 return false;
112             }
113         } else if (!feature.equals(other.feature)) {
114             return false;
115         }
116         return true;
117     }
118
119     @Override
120     public String toString() {
121         return feature.getName();
122     }
123
124     /**
125      * @return
126      * @see org.apache.karaf.features.Feature#getId()
127      */
128     @Override
129     public String getId() {
130         return feature.getId();
131     }
132
133     /**
134      * @return
135      * @see org.apache.karaf.features.Feature#getName()
136      */
137     @Override
138     public String getName() {
139         return feature.getName();
140     }
141
142     /**
143      * @return
144      * @see org.apache.karaf.features.Feature#getDescription()
145      */
146     @Override
147     public String getDescription() {
148         return feature.getDescription();
149     }
150
151     /**
152      * @return
153      * @see org.apache.karaf.features.Feature#getDetails()
154      */
155     @Override
156     public String getDetails() {
157         return feature.getDetails();
158     }
159
160     /**
161      * @return
162      * @see org.apache.karaf.features.Feature#getVersion()
163      */
164     @Override
165     public String getVersion() {
166         return feature.getVersion();
167     }
168
169     /**
170      * @return
171      * @see org.apache.karaf.features.Feature#hasVersion()
172      */
173     @Override
174     public boolean hasVersion() {
175         return feature.hasVersion();
176     }
177
178     /**
179      * @return
180      * @see org.apache.karaf.features.Feature#getResolver()
181      */
182     @Override
183     public String getResolver() {
184         return feature.getResolver();
185     }
186
187     /**
188      * @return
189      * @see org.apache.karaf.features.Feature#getInstall()
190      */
191     @Override
192     public String getInstall() {
193         return feature.getInstall();
194     }
195
196     /**
197      * @return
198      * @see org.apache.karaf.features.Feature#getDependencies()
199      */
200     @Override
201     public List<Dependency> getDependencies() {
202         return feature.getDependencies();
203     }
204
205     /**
206      * @return
207      * @see org.apache.karaf.features.Feature#getBundles()
208      */
209     @Override
210     public List<BundleInfo> getBundles() {
211         return feature.getBundles();
212     }
213
214     /**
215      * @return
216      * @see org.apache.karaf.features.Feature#getConfigurations()
217      */
218     @Override
219     public List<ConfigInfo> getConfigurations() {
220         return feature.getConfigurations();
221     }
222
223     /**
224      * @return
225      * @see org.apache.karaf.features.Feature#getConfigurationFiles()
226      */
227     @Override
228     public List<ConfigFileInfo> getConfigurationFiles() {
229         return feature.getConfigurationFiles();
230     }
231
232     /**
233      * @return
234      * @see org.apache.karaf.features.Feature#getConditional()
235      */
236     @Override
237     public List<? extends Conditional> getConditional() {
238         return feature.getConditional();
239     }
240
241     /**
242      * @return
243      * @see org.apache.karaf.features.Feature#getStartLevel()
244      */
245     @Override
246     public int getStartLevel() {
247         return feature.getStartLevel();
248     }
249
250     /**
251      * @return
252      * @see org.apache.karaf.features.Feature#getRegion()
253      */
254     @Override
255     public String getRegion() {
256         return feature.getRegion();
257     }
258
259 }