From 04c868333214c0e5bfce3926c43a4302615f2ac5 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 26 Oct 2016 12:18:11 +0200 Subject: Honeycomb-73 Extensible initializers framework Change-Id: Ib23453d4040d59a512686315995a5cf9e532cefc Signed-off-by: Maros Marsalek --- .../translate/impl/read/GenericInitListReader.java | 82 ++++++++++++++++++++++ .../translate/impl/read/GenericInitReader.java | 73 +++++++++++++++++++ .../translate/impl/read/GenericListReader.java | 7 +- .../translate/impl/read/GenericReader.java | 12 ++-- 4 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java create mode 100644 infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java (limited to 'infra/translate-impl') diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java new file mode 100644 index 000000000..cabf31633 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitListReader.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read; + +import static io.fd.honeycomb.translate.impl.read.GenericInitReader.writeInit; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.InitListReader; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; +import io.fd.honeycomb.translate.util.RWUtils; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class GenericInitListReader, K extends Identifier, B extends Builder> + extends GenericListReader + implements InitListReader { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitListReader.class); + + public GenericInitListReader(@Nonnull final InstanceIdentifier id, + @Nonnull final InitializingListReaderCustomizer customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + for (K k : getAllIds(id, ctx)) { + initSingle(broker, RWUtils.replaceLastInId(id, RWUtils.getCurrentIdItem(id, k)), ctx); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } + + private void initSingle(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized init = + ((InitializingListReaderCustomizer) customizer).init(id, operational.get(), ctx); + LOG.debug("{}: Writing init config : {} at: {}", GenericInitListReader.this, init.getData(), init.getId()); + writeInit(broker, init); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java new file mode 100644 index 000000000..024e0a815 --- /dev/null +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericInitReader.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.impl.read; + +import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.Initializer; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class GenericInitReader> + extends GenericReader + implements Initializer { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitReader.class); + + public GenericInitReader(@Nonnull final InstanceIdentifier id, + @Nonnull final InitializingReaderCustomizer customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier id, final ReadContext ctx) throws + InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized init = + ((InitializingReaderCustomizer) customizer).init(id, operational.get(), ctx); + LOG.debug("{}: Writing init config : {} at: {}", GenericInitReader.this, init.getData(), init.getId()); + writeInit(broker, init); + } + } catch (ReadFailedException e) { + LOG.warn("{}: Failed to initialize current, unable to read: {}", this, id, e); + throw new InitFailedException(e.getFailedId(), e); + } + } + + @SuppressWarnings("unchecked") + static void writeInit(final DataBroker broker, final Initialized init) { + final WriteTransaction writeTx = broker.newWriteOnlyTransaction(); + writeTx.merge(CONFIGURATION, (InstanceIdentifier) init.getId(), init.getData(), true); + writeTx.submit(); + } +} diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java index d8accec91..be6c1b638 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericListReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Cisco and/or its affiliates. + * Copyright (c) {today.year} Cisco and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,12 +47,12 @@ import org.slf4j.LoggerFactory; */ @Beta @ThreadSafe -public final class GenericListReader, K extends Identifier, B extends Builder> +public class GenericListReader, K extends Identifier, B extends Builder> extends AbstractGenericReader implements ListReader { private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class); - private final ListReaderCustomizer customizer; + protected final ListReaderCustomizer customizer; /** * Create new {@link GenericListReader} @@ -113,6 +113,7 @@ public final class GenericListReader, K e } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier id) { return customizer.getBuilder(id); diff --git a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java index f60c8f137..e76b6e9a3 100644 --- a/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java +++ b/infra/translate-impl/src/main/java/io/fd/honeycomb/translate/impl/read/GenericReader.java @@ -18,9 +18,9 @@ package io.fd.honeycomb.translate.impl.read; import com.google.common.annotations.Beta; import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.read.Reader; import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; -import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.util.read.AbstractGenericReader; import javax.annotation.Nonnull; import javax.annotation.concurrent.ThreadSafe; @@ -33,15 +33,16 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ @Beta @ThreadSafe -public final class GenericReader> extends AbstractGenericReader - implements Reader { +public class GenericReader> + extends AbstractGenericReader + implements Reader { - private final ReaderCustomizer customizer; + protected final ReaderCustomizer customizer; /** * Create a new {@link GenericReader}. * - * @param id Instance identifier for managed data type + * @param id Instance identifier for managed data type * @param customizer Customizer instance to customize this generic reader */ public GenericReader(@Nonnull final InstanceIdentifier id, @@ -61,6 +62,7 @@ public final class GenericReader> ext } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier id) { return customizer.getBuilder(id); -- cgit 1.2.3-korg