2 * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.config.loader.impl;
11 import static java.util.Objects.requireNonNull;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URISyntaxException;
18 import java.nio.file.WatchEvent;
19 import java.nio.file.WatchKey;
20 import java.nio.file.WatchService;
21 import java.util.HashMap;
23 import java.util.regex.Pattern;
24 import javax.annotation.concurrent.GuardedBy;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.stream.XMLInputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamReader;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.protocol.bgp.config.loader.spi.ConfigFileProcessor;
31 import org.opendaylight.protocol.bgp.config.loader.spi.ConfigLoader;
32 import org.opendaylight.yangtools.concepts.AbstractRegistration;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.SAXException;
45 public final class ConfigLoaderImpl implements ConfigLoader, AutoCloseable {
46 private static final Logger LOG = LoggerFactory.getLogger(ConfigLoaderImpl.class);
47 private static final String INTERRUPTED = "InterruptedException";
48 private static final String EXTENSION = "-.*\\.xml";
49 private static final String INITIAL = "^";
51 private final Map<String, ConfigFileProcessor> configServices = new HashMap<>();
52 private final SchemaContext schemaContext;
53 private final BindingNormalizedNodeSerializer bindingSerializer;
54 private final String path;
55 private final Thread watcherThread;
57 public ConfigLoaderImpl(final SchemaContext schemaContext, final BindingNormalizedNodeSerializer bindingSerializer,
58 final String path, final WatchService watchService) {
59 this.schemaContext = requireNonNull(schemaContext);
60 this.bindingSerializer = requireNonNull(bindingSerializer);
61 this.path = requireNonNull(path);
62 requireNonNull(watchService);
63 this.watcherThread = new Thread(new ConfigLoaderImplRunnable(watchService));
64 this.watcherThread.start();
65 LOG.info("Config Loader service initiated");
68 private void handleConfigFile(final ConfigFileProcessor config, final String filename) {
69 final NormalizedNode<?, ?> dto;
71 dto = parseDefaultConfigFile(config, filename);
72 } catch (final Exception e) {
73 LOG.warn("Failed to parse config file {}", filename, e);
76 LOG.info("Loading initial config {}", filename);
77 config.loadConfiguration(dto);
80 private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename)
81 throws IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
82 final NormalizedNodeResult result = new NormalizedNodeResult();
83 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
85 final InputStream resourceAsStream = new FileInputStream(new File(this.path, filename));
86 final XMLInputFactory factory = XMLInputFactory.newInstance();
87 final XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
89 final SchemaNode schemaNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, config.getSchemaPath());
90 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, this.schemaContext, schemaNode);
91 xmlParser.parse(reader);
93 return result.getResult();
97 public synchronized AbstractRegistration registerConfigFile(final ConfigFileProcessor config) {
98 final String pattern = INITIAL + config.getSchemaPath().getLastComponent().getLocalName() + EXTENSION;
99 this.configServices.put(pattern, config);
101 final File[] fList = new File(this.path).listFiles();
103 for (final File file : fList) {
105 final String filename = file.getName();
106 if (Pattern.matches(pattern, filename)) {
107 handleConfigFile(config, filename);
113 return new AbstractRegistration() {
115 protected void removeRegistration() {
116 synchronized (ConfigLoaderImpl.this) {
117 ConfigLoaderImpl.this.configServices.remove(pattern);
124 public BindingNormalizedNodeSerializer getBindingNormalizedNodeSerializer() {
125 return this.bindingSerializer;
130 public void close() throws Exception {
131 this.watcherThread.interrupt();
134 private class ConfigLoaderImplRunnable implements Runnable {
135 private final WatchService watchService;
137 ConfigLoaderImplRunnable(final WatchService watchService) {
138 this.watchService = watchService;
144 while (!Thread.currentThread().isInterrupted()) {
145 handleChanges(this.watchService);
147 } catch (final Exception e) {
148 LOG.warn(INTERRUPTED, e);
152 private synchronized void handleChanges(final WatchService watchService) {
154 final WatchKey key = watchService.take();
156 for (final WatchEvent<?> event : key.pollEvents()) {
157 handleEvent(event.context().toString());
159 final boolean reset = key.reset();
161 LOG.warn("Could not reset the watch key.");
164 } catch (final InterruptedException e) {
165 LOG.warn(INTERRUPTED, e);
169 private synchronized void handleEvent(final String filename) {
170 ConfigLoaderImpl.this.configServices.entrySet().stream()
171 .filter(entry -> Pattern.matches(entry.getKey(), filename)).
172 forEach(entry -> handleConfigFile(entry.getValue(), filename));