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.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.Iterables;
13 import com.google.common.collect.Lists;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.SortedSet;
20 import javax.xml.bind.JAXBContext;
21 import javax.xml.bind.JAXBException;
22 import javax.xml.bind.Unmarshaller;
23 import org.apache.karaf.features.ConfigFileInfo;
24 import org.apache.karaf.features.Feature;
25 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
26 import org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot;
29 * A ConfigSnapshotHolder that can track all the additional information
30 * relavent to the fact we are getting these from a Feature.
32 * Includes tracking the 'featureChain' - an reverse ordered list of the dependency
33 * graph of features that caused us to push this FeatureConfigSnapshotHolder.
34 * So if A -> B -> C, then the feature chain would be C -> B -> A
36 public class FeatureConfigSnapshotHolder implements ConfigSnapshotHolder {
37 private ConfigSnapshot unmarshalled = null;
38 private ConfigFileInfo fileInfo = null;
39 private List<Feature> featureChain = new ArrayList<Feature>();
42 * @param holder - FeatureConfigSnapshotHolder that we
43 * @param feature - new
45 public FeatureConfigSnapshotHolder(final FeatureConfigSnapshotHolder holder, final Feature feature) throws JAXBException {
46 this(holder.fileInfo,holder.getFeature());
47 this.featureChain.add(feature);
51 * Create a FeatureConfigSnapshotHolder for a given ConfigFileInfo and record the associated
52 * feature we are creating it from.
53 * @param fileInfo - ConfigFileInfo to read into the ConfigSnapshot
54 * @param feature - Feature the ConfigFileInfo was attached to
56 public FeatureConfigSnapshotHolder(final ConfigFileInfo fileInfo, final Feature feature) throws JAXBException {
57 Preconditions.checkNotNull(fileInfo);
58 Preconditions.checkNotNull(fileInfo.getFinalname());
59 Preconditions.checkNotNull(feature);
60 this.fileInfo = fileInfo;
61 this.featureChain.add(feature);
62 JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
63 Unmarshaller um = jaxbContext.createUnmarshaller();
64 File file = new File(fileInfo.getFinalname());
65 unmarshalled = ((ConfigSnapshot) um.unmarshal(file));
69 * @see java.lang.Object#hashCode()
71 * We really care most about the underlying ConfigShapshot, so compute hashcode on that
74 public int hashCode() {
77 result = prime * result + ((unmarshalled != null && unmarshalled.getConfigSnapshot() == null) ? 0 : unmarshalled.getConfigSnapshot().hashCode());
82 * @see java.lang.Object#equals(java.lang.Object)
84 * We really care most about the underlying ConfigShapshot, so compute equality on that
87 public boolean equals(Object obj) {
94 if (getClass() != obj.getClass()) {
97 FeatureConfigSnapshotHolder fcsh = (FeatureConfigSnapshotHolder)obj;
98 if(this.unmarshalled.getConfigSnapshot().equals(fcsh.unmarshalled.getConfigSnapshot())) {
105 public String toString() {
106 StringBuilder b = new StringBuilder();
107 Path p = Paths.get(fileInfo.getFinalname());
108 b.append(p.getFileName())
110 .append(getCauseFeature())
112 .append(getFeature())
118 public String getConfigSnapshot() {
119 return unmarshalled.getConfigSnapshot();
123 public SortedSet<String> getCapabilities() {
124 return unmarshalled.getCapabilities();
127 public ConfigFileInfo getFileInfo() {
132 * @returns The original feature to which the ConfigFileInfo was attached
134 * A -> B -> C, ConfigFileInfo Foo is attached to C.
136 * thus C is the 'Feature' Foo was attached.
138 public Feature getFeature() {
139 return featureChain.get(0);
143 * @return The dependency chain of the features that caused the ConfigFileInfo to be pushed in reverse order.
145 * A -> B -> C, ConfigFileInfo Foo is attached to C.
146 * The returned list is
149 public ImmutableList<Feature> getFeatureChain() {
150 return ImmutableList.copyOf(Lists.reverse(featureChain));
154 * @return The feature the installation of which was the root cause
155 * of this pushing of the ConfigFileInfo.
157 * A -> B -> C, ConfigFileInfo Foo is attached to C.
159 * this A is the 'Cause' of the installation of Foo.
161 public Feature getCauseFeature() {
162 return Iterables.getLast(featureChain);