diff options
Diffstat (limited to 'infra/translate-impl/src/main/java')
4 files changed, 166 insertions, 8 deletions
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<O extends DataObject & Identifiable<K>, K extends Identifier<O>, B extends Builder<O>> + extends GenericListReader<O, K, B> + implements InitListReader<O, K, B> { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitListReader.class); + + public GenericInitListReader(@Nonnull final InstanceIdentifier<O> id, + @Nonnull final InitializingListReaderCustomizer<O, K, B> customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<O> 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<O> id, final ReadContext ctx) + throws InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional<O> operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized<? extends DataObject> init = + ((InitializingListReaderCustomizer<O, K, B>) 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<O extends DataObject, B extends Builder<O>> + extends GenericReader<O, B> + implements Initializer<O> { + + private static final Logger LOG = LoggerFactory.getLogger(GenericInitReader.class); + + public GenericInitReader(@Nonnull final InstanceIdentifier<O> id, + @Nonnull final InitializingReaderCustomizer<O, B> customizer) { + super(id, customizer); + } + + @Override + public void init(final DataBroker broker, final InstanceIdentifier<O> id, final ReadContext ctx) throws + InitFailedException { + LOG.debug("{}: Initializing current: {}", this, id); + + try { + final Optional<O> operational = readCurrent(id, ctx); + if (operational.isPresent()) { + final Initialized<? extends DataObject> init = + ((InitializingReaderCustomizer<O, B>) 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<? extends DataObject> 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<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> +public class GenericListReader<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>> extends AbstractGenericReader<C, B> implements ListReader<C, K, B> { private static final Logger LOG = LoggerFactory.getLogger(GenericListReader.class); - private final ListReaderCustomizer<C, K, B> customizer; + protected final ListReaderCustomizer<C, K, B> customizer; /** * Create new {@link GenericListReader} @@ -113,6 +113,7 @@ public final class GenericListReader<C extends DataObject & Identifiable<K>, K e } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier<C> 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<C extends DataObject, B extends Builder<C>> extends AbstractGenericReader<C, B> - implements Reader<C, B> { +public class GenericReader<C extends DataObject, B extends Builder<C>> + extends AbstractGenericReader<C, B> + implements Reader<C, B> { - private final ReaderCustomizer<C, B> customizer; + protected final ReaderCustomizer<C, B> 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<C> id, @@ -61,6 +62,7 @@ public final class GenericReader<C extends DataObject, B extends Builder<C>> ext } } + @Nonnull @Override public B getBuilder(@Nonnull final InstanceIdentifier<C> id) { return customizer.getBuilder(id); |