diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2016-04-10 22:06:36 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2016-04-12 10:59:14 +0200 |
commit | 6dcea1de979536dbe0478e31f17191bcfae92f3c (patch) | |
tree | a5070e77f1de0c17476a33aeaaa68fc21ba4315a /v3po/impl/src/main/java/io | |
parent | 4ebc67b58e0172bd4f5704e52a10714ee64d7d07 (diff) |
HONEYCOMB-34: Configurable serializer dependency
Change-Id: I05cb7836ec4a379f7617710a6c6f64c3541ff0c9
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'v3po/impl/src/main/java/io')
-rw-r--r-- | v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/V3poProvider.java | 18 | ||||
-rw-r--r-- | v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppDataBrokerInitializationProvider.java | 151 |
2 files changed, 123 insertions, 46 deletions
diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/V3poProvider.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/V3poProvider.java index 973d34585..d07c0ffca 100644 --- a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/V3poProvider.java +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/V3poProvider.java @@ -56,6 +56,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev 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.VxlanTunnel; +import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.openvpp.vppjapi.vppApi; import org.openvpp.vppjapi.vppInterfaceDetails; @@ -67,6 +68,7 @@ public class V3poProvider implements BindingAwareProvider, AutoCloseable, Broker private static final Logger LOG = LoggerFactory.getLogger(V3poProvider.class); private final Broker domBroker; + private final BindingNormalizedNodeSerializer serializer; private RpcRegistration<V3poService> v3poService; private VppIetfInterfaceListener vppInterfaceListener; private VppBridgeDomainListener vppBridgeDomainListener; @@ -77,13 +79,15 @@ public class V3poProvider implements BindingAwareProvider, AutoCloseable, Broker VppPollOperDataImpl vppPollOperData; private VppDataBrokerInitializationProvider vppDataBrokerInitializationProvider; - public V3poProvider(@Nonnull final Broker domBroker, final vppApi vppJapiDependency, - final ReaderRegistry readerRegistry, - final WriterRegistry writerRegistry) { - api = vppJapiDependency; - this.readerRegistry = readerRegistry; - this.writerRegistry = writerRegistry; + public V3poProvider(@Nonnull final Broker domBroker, final vppApi vppJapi, + @Nonnull final ReaderRegistry readerRegistry, + @Nonnull final WriterRegistry writerRegistry, + @Nonnull final BindingNormalizedNodeSerializer serializer) { + api = vppJapi; + this.readerRegistry = Preconditions.checkNotNull(readerRegistry, "readerRegistry should not be null"); + this.writerRegistry = Preconditions.checkNotNull(writerRegistry, "writerRegistry should not be null"); this.domBroker = Preconditions.checkNotNull(domBroker, "domBroker should not be null"); + this.serializer = Preconditions.checkNotNull(serializer, "serializer should not be null"); } private void initializeVppConfig() { @@ -194,7 +198,7 @@ public class V3poProvider implements BindingAwareProvider, AutoCloseable, Broker startOperationalUpdateTimer(); // TODO make configurable: - vppDataBrokerInitializationProvider = new VppDataBrokerInitializationProvider(db, readerRegistry, writerRegistry); + vppDataBrokerInitializationProvider = new VppDataBrokerInitializationProvider(db, readerRegistry, writerRegistry, serializer); // TODO pull the registration into Module domBroker.registerProvider(vppDataBrokerInitializationProvider); } diff --git a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppDataBrokerInitializationProvider.java b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppDataBrokerInitializationProvider.java index 58195ba3f..b0ee447ba 100644 --- a/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppDataBrokerInitializationProvider.java +++ b/v3po/impl/src/main/java/io/fd/honeycomb/v3po/impl/VppDataBrokerInitializationProvider.java @@ -26,13 +26,18 @@ import com.google.common.util.concurrent.ListenableFuture; import io.fd.honeycomb.v3po.data.ModifiableDataTree; import io.fd.honeycomb.v3po.data.ReadableDataTree; import io.fd.honeycomb.v3po.data.impl.ConfigDataTree; -import io.fd.honeycomb.v3po.data.impl.OperationalDataTree; import io.fd.honeycomb.v3po.data.impl.DataBroker; +import io.fd.honeycomb.v3po.data.impl.OperationalDataTree; +import io.fd.honeycomb.v3po.translate.Context; +import io.fd.honeycomb.v3po.translate.TranslationException; +import io.fd.honeycomb.v3po.translate.read.ReadContext; import io.fd.honeycomb.v3po.translate.read.ReaderRegistry; import io.fd.honeycomb.v3po.translate.write.WriterRegistry; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import javassist.ClassPool; +import java.util.List; +import java.util.Map; import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; @@ -47,9 +52,12 @@ import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.sal.core.api.Broker; import org.opendaylight.controller.sal.core.api.Provider; import org.opendaylight.controller.sal.core.api.model.SchemaService; -import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer; +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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId; @@ -58,18 +66,16 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator; -import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator; -import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry; +import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; import org.opendaylight.yangtools.concepts.ObjectRegistration; -import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy; -import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext; -import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils; +import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; 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.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -90,15 +96,19 @@ public final class VppDataBrokerInitializationProvider implements Provider, Auto private final ReaderRegistry readerRegistry; private final InstanceIdentifier<Node> mountPointPath; private final WriterRegistry writerRegistry; + private final BindingNormalizedNodeSerializer serializer; private ObjectRegistration<DOMMountPoint> mountPointRegistration; private DOMDataBroker broker; - public VppDataBrokerInitializationProvider(@Nonnull final org.opendaylight.controller.md.sal.binding.api.DataBroker bindingBroker, - final ReaderRegistry readerRegistry, - final WriterRegistry writerRegistry) { + public VppDataBrokerInitializationProvider( + @Nonnull final org.opendaylight.controller.md.sal.binding.api.DataBroker bindingBroker, + final ReaderRegistry readerRegistry, + final WriterRegistry writerRegistry, + final BindingNormalizedNodeSerializer serializer) { this.bindingBroker = checkNotNull(bindingBroker, "bindingBroker should not be null"); this.readerRegistry = checkNotNull(readerRegistry, "readerRegistry should not be null"); this.writerRegistry = checkNotNull(writerRegistry, "writerRegistry should not be null"); + this.serializer = checkNotNull(serializer, "serializer should not be null"); this.mountPointPath = getMountPointPath(); } @@ -120,7 +130,7 @@ public final class VppDataBrokerInitializationProvider implements Provider, Auto final SchemaService schemaService = providerSession.getService(SchemaService.class); final SchemaContext globalContext = schemaService.getGlobalContext(); - final BindingNormalizedNodeSerializer serializer = initSerializer(globalContext); + // final BindingNormalizedNodeSerializer serializer = initSerializer(globalContext); final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(mountPointPath); final DOMMountPointService.DOMMountPointBuilder mountPointBuilder = mountPointService.createMountPoint(path); @@ -158,43 +168,106 @@ public final class VppDataBrokerInitializationProvider implements Provider, Auto } } - private BindingNormalizedNodeSerializer initSerializer(final SchemaContext globalContext) { - final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault()); - // TODO this produces ClassNotFoundException - //final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(); - - // FIXME get global class loader instance - final GeneratedClassLoadingStrategy loadingStrategy = - new GeneratedClassLoadingStrategy() { - @Override - public Class<?> loadClass(final String fullyQualifiedName) - throws ClassNotFoundException { - return Class.forName(fullyQualifiedName); - } - }; - final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(utils); - - // TODO make configurable: - final BindingNormalizedNodeCodecRegistry serializer = new BindingNormalizedNodeCodecRegistry(generator); - final BindingRuntimeContext context = BindingRuntimeContext.create(loadingStrategy, globalContext); - serializer.onBindingRuntimeContextUpdated(context); - return serializer; - } - private DOMDataBroker initVppDataBroker(final SchemaContext globalContext, final BindingNormalizedNodeSerializer serializer) { - final ReadableDataTree operationalData = + final ReadableDataTree operationalDataTree = new OperationalDataTree(serializer, globalContext, readerRegistry); // TODO make configurable final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION); // TODO make configurable dataTree.setSchemaContext(globalContext); - final ModifiableDataTree configDataProxy = + final ModifiableDataTree configDataTree = new ConfigDataTree(serializer, dataTree, writerRegistry); // TODO make configurable - return new DataBroker(operationalData, configDataProxy); + + // init operational data tree before data broker is initialized + + try { + initConfig(serializer, configDataTree); + } catch (Exception e) { + LOG.warn("Failed to initialize config", e); + } + + return new DataBroker(operationalDataTree, configDataTree); } + private void initConfig(final BindingNormalizedNodeSerializer serializer, final ModifiableDataTree configDataTree) + throws TranslationException, DataValidationFailedException { + LOG.info("Config initialization"); + + final Optional<? extends DataObject> data = readerRegistry.read(InstanceIdentifier.create(VppState.class), new ReadContextImpl()); + LOG.info("Config initialization data={}", data); + + if (data.isPresent()) { + // conversion + VppState vppOperationalData = (VppState) data.get(); + final Vpp vppConfigData = convert(vppOperationalData); + + final Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedData = + serializer.toNormalizedNode(InstanceIdentifier.create(Vpp.class), vppConfigData); + + final DataTreeModification modification = configDataTree.takeSnapshot().newModification(); + final YangInstanceIdentifier biPath = normalizedData.getKey(); + final NormalizedNode<?, ?> biData = normalizedData.getValue(); + LOG.info("Config initialization biPath={}, biData={}", biPath, biData); + modification.write(biPath, biData); + modification.ready(); + + LOG.info("Config writing modification ..."); + configDataTree.modify(modification); // TODO do not write to VPP + LOG.info("Config writing modification written successfully."); + } else { + LOG.info("Data is not present"); + } + } + + private Vpp convert(final VppState vppState) { + final BridgeDomains bridgeDomains = vppState.getBridgeDomains(); + final List<BridgeDomain> bridgeDomainList = bridgeDomains.getBridgeDomain(); + + VppBuilder vppBuilder = new VppBuilder(); + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomainsBuilder bdsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomainsBuilder(); + final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain> + listOfBDs = new ArrayList<>(); + + // TODO use reflexions + for (BridgeDomain bd : bridgeDomainList) { + org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder bdBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder(); + bdBuilder.setLearn(bd.isLearn()); + bdBuilder.setUnknownUnicastFlood(bd.isUnknownUnicastFlood()); + bdBuilder.setArpTermination(bd.isArpTermination()); + bdBuilder.setFlood(bd.isFlood()); + bdBuilder.setForward(bd.isForward()); + bdBuilder.setKey(new BridgeDomainKey(bd.getKey().getName())); + // TODO bdBuilder.setL2Fib(bd.getL2Fib()); + bdBuilder.setName(bd.getName()); + listOfBDs.add(bdBuilder.build()); + } + + bdsBuilder.setBridgeDomain(listOfBDs); + vppBuilder.setBridgeDomains(bdsBuilder.build()); + return vppBuilder.build(); + } + + + // 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; + } + + @Override + public void close() { + // Make sure to clear the storage in case some customizer stored it to prevent memory leaks + ctx.close(); + } + } + + /** * Writes placeholder data into MD-SAL's global datastore to indicate the presence of VPP mountpoint. */ |