From fdacbae34f18c8fa74b68f82b9b4e24e6343c5b3 Mon Sep 17 00:00:00 2001 From: Tomas Olvecky Date: Fri, 27 Jun 2014 11:38:37 +0200 Subject: [PATCH] Bug:1253 - Fix NPE in config pusher Add better error message when ConfigSnapshotHolder contract is voilated. Fix org.opendaylight.controller.config.persist.storage.file.xml.model.ConfigSnapshot to return empty set instead of null in getCapabilities() Change-Id: I9783cccbe53ae69e322e1d5660ad1f3b52b007fa Signed-off-by: Tomas Olvecky --- .../persist/api/ConfigSnapshotHolder.java | 3 ++- .../file/xml/model/ConfigSnapshot.java | 3 ++- .../netconf/persist/impl/ConfigPusher.java | 23 +++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/opendaylight/config/config-persister-api/src/main/java/org/opendaylight/controller/config/persist/api/ConfigSnapshotHolder.java b/opendaylight/config/config-persister-api/src/main/java/org/opendaylight/controller/config/persist/api/ConfigSnapshotHolder.java index c00b26d9ce..ba13ebb8f0 100644 --- a/opendaylight/config/config-persister-api/src/main/java/org/opendaylight/controller/config/persist/api/ConfigSnapshotHolder.java +++ b/opendaylight/config/config-persister-api/src/main/java/org/opendaylight/controller/config/persist/api/ConfigSnapshotHolder.java @@ -12,13 +12,14 @@ import java.util.SortedSet; public interface ConfigSnapshotHolder { /** - * Get part of get-config document that contains just + * Get XML node that should be pushed to netconf's edit-config */ String getConfigSnapshot(); /** * Get only required capabilities referenced by the snapshot. + * If no value is specified, return empty set instead of null */ SortedSet getCapabilities(); diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigSnapshot.java b/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigSnapshot.java index d13052a7e2..7c069dab6b 100644 --- a/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigSnapshot.java +++ b/opendaylight/config/config-persister-file-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/file/xml/model/ConfigSnapshot.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.config.persist.storage.file.xml.model; +import java.util.TreeSet; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import javax.xml.bind.annotation.XmlAnyElement; @@ -22,7 +23,7 @@ public class ConfigSnapshot { public static final String SNAPSHOT_ROOT_ELEMENT_NAME = "snapshot"; private String configSnapshot; - private SortedSet capabilities; + private SortedSet capabilities = new TreeSet<>(); ConfigSnapshot(String configXml, SortedSet capabilities) { this.configSnapshot = configXml; diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java index 8d85a35bc7..eddad8b4c7 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java @@ -8,6 +8,11 @@ package org.opendaylight.controller.netconf.persist.impl; +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Function; +import com.google.common.base.Stopwatch; +import com.google.common.collect.Collections2; import java.io.IOException; import java.io.InputStream; import java.util.Collection; @@ -16,11 +21,10 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; +import java.util.SortedSet; import java.util.TreeMap; import java.util.concurrent.TimeUnit; - import javax.annotation.concurrent.Immutable; - import org.opendaylight.controller.config.api.ConflictingVersionException; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; @@ -41,11 +45,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.base.Stopwatch; -import com.google.common.collect.Collections2; - @Immutable public class ConfigPusher { private static final Logger logger = LoggerFactory.getLogger(ConfigPusher.class); @@ -84,7 +83,11 @@ public class ConfigPusher { ConflictingVersionException lastException; Stopwatch stopwatch = new Stopwatch().start(); do { - try (NetconfOperationService operationService = getOperationServiceWithRetries(configSnapshotHolder.getCapabilities(), configSnapshotHolder.toString())) { + String idForReporting = configSnapshotHolder.toString(); + SortedSet expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(), + "Expected capabilities must not be null - %s, check %s", idForReporting, + configSnapshotHolder.getClass().getName()); + try (NetconfOperationService operationService = getOperationServiceWithRetries(expectedCapabilities, idForReporting)) { return pushConfig(configSnapshotHolder, operationService); } catch (ConflictingVersionException e) { lastException = e; @@ -252,7 +255,7 @@ public class ConfigPusher { private static NetconfMessage createEditConfigMessage(Element dataElement) throws NetconfDocumentedException { String editConfigResourcePath = "/netconfOp/editConfig.xml"; try (InputStream stream = ConfigPersisterNotificationHandler.class.getResourceAsStream(editConfigResourcePath)) { - Preconditions.checkNotNull(stream, "Unable to load resource " + editConfigResourcePath); + checkNotNull(stream, "Unable to load resource " + editConfigResourcePath); Document doc = XmlUtil.readXmlToDocument(stream); @@ -274,7 +277,7 @@ public class ConfigPusher { private static NetconfMessage getCommitMessage() { String resource = "/netconfOp/commit.xml"; try (InputStream stream = ConfigPusher.class.getResourceAsStream(resource)) { - Preconditions.checkNotNull(stream, "Unable to load resource " + resource); + checkNotNull(stream, "Unable to load resource " + resource); return new NetconfMessage(XmlUtil.readXmlToDocument(stream)); } catch (SAXException | IOException e) { // error reading the xml file bundled into the jar -- 2.36.6