read UpgradeConfig from XML configuration file
authorMichael Vorburger <mike@vorburger.ch>
Wed, 31 Oct 2018 20:16:58 +0000 (21:16 +0100)
committerMichael Vorburger <mike@vorburger.ch>
Wed, 1 Jul 2020 23:37:38 +0000 (01:37 +0200)
Signed-off-by: Michael Vorburger <mike@vorburger.ch>
TODO.md
pom.xml
src/main/java/org/opendaylight/controller/simple/ConfigReader.java [new file with mode: 0644]
src/main/java/org/opendaylight/serviceutils/simple/UpgradeWiring.java
src/test/java/org/opendaylight/serviceutils/simple/test/ServiceUtilsModuleTest.java

diff --git a/TODO.md b/TODO.md
index fc9e550ef5df593cade59755a4a1822a03e17b56..2691a53b53b2a4408b29535e2a74b789baae4144 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -13,7 +13,7 @@
 
 - [X] working /diagstatus and showSvcStatus (#39) [waiting for infrautils merges]
 
-- [ ] RestConfWiring with Web API
+- [X] RestConfWiring with Web API
 
 - [ ] make RestConfConfig readable from YAML using http://immutables.github.io/json.html and update @ConfigImmutableStyle for it, then upstream to infrautils
 
@@ -43,7 +43,7 @@
 - [ ] create a Binding Generator? (reflecting upon annotated classes)
 - [X] [re-implement ClassPathScanner](https://github.com/vorburger/opendaylight-simple/pull/18#issuecomment-426859615) using [ClassGraph](https://github.com/classgraph/classgraph) used in [INFRAUTILS-52](https://jira.opendaylight.org/browse/INFRAUTILS-52) (and rename it to something more appropriate)
 
-- [ ] read YANG XML configuration files using [DataStoreAppConfigDefaultXMLReader](https://git.opendaylight.org/gerrit/#/c/76416/3/opendaylight/blueprint/src/test/java/org/opendaylight/controller/blueprint/tests/DataStoreAppConfigDefaultXMLReaderTest.java)
+- [X] read YANG XML configuration files using [DataStoreAppConfigDefaultXMLReader](https://git.opendaylight.org/gerrit/#/c/76416/3/opendaylight/blueprint/src/test/java/org/opendaylight/controller/blueprint/tests/DataStoreAppConfigDefaultXMLReaderTest.java)
 
 - [ ] run genius CSIT
 
diff --git a/pom.xml b/pom.xml
index 05247ce309a033e75ef828b0e8e6d765064f58c7..6e0817cc54c35bd71baf221b51d5c3946eeac7e4 100644 (file)
--- a/pom.xml
+++ b/pom.xml
           <groupId>org.iq80.leveldb</groupId>
           <artifactId>leveldb-api</artifactId>
         </exclusion>
-        <exclusion>
-          <groupId>org.opendaylight.controller</groupId>
-          <artifactId>blueprint</artifactId>
-        </exclusion>
       </exclusions>
     </dependency>
 
diff --git a/src/main/java/org/opendaylight/controller/simple/ConfigReader.java b/src/main/java/org/opendaylight/controller/simple/ConfigReader.java
new file mode 100644 (file)
index 0000000..8121540
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.simple;
+
+import com.google.common.io.Resources;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
+import org.opendaylight.controller.blueprint.ext.BindingContext;
+import org.opendaylight.controller.blueprint.ext.ConfigXMLReaderException;
+import org.opendaylight.controller.blueprint.ext.DataStoreAppConfigDefaultXMLReader;
+import org.opendaylight.controller.blueprint.ext.DataStoreAppConfigDefaultXMLReader.ConfigURLProvider;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.xml.sax.SAXException;
+
+/**
+ * Reads YAML configuration files.
+ *
+ * @author Michael Vorburger.ch
+ */
+@Singleton
+public class ConfigReader {
+
+    // TODO this is currently only tested by the ServiceUtilsModuleTest; create a dedicated test when upstreamed
+
+    private final DOMSchemaService schemaService;
+    private final BindingNormalizedNodeSerializer bindingSerializer;
+
+    @Inject
+    public ConfigReader(DOMSchemaService schemaService, BindingNormalizedNodeSerializer bindingSerializer) {
+        this.schemaService = schemaService;
+        this.bindingSerializer = bindingSerializer;
+    }
+
+    public <T extends DataObject> T read(String resourcePathWithoutExtension, Class<T> yangType) {
+        String xmlResourcePath = resourcePathWithoutExtension + ".xml";
+        ConfigURLProvider configURLProvider = appConfigFileName -> Optional
+                .of(Resources.getResource(ConfigReader.class, xmlResourcePath));
+        try {
+            return new DataStoreAppConfigDefaultXMLReader<T>(xmlResourcePath, xmlResourcePath, schemaService,
+                    bindingSerializer, BindingContext.create(yangType.getName(), yangType, null), configURLProvider)
+                            .createDefaultInstance();
+        } catch (ConfigXMLReaderException | ParserConfigurationException | XMLStreamException | IOException
+                | SAXException | URISyntaxException e) {
+            throw new IllegalArgumentException(
+                    "Resource not found on the classpath, or it's invalid: " + xmlResourcePath, e);
+        }
+    }
+}
index 44229b41aa9583f4d019aeb1d09af7577cc0e403..e674063a7f59e92cf5693404796225857d3b4933 100644 (file)
@@ -15,7 +15,6 @@ import org.opendaylight.controller.simple.ConfigReader;
 import org.opendaylight.serviceutils.upgrade.UpgradeState;
 import org.opendaylight.serviceutils.upgrade.impl.UpgradeStateListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.upgrade.rev180702.UpgradeConfig;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.upgrade.rev180702.UpgradeConfigBuilder;
 
 /**
  * Guice Module for the Upgrades API.
@@ -30,7 +29,7 @@ public class UpgradeWiring extends AbstractModule {
 
     @Provides
     @Singleton UpgradeConfig getUpgradeConfig(ConfigReader configReader) {
-        return new UpgradeConfigBuilder().setUpgradeInProgress(false).build();
+        return configReader.read("/initial/serviceutils-upgrade-config", UpgradeConfig.class);
     }
 
     @Provides
index cb358284b4c407b9a675fd71b9daefa7ca726ad4..4ae44fa274ee2c1d1ddc77c2db5f6aef156b66b7 100644 (file)
@@ -23,7 +23,8 @@ import org.opendaylight.serviceutils.upgrade.UpgradeState;
  */
 public class ServiceUtilsModuleTest extends AbstractSimpleDistributionTest {
 
-    public @Rule GuiceRule guice = new GuiceRule(ServiceUtilsWiring.class, ControllerWiring.class, AnnotationsModule.class);
+    public @Rule GuiceRule guice = new GuiceRule(ServiceUtilsWiring.class, ControllerWiring.class,
+            AnnotationsModule.class);
 
     @Inject UpgradeState upgradeState;