2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.configpusherfeature.internal;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.io.Files;
14 import java.io.FileInputStream;
15 import java.util.LinkedHashSet;
16 import java.util.List;
18 import javax.xml.bind.JAXBException;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.stream.XMLStreamException;
21 import org.apache.karaf.features.BundleInfo;
22 import org.apache.karaf.features.Capability;
23 import org.apache.karaf.features.Conditional;
24 import org.apache.karaf.features.ConfigFileInfo;
25 import org.apache.karaf.features.ConfigInfo;
26 import org.apache.karaf.features.Dependency;
27 import org.apache.karaf.features.Feature;
28 import org.apache.karaf.features.Library;
29 import org.apache.karaf.features.Requirement;
30 import org.apache.karaf.features.Scoping;
31 import org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Element;
37 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
38 * its underlying ConfigFileInfo's
40 * Delegates the the contained feature and provides additional methods.
42 public class AbstractFeatureWrapper implements Feature {
43 private static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureWrapper.class);
45 protected static final String CONFIG_FILE_SUFFIX = "xml";
47 protected Feature feature = null;
49 protected AbstractFeatureWrapper() {
50 // prevent instantiation without Feature
54 * @param f Feature to wrap
56 public AbstractFeatureWrapper(final Feature f) {
57 Preconditions.checkNotNull(f,"FeatureWrapper requires non-null Feature in constructor");
62 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
63 * from the underlying Feature Config files
65 public Set<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
66 final Set<FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
67 for(final ConfigFileInfo c: getConfigurationFiles()) {
68 // Skip non config snapshot XML files
69 if(isConfigSnapshot(c.getFinalname())) {
70 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(c);
71 if(featureConfigSnapshotHolder.isPresent()) {
72 snapShotHolders.add(featureConfigSnapshotHolder.get());
76 return snapShotHolders;
79 protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
81 return Optional.of(new FeatureConfigSnapshotHolder(c, this));
82 } catch (final JAXBException e) {
83 LOG.warn("Unable to parse configuration snapshot. Config from '{}' will be IGNORED. " +
84 "Note that subsequent config files may fail due to this problem. " +
85 "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
87 } catch (final XMLStreamException e) {
88 // Files that cannot be loaded are ignored as non config subsystem files e.g. jetty.xml
89 LOG.debug("Unable to read configuration file '{}'. Not a configuration snapshot",
92 return Optional.absent();
95 private static boolean isConfigSnapshot(String fileName) {
96 if(!Files.getFileExtension(fileName).equals(CONFIG_FILE_SUFFIX)) {
100 if(fileName.endsWith("jetty.xml")) {
101 // Special case - ignore the jetty.xml file as it contains a DTD and causes a "Connection refused"
102 // error when it tries to go out to the network to retrieve it. We don't want it trying to go out
103 // to the network nor do we want an error logged trying to parse it.
107 File file = new File(System.getProperty("karaf.home"), fileName);
108 try(FileInputStream fis = new FileInputStream(file)) {
109 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
110 builderFactory.setNamespaceAware(true);
111 builderFactory.setCoalescing(true);
112 builderFactory.setIgnoringElementContentWhitespace(true);
113 builderFactory.setIgnoringComments(true);
115 Element root = builderFactory.newDocumentBuilder().parse(fis).getDocumentElement();
116 return ConfigSnapshot.SNAPSHOT_ROOT_ELEMENT_NAME.equals(root.getLocalName());
117 } catch (Exception e) {
118 LOG.error("Could not parse XML file {}", file, e);
124 public int hashCode() {
125 final int prime = 31;
127 result = prime * result + (feature == null ? 0 : feature.hashCode());
132 public boolean equals(final Object obj) {
139 if (getClass() != obj.getClass()) {
142 final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
143 if (feature == null) {
144 if (other.feature != null) {
147 } else if (!feature.equals(other.feature)) {
154 public String toString() {
155 return feature.getName();
159 public String getId() {
160 return feature.getId();
164 public String getName() {
165 return feature.getName();
169 public String getDescription() {
170 return feature.getDescription();
174 public String getDetails() {
175 return feature.getDetails();
179 public String getVersion() {
180 return feature.getVersion();
184 public boolean hasVersion() {
185 return feature.hasVersion();
189 public String getResolver() {
190 return feature.getResolver();
194 public String getInstall() {
195 return feature.getInstall();
199 public List<Dependency> getDependencies() {
200 return feature.getDependencies();
204 public List<BundleInfo> getBundles() {
205 return feature.getBundles();
209 public List<ConfigInfo> getConfigurations() {
210 return feature.getConfigurations();
214 public List<ConfigFileInfo> getConfigurationFiles() {
215 return feature.getConfigurationFiles();
219 public List<? extends Conditional> getConditional() {
220 return feature.getConditional();
224 public int getStartLevel() {
225 return feature.getStartLevel();
229 public List<? extends Capability> getCapabilities() {
230 return feature.getCapabilities();
234 public List<? extends Library> getLibraries() {
235 return feature.getLibraries();
239 public String getNamespace() {
240 return feature.getNamespace();
244 public String getRepositoryUrl() {
245 return feature.getRepositoryUrl();
249 public List<? extends Requirement> getRequirements() {
250 return feature.getRequirements();
254 public List<String> getResourceRepositories() {
255 return feature.getResourceRepositories();
259 public Scoping getScoping() {
260 return feature.getScoping();
264 public boolean isHidden() {
265 return feature.isHidden();