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