Merge "BUG-672 Make Xml directory adapter skip initial config files with invalid...
[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             Optional<ConfigSnapshotHolder> h = fromXmlSnapshot(file);
79             // Ignore non valid snapshot
80             if(h.isPresent() == false) {
81                 continue;
82             }
83
84             result.add(h.get());
85         }
86         return result;
87     }
88
89     private Optional<ConfigSnapshotHolder> fromXmlSnapshot(File file) {
90         try {
91             return Optional.of(loadLastConfig(file));
92         } catch (JAXBException e) {
93             // In case of parse error, issue a warning, ignore and continue
94             logger.warn(
95                     "Unable to parse configuration snapshot from {}. Initial config from {} will be IGNORED in this run. " +
96                     "Note that subsequent config files may fail due to this problem. " +
97                     "Xml markup in this file needs to be fixed, for detailed information see enclosed exception.",
98                     file, file, e);
99         }
100
101         return Optional.absent();
102     }
103
104     public static ConfigSnapshotHolder loadLastConfig(File file) throws JAXBException {
105         JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
106         Unmarshaller um = jaxbContext.createUnmarshaller();
107
108         return asHolder((ConfigSnapshot) um.unmarshal(file));
109     }
110
111     private static ConfigSnapshotHolder asHolder(final ConfigSnapshot unmarshalled) {
112         return new ConfigSnapshotHolder() {
113             @Override
114             public String getConfigSnapshot() {
115                 return unmarshalled.getConfigSnapshot();
116             }
117
118             @Override
119             public SortedSet<String> getCapabilities() {
120                 return unmarshalled.getCapabilities();
121             }
122
123             @Override
124             public String toString() {
125                 return unmarshalled.toString();
126             }
127         };
128     }
129
130     private static FilenameFilter getFilter(final Set<String>fileExtensions) {
131         checkArgument(fileExtensions.isEmpty() == false, "No file extension provided", fileExtensions);
132
133         return new FilenameFilter() {
134             @Override
135             public boolean accept(File dir, String name) {
136                 String ext = Files.getFileExtension(name);
137                 return fileExtensions.contains(ext);
138             }
139         };
140     }
141
142     @Override
143     public void close() {
144
145     }
146
147     @Override
148     public String toString() {
149         final StringBuffer sb = new StringBuffer("XmlDirectoryPersister{");
150         sb.append("storage=").append(storage);
151         sb.append('}');
152         return sb.toString();
153     }
154 }

©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.