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.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;
28 * Wrap a Feature for the purposes of extracting the FeatureConfigSnapshotHolders from
29 * its underlying ConfigFileInfo's
31 * Delegates the the contained feature and provides additional methods.
33 public class AbstractFeatureWrapper implements Feature {
34 private static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureWrapper.class);
36 private static final String CONFIG_FILE_PATH_SUFFIX = "opendaylight" + File.separator + "karaf";
37 protected static final String CONFIG_FILE_SUFFIX = "xml";
39 protected Feature feature = null;
41 protected AbstractFeatureWrapper() {
42 // prevent instantiation without Feature
46 * @param f Feature to wrap
48 public AbstractFeatureWrapper(final Feature f) {
49 Preconditions.checkNotNull(f,"FeatureWrapper requires non-null Feature in constructor");
54 * Get FeatureConfigSnapshotHolders appropriate to feed to the config subsystem
55 * from the underlying Feature Config files
57 public LinkedHashSet<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolders() throws Exception {
58 final LinkedHashSet <FeatureConfigSnapshotHolder> snapShotHolders = new LinkedHashSet<>();
59 for(final ConfigFileInfo c: getConfigurationFiles()) {
61 if(isConfigXMLFile(c.getFinalname())) {
62 final Optional<FeatureConfigSnapshotHolder> featureConfigSnapshotHolder = getFeatureConfigSnapshotHolder(c);
63 if(featureConfigSnapshotHolder.isPresent()) {
64 snapShotHolders.add(featureConfigSnapshotHolder.get());
68 return snapShotHolders;
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);
77 protected Optional<FeatureConfigSnapshotHolder> getFeatureConfigSnapshotHolder(final ConfigFileInfo c) {
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.",
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",
90 return Optional.absent();
94 public int hashCode() {
97 result = prime * result + (feature == null ? 0 : feature.hashCode());
102 public boolean equals(final Object obj) {
109 if (getClass() != obj.getClass()) {
112 final AbstractFeatureWrapper other = (AbstractFeatureWrapper) obj;
113 if (feature == null) {
114 if (other.feature != null) {
117 } else if (!feature.equals(other.feature)) {
124 public String toString() {
125 return feature.getName();
130 * @see org.apache.karaf.features.Feature#getId()
133 public String getId() {
134 return feature.getId();
139 * @see org.apache.karaf.features.Feature#getName()
142 public String getName() {
143 return feature.getName();
148 * @see org.apache.karaf.features.Feature#getDescription()
151 public String getDescription() {
152 return feature.getDescription();
157 * @see org.apache.karaf.features.Feature#getDetails()
160 public String getDetails() {
161 return feature.getDetails();
166 * @see org.apache.karaf.features.Feature#getVersion()
169 public String getVersion() {
170 return feature.getVersion();
175 * @see org.apache.karaf.features.Feature#hasVersion()
178 public boolean hasVersion() {
179 return feature.hasVersion();
184 * @see org.apache.karaf.features.Feature#getResolver()
187 public String getResolver() {
188 return feature.getResolver();
193 * @see org.apache.karaf.features.Feature#getInstall()
196 public String getInstall() {
197 return feature.getInstall();
202 * @see org.apache.karaf.features.Feature#getDependencies()
205 public List<Dependency> getDependencies() {
206 return feature.getDependencies();
211 * @see org.apache.karaf.features.Feature#getBundles()
214 public List<BundleInfo> getBundles() {
215 return feature.getBundles();
220 * @see org.apache.karaf.features.Feature#getConfigurations()
223 public List<ConfigInfo> getConfigurations() {
224 return feature.getConfigurations();
229 * @see org.apache.karaf.features.Feature#getConfigurationFiles()
232 public List<ConfigFileInfo> getConfigurationFiles() {
233 return feature.getConfigurationFiles();
238 * @see org.apache.karaf.features.Feature#getConditional()
241 public List<? extends Conditional> getConditional() {
242 return feature.getConditional();
247 * @see org.apache.karaf.features.Feature#getStartLevel()
250 public int getStartLevel() {
251 return feature.getStartLevel();
256 * @see org.apache.karaf.features.Feature#getRegion()
259 public String getRegion() {
260 return feature.getRegion();