Bug 2150: Feature wrappers now log parse errors better.
[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(Files.getFileExtension(c.getFinalname()).equals(CONFIG_FILE_SUFFIX)) {
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     protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
70         try {
71             return Optional.of(new FeatureConfigSnapshotHolder(c, this));
72         } catch (final JAXBException e) {
73             LOG.warn("Unable to parse configuration snapshot. Config from '{}' will be IGNORED. " +
74                             "Note that subsequent config files may fail due to this problem. " +
75                             "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
76                     c.getFinalname(), e);
77         } catch (final XMLStreamException e) {
78             // Files that cannot be loaded are ignored as non config subsystem files e.g. jetty.xml
79             LOG.debug("Unable to read configuration file '{}'. Not a configuration snapshot",
80                     c.getFinalname(), e);
81         }
82         return Optional.absent();
83     }
84
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result + ((feature == null) ? 0 : feature.hashCode());
90         return result;
91     }
92
93     @Override
94     public boolean equals(final Object obj) {
95         if (this == obj) {
96             return true;
97         }
98         if (obj == null) {
99             return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
105         if (feature == null) {
106             if (other.feature != null) {
107                 return false;
108             }
109         } else if (!feature.equals(other.feature)) {
110             return false;
111         }
112         return true;
113     }
114
115     @Override
116     public String toString() {
117         return feature.getName();
118     }
119
120     /**
121      * @return
122      * @see org.apache.karaf.features.Feature#getId()
123      */
124     @Override
125     public String getId() {
126         return feature.getId();
127     }
128
129     /**
130      * @return
131      * @see org.apache.karaf.features.Feature#getName()
132      */
133     @Override
134     public String getName() {
135         return feature.getName();
136     }
137
138     /**
139      * @return
140      * @see org.apache.karaf.features.Feature#getDescription()
141      */
142     @Override
143     public String getDescription() {
144         return feature.getDescription();
145     }
146
147     /**
148      * @return
149      * @see org.apache.karaf.features.Feature#getDetails()
150      */
151     @Override
152     public String getDetails() {
153         return feature.getDetails();
154     }
155
156     /**
157      * @return
158      * @see org.apache.karaf.features.Feature#getVersion()
159      */
160     @Override
161     public String getVersion() {
162         return feature.getVersion();
163     }
164
165     /**
166      * @return
167      * @see org.apache.karaf.features.Feature#hasVersion()
168      */
169     @Override
170     public boolean hasVersion() {
171         return feature.hasVersion();
172     }
173
174     /**
175      * @return
176      * @see org.apache.karaf.features.Feature#getResolver()
177      */
178     @Override
179     public String getResolver() {
180         return feature.getResolver();
181     }
182
183     /**
184      * @return
185      * @see org.apache.karaf.features.Feature#getInstall()
186      */
187     @Override
188     public String getInstall() {
189         return feature.getInstall();
190     }
191
192     /**
193      * @return
194      * @see org.apache.karaf.features.Feature#getDependencies()
195      */
196     @Override
197     public List<Dependency> getDependencies() {
198         return feature.getDependencies();
199     }
200
201     /**
202      * @return
203      * @see org.apache.karaf.features.Feature#getBundles()
204      */
205     @Override
206     public List<BundleInfo> getBundles() {
207         return feature.getBundles();
208     }
209
210     /**
211      * @return
212      * @see org.apache.karaf.features.Feature#getConfigurations()
213      */
214     @Override
215     public List<ConfigInfo> getConfigurations() {
216         return feature.getConfigurations();
217     }
218
219     /**
220      * @return
221      * @see org.apache.karaf.features.Feature#getConfigurationFiles()
222      */
223     @Override
224     public List<ConfigFileInfo> getConfigurationFiles() {
225         return feature.getConfigurationFiles();
226     }
227
228     /**
229      * @return
230      * @see org.apache.karaf.features.Feature#getConditional()
231      */
232     @Override
233     public List<? extends Conditional> getConditional() {
234         return feature.getConditional();
235     }
236
237     /**
238      * @return
239      * @see org.apache.karaf.features.Feature#getStartLevel()
240      */
241     @Override
242     public int getStartLevel() {
243         return feature.getStartLevel();
244     }
245
246     /**
247      * @return
248      * @see org.apache.karaf.features.Feature#getRegion()
249      */
250     @Override
251     public String getRegion() {
252         return feature.getRegion();
253     }
254
255 }