2 * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
10 import com.google.common.base.Strings;
11 import com.google.common.io.Resources;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URISyntaxException;
16 import java.util.Optional;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.stream.XMLStreamException;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.yangtools.util.xml.UntrustedXML;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.xml.sax.SAXException;
36 * DataObject XML file reader used by {@link DataStoreAppConfigMetadata}.
37 * Available as a standalone class to make it easy to write unit tests which can
38 * catch malformed default "clustered-app-conf" config data XML files in
39 * downstream projects.
41 * @author Thomas Pantelis (originally; re-factored by Michael Vorburger.ch)
43 public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
45 private static final Logger LOG = LoggerFactory.getLogger(DataStoreAppConfigDefaultXMLReader.class);
47 private final String logName;
48 private final String defaultAppConfigFileName;
49 private final DOMSchemaService schemaService;
50 private final BindingNormalizedNodeSerializer bindingSerializer;
51 private final BindingContext bindingContext;
52 private final ConfigURLProvider inputStreamProvider;
55 public interface FallbackConfigProvider {
56 NormalizedNode get(SchemaTreeInference dataSchema)
57 throws IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException;
61 public interface ConfigURLProvider {
62 Optional<URL> getURL(String appConfigFileName) throws IOException;
65 public DataStoreAppConfigDefaultXMLReader(
67 final String defaultAppConfigFileName,
68 final DOMSchemaService schemaService,
69 final BindingNormalizedNodeSerializer bindingSerializer,
70 final BindingContext bindingContext,
71 final ConfigURLProvider inputStreamProvider) {
73 this.logName = logName;
74 this.defaultAppConfigFileName = defaultAppConfigFileName;
75 this.schemaService = schemaService;
76 this.bindingSerializer = bindingSerializer;
77 this.bindingContext = bindingContext;
78 this.inputStreamProvider = inputStreamProvider;
81 public DataStoreAppConfigDefaultXMLReader(
82 final Class<?> testClass,
83 final String defaultAppConfigFileName,
84 final DOMSchemaService schemaService,
85 final BindingNormalizedNodeSerializer bindingSerializer,
86 final Class<T> klass) {
87 this(testClass.getName(), defaultAppConfigFileName, schemaService, bindingSerializer,
88 BindingContext.create(testClass.getName(), klass, null),
89 appConfigFileName -> Optional.of(getURL(testClass, defaultAppConfigFileName)));
92 private static URL getURL(final Class<?> testClass, final String defaultAppConfigFileName) {
93 return Resources.getResource(testClass, defaultAppConfigFileName);
96 public T createDefaultInstance() throws ConfigXMLReaderException, ParserConfigurationException, XMLStreamException,
97 IOException, SAXException, URISyntaxException {
98 return createDefaultInstance(dataSchema -> {
99 throw new IllegalArgumentException(
100 "Failed to read XML (not creating model from defaults as runtime would, for better clarity in tests)");
104 @SuppressWarnings("unchecked")
105 public T createDefaultInstance(final FallbackConfigProvider fallback) throws ConfigXMLReaderException,
106 URISyntaxException, ParserConfigurationException, XMLStreamException, SAXException, IOException {
107 YangInstanceIdentifier yangPath = bindingSerializer.toYangInstanceIdentifier(bindingContext.appConfigPath);
109 LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath,
110 bindingContext.bindingQName);
112 checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
114 EffectiveModelContext schemaContext = schemaService.getGlobalContext();
116 Module module = schemaContext.findModule(bindingContext.bindingQName.getModule()).orElse(null);
117 checkNotNull(module, "%s: Could not obtain the module schema for namespace %s, revision %s",
118 logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision());
120 final SchemaInferenceStack schemaStack = SchemaInferenceStack.of(schemaContext);
121 final SchemaTreeEffectiveStatement<?> dataSchema;
123 dataSchema = schemaStack.enterSchemaTree(bindingContext.bindingQName);
124 } catch (IllegalArgumentException e) {
125 throw new ConfigXMLReaderException(
126 logName + ": Could not obtain the schema for " + bindingContext.bindingQName, e);
129 checkCondition(bindingContext.schemaType.isInstance(dataSchema),
130 "%s: Expected schema type %s for %s but actual type is %s", logName,
131 bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass());
133 NormalizedNode dataNode = parsePossibleDefaultAppConfigXMLFile(schemaStack);
134 if (dataNode == null) {
135 dataNode = fallback.get(schemaStack.toSchemaTreeInference());
138 DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
140 // This shouldn't happen but need to handle it in case...
141 checkNotNull(appConfig, "%s: Could not create instance for app config binding %s", logName,
142 bindingContext.appConfigBindingClass);
144 return (T) appConfig;
147 private static void checkNotNull(final Object reference, final String errorMessageFormat,
148 final Object... formatArgs) throws ConfigXMLReaderException {
149 checkCondition(reference != null, errorMessageFormat, formatArgs);
152 private static void checkCondition(final boolean expression, final String errorMessageFormat,
153 final Object... formatArgs) throws ConfigXMLReaderException {
155 throw new ConfigXMLReaderException(String.format(errorMessageFormat, formatArgs));
159 private NormalizedNode parsePossibleDefaultAppConfigXMLFile(final SchemaInferenceStack schemaStack)
160 throws ConfigXMLReaderException {
161 String appConfigFileName = defaultAppConfigFileName;
162 if (Strings.isNullOrEmpty(appConfigFileName)) {
163 String moduleName = schemaStack.currentModule().argument().getLocalName();
165 appConfigFileName = moduleName + "_" + bindingContext.bindingQName.getLocalName() + ".xml";
168 Optional<URL> optionalURL;
170 optionalURL = inputStreamProvider.getURL(appConfigFileName);
171 } catch (final IOException e) {
172 String msg = String.format("%s: Could not getURL()", logName);
174 throw new ConfigXMLReaderException(msg, e);
176 if (!optionalURL.isPresent()) {
179 URL url = optionalURL.get();
180 try (InputStream is = url.openStream()) {
181 Document root = UntrustedXML.newDocumentBuilder().parse(is);
182 NormalizedNode dataNode = bindingContext.parseDataElement(root.getDocumentElement(),
183 schemaStack.toSchemaTreeInference());
185 LOG.debug("{}: Parsed data node: {}", logName, dataNode);
188 } catch (final IOException | SAXException | XMLStreamException | ParserConfigurationException
189 | URISyntaxException e) {
190 String msg = String.format("%s: Could not read/parse app config %s", logName, url);
192 throw new ConfigXMLReaderException(msg, e);