Merge changes I6029bd48,Ie8eb4152
[controller.git] / opendaylight / config / config-persister-directory-xml-adapter / src / main / java / org / opendaylight / controller / config / persist / storage / directory / xml / XmlDirectoryPersister.java
1 /*
2  * Copyright (c) 2013 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.config.persist.storage.directory.xml;
9
10 import com.google.common.base.Optional;
11 import com.google.common.io.Files;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.config.persist.api.Persister;
14 import org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import javax.xml.bind.JAXBContext;
19 import javax.xml.bind.JAXBException;
20 import javax.xml.bind.Unmarshaller;
21 import java.io.File;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.SortedSet;
30
31 import static com.google.common.base.Preconditions.checkArgument;
32
33 public class XmlDirectoryPersister implements Persister {
34     private static final Logger logger = LoggerFactory.getLogger(XmlDirectoryPersister.class);
35
36     private final File storage;
37     private final Optional<FilenameFilter> extensionsFilter;
38
39     /**
40      * Creates XmlDirectoryPersister that picks up all files in specified folder
41      */
42     public XmlDirectoryPersister(File storage) {
43         this(storage, Optional.<FilenameFilter>absent());
44     }
45
46     /**
47      * Creates XmlDirectoryPersister that picks up files only with specified file extension
48      */
49     public XmlDirectoryPersister(File storage, Set<String> fileExtensions) {
50         this(storage, Optional.of(getFilter(fileExtensions)));
51     }
52
53     private XmlDirectoryPersister(File storage, Optional<FilenameFilter> extensionsFilter) {
54         checkArgument(storage.exists() && storage.isDirectory(), "Storage directory does not exist: " + storage);
55         this.storage = storage;
56         this.extensionsFilter = extensionsFilter;
57     }
58
59     @Override
60     public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
61         throw new UnsupportedOperationException("This adapter is read only. Please set readonly=true on " + getClass());
62     }
63
64     @Override
65     public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
66         File[] filesArray = extensionsFilter.isPresent() ? storage.listFiles(extensionsFilter.get()) : storage.listFiles();
67         if (filesArray == null || filesArray.length == 0) {
68             return Collections.emptyList();
69         }
70         List<File> sortedFiles = new ArrayList<>(Arrays.asList(filesArray));
71         Collections.sort(sortedFiles);
72         // combine all found files
73         logger.debug("Reading files in following order: {}", sortedFiles);
74
75         List<ConfigSnapshotHolder> result = new ArrayList<>();
76         for (File file : sortedFiles) {
77             logger.trace("Adding file '{}' to combined result", file);
78             ConfigSnapshotHolder h = fromXmlSnapshot(file);
79             result.add(h);
80         }
81         return result;
82     }
83
84     private ConfigSnapshotHolder fromXmlSnapshot(File file) {
85         try {
86             return loadLastConfig(file);
87         } catch (JAXBException e) {
88             logger.warn("Unable to restore configuration snapshot from {}", file, e);
89             throw new IllegalStateException("Unable to restore configuration snapshot from " + file, e);
90         }
91     }
92
93     public static ConfigSnapshotHolder loadLastConfig(File file) throws JAXBException {
94         JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
95         Unmarshaller um = jaxbContext.createUnmarshaller();
96
97         return asHolder((ConfigSnapshot) um.unmarshal(file));
98     }
99
100     private static ConfigSnapshotHolder asHolder(final ConfigSnapshot unmarshalled) {
101         return new ConfigSnapshotHolder() {
102             @Override
103             public String getConfigSnapshot() {
104                 return unmarshalled.getConfigSnapshot();
105             }
106
107             @Override
108             public SortedSet<String> getCapabilities() {
109                 return unmarshalled.getCapabilities();
110             }
111
112             @Override
113             public String toString() {
114                 return unmarshalled.toString();
115             }
116         };
117     }
118
119     private static FilenameFilter getFilter(final Set<String>fileExtensions) {
120         checkArgument(fileExtensions.isEmpty() == false, "No file extension provided", fileExtensions);
121
122         return new FilenameFilter() {
123             @Override
124             public boolean accept(File dir, String name) {
125                 String ext = Files.getFileExtension(name);
126                 return fileExtensions.contains(ext);
127             }
128         };
129     }
130
131     @Override
132     public void close() {
133
134     }
135
136     @Override
137     public String toString() {
138         final StringBuffer sb = new StringBuffer("XmlDirectoryPersister{");
139         sb.append("storage=").append(storage);
140         sb.append('}');
141         return sb.toString();
142     }
143 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.