From ee1202f9cb83b2b9bcf6ae3f8704bbf0f39b77a5 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 18 May 2016 15:05:51 +0200 Subject: HONEYCOMB-61: Restore configuration and context from persisted files Change-Id: I6edce127f8895f5d65998b4be71a0a111ca2e8bb Signed-off-by: Maros Marsalek --- .../v3po/data/impl/PersistingDataTreeAdapter.java | 51 ++++------------------ .../honeycomb/v3po/data/impl/WriteTransaction.java | 4 +- .../impl/rev160411/InMemoryDataTreeModule.java | 1 + .../rev160411/PersistingDataTreeAdapterModule.java | 8 +++- v3po/data-impl/src/main/yang/data-impl.yang | 9 +--- 5 files changed, 19 insertions(+), 54 deletions(-) (limited to 'v3po/data-impl/src/main') diff --git a/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/PersistingDataTreeAdapter.java b/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/PersistingDataTreeAdapter.java index a54b7f148..df5bbee4b 100644 --- a/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/PersistingDataTreeAdapter.java +++ b/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/PersistingDataTreeAdapter.java @@ -18,33 +18,22 @@ package io.fd.honeycomb.v3po.data.impl; import static com.google.common.base.Preconditions.checkArgument; -import com.google.common.base.Charsets; import com.google.common.base.Optional; -import com.google.gson.stream.JsonWriter; +import io.fd.honeycomb.v3po.translate.util.JsonUtils; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import javax.annotation.Nonnull; import org.opendaylight.controller.sal.core.api.model.SchemaService; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; -import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory; -import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter; -import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,17 +71,16 @@ public class PersistingDataTreeAdapter implements org.opendaylight.yangtools.yan private Path testPersistPath(final Path persistPath) { try { checkArgument(!Files.isDirectory(persistPath), "Path %s points to a directory", persistPath); + if(Files.exists(persistPath)) { + checkArgument(Files.isReadable(persistPath), + "Provided path %s points to existing, but non-readable file", persistPath); + return persistPath; + } Files.createDirectories(persistPath.getParent()); Files.write(persistPath, new byte[]{}, StandardOpenOption.CREATE); } catch (IOException | UnsupportedOperationException e) { LOG.warn("Provided path for persistence: {} is not usable", persistPath, e); throw new IllegalArgumentException("Path " + persistPath + " cannot be used as "); - } finally { - try { - Files.delete(persistPath); - } catch (IOException e) { - LOG.warn("Unable to delete file at {}", persistPath, e); - } } return persistPath; @@ -131,17 +119,8 @@ public class PersistingDataTreeAdapter implements org.opendaylight.yangtools.yan if(Files.exists(path)) { Files.delete(path); } - // TODO once we are in static environment, do the writer, streamWriter and NNWriter initialization only once - final JsonWriter - jsonWriter = createJsonWriter(Files.newOutputStream(path, StandardOpenOption.CREATE), true); - final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter - .createNestedWriter(JSONCodecFactory.create(schemaServiceDependency.getGlobalContext()), SchemaPath.ROOT, null, jsonWriter); - final NormalizedNodeWriter normalizedNodeWriter = - NormalizedNodeWriter.forStreamWriter(streamWriter, true); - jsonWriter.beginObject(); - writeChildren(normalizedNodeWriter,(ContainerNode) currentRoot.get()); - jsonWriter.endObject(); - jsonWriter.flush(); + JsonUtils.writeJsonRoot(currentRoot.get(), schemaServiceDependency.getGlobalContext(), + Files.newOutputStream(path, StandardOpenOption.CREATE)); LOG.trace("Data persisted successfully in {}", path); } catch (IOException e) { throw new IllegalStateException("Unable to persist current data", e); @@ -151,20 +130,6 @@ public class PersistingDataTreeAdapter implements org.opendaylight.yangtools.yan } } - private void writeChildren(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException { - for(final DataContainerChild child : data.getValue()) { - nnWriter.write(child); - } - } - - private JsonWriter createJsonWriter(final OutputStream entityStream, boolean prettyPrint) { - if (prettyPrint) { - return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8), 2); - } else { - return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8)); - } - } - @Override public YangInstanceIdentifier getRootPath() { return delegateDependency.getRootPath(); diff --git a/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/WriteTransaction.java b/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/WriteTransaction.java index 3644a9fe7..ac4ba9049 100644 --- a/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/WriteTransaction.java +++ b/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/WriteTransaction.java @@ -77,11 +77,11 @@ final class WriteTransaction implements DOMDataWriteTransaction { final java.util.function.Consumer r) { switch (store) { case CONFIGURATION: - checkArgument(configModification != null, "Modification of {} is not supported", store); + checkArgument(configModification != null, "Modification of %s is not supported", store); r.accept(configModification); break; case OPERATIONAL: - checkArgument(operationalModification != null, "Modification of {} is not supported", store); + checkArgument(operationalModification != null, "Modification of %s is not supported", store); r.accept(operationalModification); break; } diff --git a/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/InMemoryDataTreeModule.java b/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/InMemoryDataTreeModule.java index 54c830ec0..956aa90f4 100644 --- a/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/InMemoryDataTreeModule.java +++ b/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/InMemoryDataTreeModule.java @@ -1,5 +1,6 @@ package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.data.impl.rev160411; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.data.api.rev160411.DatatreeType; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.tree.*; import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; diff --git a/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/PersistingDataTreeAdapterModule.java b/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/PersistingDataTreeAdapterModule.java index aa57f4d9b..145fc4345 100644 --- a/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/PersistingDataTreeAdapterModule.java +++ b/v3po/data-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/data/impl/rev160411/PersistingDataTreeAdapterModule.java @@ -1,6 +1,8 @@ package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.data.impl.rev160411; +import java.nio.file.InvalidPathException; import java.nio.file.Paths; +import org.opendaylight.controller.config.api.JmxAttributeValidationException; public class PersistingDataTreeAdapterModule extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.data.impl.rev160411.AbstractPersistingDataTreeAdapterModule { @@ -18,7 +20,11 @@ public class PersistingDataTreeAdapterModule extends @Override public void customValidation() { - // add custom validation form module attributes here. + try { + Paths.get(getPersistFilePath()); + } catch (InvalidPathException e) { + throw new JmxAttributeValidationException("Invalid persist path", e, persistFilePathJmxAttribute); + } } @Override diff --git a/v3po/data-impl/src/main/yang/data-impl.yang b/v3po/data-impl/src/main/yang/data-impl.yang index 3af9d8d3f..a6b217a84 100644 --- a/v3po/data-impl/src/main/yang/data-impl.yang +++ b/v3po/data-impl/src/main/yang/data-impl.yang @@ -24,13 +24,6 @@ module data-impl { config:java-name-prefix InMemoryDataTree; } - typedef datatree-type { - type enumeration { - enum config; - enum oper; - } - } - augment "/config:modules/config:module/config:configuration" { case inmemory-data-tree { when "/config:modules/config:module/config:type = 'inmemory-data-tree'"; @@ -45,7 +38,7 @@ module data-impl { } leaf type { - type datatree-type; + type dapi:datatree-type; } } } -- cgit 1.2.3-korg