From 3edb738db9c756d42ee5c0b6fa172b6f2caa808a Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 11 Apr 2016 21:23:57 +0200 Subject: HONEYCOMB-34: Config tree initialization using binding data broker Change-Id: I070aca2cc35dd10ea5bde19c8cbf4cad1c50f468 Signed-off-by: Marek Gradzki --- .../vpp/data/init/AbstractDataTreeConverter.java | 99 ++++++++-------------- .../v3po/vpp/data/init/VppInitializer.java | 11 +-- 2 files changed, 38 insertions(+), 72 deletions(-) (limited to 'v3po/vpp-cfg-init/src/main/java/io') diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/AbstractDataTreeConverter.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/AbstractDataTreeConverter.java index 3048dc18b..bf5a77d4b 100644 --- a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/AbstractDataTreeConverter.java +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/AbstractDataTreeConverter.java @@ -20,20 +20,14 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.Beta; import com.google.common.base.Optional; -import io.fd.honeycomb.v3po.data.ModifiableDataTree; -import io.fd.honeycomb.v3po.translate.Context; -import io.fd.honeycomb.v3po.translate.read.ReadContext; -import io.fd.honeycomb.v3po.translate.read.ReadFailedException; -import io.fd.honeycomb.v3po.translate.read.ReaderRegistry; -import java.util.Map; -import javax.annotation.Nonnull; -import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; +import com.google.common.util.concurrent.CheckedFuture; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,20 +42,14 @@ public abstract class AbstractDataTreeConverter idOper; private final InstanceIdentifier idConfig; + private final DataBroker bindingDataBroker; - public AbstractDataTreeConverter(@Nonnull final ReaderRegistry readerRegistry, - @Nonnull final ModifiableDataTree configDataTree, - @Nonnull final BindingNormalizedNodeSerializer serializer, - @Nonnull final InstanceIdentifier idOper, - @Nonnull final InstanceIdentifier idConfig) { - this.readerRegistry = checkNotNull(readerRegistry, "readerRegistry should not be null"); - this.configDataTree = checkNotNull(configDataTree, "configDataTree should not be null"); - this.serializer = checkNotNull(serializer, "serializer should not be null"); + public AbstractDataTreeConverter(final DataBroker bindingDataBroker, + final InstanceIdentifier idOper, + final InstanceIdentifier idConfig) { + this.bindingDataBroker = checkNotNull(bindingDataBroker, "bindingDataBroker should not be null"); this.idOper = checkNotNull(idOper, "idOper should not be null"); this.idConfig = checkNotNull(idConfig, "idConfig should not be null"); } @@ -69,59 +57,42 @@ public abstract class AbstractDataTreeConverter data; - try (ReadContext ctx = new ReadContextImpl()) { - data = readerRegistry.read(idOper, ctx); - } catch (ReadFailedException e) { - LOG.warn("Failed to read operational state", e); - return; - } - LOG.debug("Config initialization data={}", data); + final Optional data = readData(); if (data.isPresent()) { - // conversion - final O operationalData = idOper.getTargetType().cast(data.get()); - final C configData = convert(operationalData); + LOG.debug("Config initialization data={}", data); - final Map.Entry> normalizedData = - serializer.toNormalizedNode(idConfig, configData); - - final DataTreeModification modification = configDataTree.takeSnapshot().newModification(); - final YangInstanceIdentifier biPath = normalizedData.getKey(); - final NormalizedNode biData = normalizedData.getValue(); - LOG.debug("Config initialization biPath={}, biData={}", biPath, biData); - modification.write(biPath, biData); - modification.ready(); + final O operationalData = data.get(); + final C configData = convert(operationalData); - LOG.debug("Config writing modification ..."); try { - configDataTree.initialize(modification); - LOG.debug("Config writing modification written successfully."); - } catch (DataValidationFailedException e) { - throw new InitializeException("Failed to read operational state", e); + LOG.debug("Initializing config with data={}", configData); + writeData(configData); + LOG.debug("Config initialization successful"); + } catch (TransactionCommitFailedException e) { + throw new InitializeException("Failed to perform config initialization", e); } } else { LOG.warn("Data is not present"); } } - protected abstract C convert(final O operationalData); - - // TODO move to utility module - private static final class ReadContextImpl implements ReadContext { - public final Context ctx = new Context(); - - @Nonnull - @Override - public Context getContext() { - return ctx; + private Optional readData() { + try (ReadOnlyTransaction readTx = bindingDataBroker.newReadOnlyTransaction()) { + final CheckedFuture, org.opendaylight.controller.md.sal.common.api.data.ReadFailedException> + readFuture = readTx.read(LogicalDatastoreType.OPERATIONAL, idOper); + return readFuture.checkedGet(); + } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) { + LOG.warn("Failed to read operational state", e); } + return Optional.absent(); + } - @Override - public void close() { - // Make sure to clear the storage in case some customizer stored it to prevent memory leaks - ctx.close(); - } + private void writeData(final C configData) throws TransactionCommitFailedException { + final WriteTransaction writeTx = bindingDataBroker.newWriteOnlyTransaction(); + writeTx.merge(LogicalDatastoreType.CONFIGURATION, idConfig, configData); + writeTx.submit().checkedGet(); } + + protected abstract C convert(final O operationalData); } diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java index c0316c0b8..3fad2a8d3 100644 --- a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java @@ -16,18 +16,16 @@ package io.fd.honeycomb.v3po.vpp.data.init; -import io.fd.honeycomb.v3po.data.ModifiableDataTree; -import io.fd.honeycomb.v3po.translate.read.ReaderRegistry; import java.util.ArrayList; import java.util.List; import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Vpp; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppState; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomains; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain; -import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,10 +33,8 @@ import org.slf4j.LoggerFactory; public class VppInitializer extends AbstractDataTreeConverter { private static final Logger LOG = LoggerFactory.getLogger(VppInitializer.class); - public VppInitializer(@Nonnull final ReaderRegistry readerRegistry, - @Nonnull final ModifiableDataTree configDataTree, - @Nonnull final BindingNormalizedNodeSerializer serializer) { - super(readerRegistry, configDataTree, serializer, InstanceIdentifier.create(VppState.class), InstanceIdentifier.create(Vpp.class) ); + public VppInitializer(@Nonnull final DataBroker bindingDataBroker) { + super(bindingDataBroker, InstanceIdentifier.create(VppState.class), InstanceIdentifier.create(Vpp.class)); } @Override @@ -61,7 +57,6 @@ public class VppInitializer extends AbstractDataTreeConverter { final List listOfBDs = new ArrayList<>(); - // TODO use reflexion for (BridgeDomain bd : bridgeDomainList) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder bdBuilder = -- cgit 1.2.3-korg