Merge "Add better error reporting to ConfigSnapshot parser"
authorTony Tkacik <ttkacik@cisco.com>
Wed, 7 Jan 2015 09:38:38 +0000 (09:38 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 7 Jan 2015 09:38:39 +0000 (09:38 +0000)
opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigSnapshot.java
opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/SnapshotHandler.java
opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigTest.java [new file with mode: 0644]
opendaylight/config/config-persister-file-xml-adapter/src/test/resources/illegalSnapshot.xml [new file with mode: 0644]

index 4f050640c5db829949a9909745da3e0bdc3ae5d1..589a644f3ddcd308f3d708c35e841be59e5cdfaa 100644 (file)
@@ -36,6 +36,7 @@ public class ConfigSnapshot {
         return new ConfigSnapshot(cfg.getConfigSnapshot(), cfg.getCapabilities());
     }
 
+
     @XmlAnyElement(SnapshotHandler.class)
     public String getConfigSnapshot() {
         return configSnapshot;
index 8214b369702d4c1441975b6d13c019d00a306d95..dacc35b83e4a7c8dfa6bb635bf97f936e6861391 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.config.persist.storage.file.xml.model;
 
+import com.google.common.base.Preconditions;
 import java.io.StringReader;
 import java.io.StringWriter;
 import javax.xml.bind.ValidationEventHandler;
@@ -31,6 +32,8 @@ class SnapshotHandler implements DomHandler<String, StreamResult> {
         String xml = rt.getWriter().toString();
         int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
         int endIndex = xml.indexOf(END_TAG);
+        Preconditions.checkArgument(beginIndex != -1 && endIndex != -1,
+                "Unknown element present in config snapshot(expected only configuration): %s", xml);
         return xml.substring(beginIndex, endIndex);
     }
 
diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigTest.java b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigTest.java
new file mode 100644 (file)
index 0000000..23b7abe
--- /dev/null
@@ -0,0 +1,12 @@
+package org.opendaylight.controller.config.persist.storage.file.xml.model;
+
+import java.io.File;
+import org.junit.Test;
+
+public class ConfigTest {
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testFromXml() throws Exception {
+        Config.fromXml(new File(getClass().getResource("/illegalSnapshot.xml").getFile()));
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/test/resources/illegalSnapshot.xml b/opendaylight/config/config-persister-file-xml-adapter/src/test/resources/illegalSnapshot.xml
new file mode 100644 (file)
index 0000000..f3fd13a
--- /dev/null
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vi: set et smarttab sw=4 tabstop=4: -->
+<!--
+ Copyright (c) 2013 Cisco Systems, 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
+-->
+<snapshot>
+    <configuration>
+        <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
+            <modules xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
+                <module>
+                    <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:clustering-service-provider">
+                        prefix:clustering-service-provider
+                    </type>
+                    <name>clustering-service-provider</name>
+
+                    <rpc-registry>
+                        <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-rpc-registry</type>
+                        <name>binding-rpc-broker</name>
+                    </rpc-registry>
+
+                </module>
+            </modules>
+        </data>
+
+    </configuration>
+
+    <services xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
+        <service>
+            <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:clustering-service-provider">prefix:clustering-service-change-registry</type>
+            <instance>
+                <name>openflow-role-change-registry</name>
+                <provider>/modules/module[type='clustering-service-provider'][name='clustering-service-provider']</provider>
+            </instance>
+        </service>
+    </services>
+
+    <required-capabilities>
+        <capability>urn:opendaylight:params:xml:ns:yang:controller:config:clustering-service-provider?module=clustering-service-provider&amp;revision=2014-11-19</capability>
+
+    </required-capabilities>
+
+</snapshot>
\ No newline at end of file