Bump odlparent/yangtools/mdsal
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / DataStoreAppConfigDefaultXMLReader.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
9
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;
15 import java.net.URL;
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;
34
35 /**
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.
40  *
41  * @author Thomas Pantelis (originally; re-factored by Michael Vorburger.ch)
42  */
43 public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(DataStoreAppConfigDefaultXMLReader.class);
46
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;
53
54     @FunctionalInterface
55     public interface FallbackConfigProvider {
56         NormalizedNode get(SchemaTreeInference dataSchema)
57             throws IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException;
58     }
59
60     @FunctionalInterface
61     public interface ConfigURLProvider {
62         Optional<URL> getURL(String appConfigFileName) throws IOException;
63     }
64
65     public DataStoreAppConfigDefaultXMLReader(
66             final String logName,
67             final String defaultAppConfigFileName,
68             final DOMSchemaService schemaService,
69             final BindingNormalizedNodeSerializer bindingSerializer,
70             final BindingContext bindingContext,
71             final ConfigURLProvider inputStreamProvider) {
72
73         this.logName = logName;
74         this.defaultAppConfigFileName = defaultAppConfigFileName;
75         this.schemaService = schemaService;
76         this.bindingSerializer = bindingSerializer;
77         this.bindingContext = bindingContext;
78         this.inputStreamProvider = inputStreamProvider;
79     }
80
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)));
90     }
91
92     private static URL getURL(final Class<?> testClass, final String defaultAppConfigFileName) {
93         return Resources.getResource(testClass, defaultAppConfigFileName);
94     }
95
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)");
101         });
102     }
103
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);
108
109         LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath,
110                 bindingContext.bindingQName);
111
112         checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
113
114         EffectiveModelContext schemaContext = schemaService.getGlobalContext();
115
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());
119
120         final SchemaInferenceStack schemaStack = SchemaInferenceStack.of(schemaContext);
121         final SchemaTreeEffectiveStatement<?> dataSchema;
122         try {
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);
127         }
128
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());
132
133         NormalizedNode dataNode = parsePossibleDefaultAppConfigXMLFile(schemaStack);
134         if (dataNode == null) {
135             dataNode = fallback.get(schemaStack.toSchemaTreeInference());
136         }
137
138         DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
139
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);
143
144         return (T) appConfig;
145     }
146
147     private static void checkNotNull(final Object reference, final String errorMessageFormat,
148             final Object... formatArgs) throws ConfigXMLReaderException {
149         checkCondition(reference != null, errorMessageFormat, formatArgs);
150     }
151
152     private static void checkCondition(final boolean expression, final String errorMessageFormat,
153             final Object... formatArgs) throws ConfigXMLReaderException {
154         if (!expression) {
155             throw new ConfigXMLReaderException(String.format(errorMessageFormat, formatArgs));
156         }
157     }
158
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();
164
165             appConfigFileName = moduleName + "_" + bindingContext.bindingQName.getLocalName() + ".xml";
166         }
167
168         Optional<URL> optionalURL;
169         try {
170             optionalURL = inputStreamProvider.getURL(appConfigFileName);
171         } catch (final IOException e) {
172             String msg = String.format("%s: Could not getURL()", logName);
173             LOG.error(msg, e);
174             throw new ConfigXMLReaderException(msg, e);
175         }
176         if (!optionalURL.isPresent()) {
177             return null;
178         }
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());
184
185             LOG.debug("{}: Parsed data node: {}", logName, dataNode);
186
187             return 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);
191             LOG.error(msg, e);
192             throw new ConfigXMLReaderException(msg, e);
193         }
194     }
195 }