diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-10-26 12:19:23 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-10-28 11:06:39 +0000 |
commit | 0924cc57c02cf8c39793db6aa6895ee7bbdb2fb6 (patch) | |
tree | 431b622df68b062299246352fa45dbbb6041de55 /samples/interfaces | |
parent | 2224731c7e3a59213f4c0d4df064e251fc069a97 (diff) |
Honeycomb-73 Extensible initializers for samples
Change-Id: I08f53a301cc147ce1e8cce8b3bd5c82e8f4b21fd
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'samples/interfaces')
4 files changed, 21 insertions, 76 deletions
diff --git a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/SampleInterfaceModule.java b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/SampleInterfaceModule.java index 000113f11..ea8a72724 100644 --- a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/SampleInterfaceModule.java +++ b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/SampleInterfaceModule.java @@ -19,13 +19,11 @@ package io.fd.honeycomb.samples.interfaces.mapping; import com.google.inject.AbstractModule; import com.google.inject.Singleton; import com.google.inject.multibindings.Multibinder; -import io.fd.honeycomb.data.init.DataTreeInitializer; import io.fd.honeycomb.notification.ManagedNotificationProducer; -import io.fd.honeycomb.samples.interfaces.mapping.config.InterfacesWriterFactory; -import io.fd.honeycomb.samples.interfaces.mapping.oper.InterfacesReaderFactory; import io.fd.honeycomb.samples.interfaces.mapping.cfgattrs.InterfacesPluginConfiguration; -import io.fd.honeycomb.samples.interfaces.mapping.init.InterfacesInitializer; +import io.fd.honeycomb.samples.interfaces.mapping.config.InterfacesWriterFactory; import io.fd.honeycomb.samples.interfaces.mapping.notification.InterfaceUpNotificationProducer; +import io.fd.honeycomb.samples.interfaces.mapping.oper.InterfacesReaderFactory; import io.fd.honeycomb.translate.read.ReaderFactory; import io.fd.honeycomb.translate.write.WriterFactory; import net.jmob.guice.conf.core.ConfigurationModule; @@ -47,7 +45,6 @@ public final class SampleInterfaceModule extends AbstractModule { // Below are classes picked up by HC framework Multibinder.newSetBinder(binder(), WriterFactory.class).addBinding().to(InterfacesWriterFactory.class); Multibinder.newSetBinder(binder(), ReaderFactory.class).addBinding().to(InterfacesReaderFactory.class); - Multibinder.newSetBinder(binder(), DataTreeInitializer.class).addBinding().to(InterfacesInitializer.class); Multibinder.newSetBinder(binder(), ManagedNotificationProducer.class).addBinding() .to(InterfaceUpNotificationProducer.class); } diff --git a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/init/InterfacesInitializer.java b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/init/InterfacesInitializer.java deleted file mode 100644 index 9aea508c7..000000000 --- a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/init/InterfacesInitializer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.samples.interfaces.mapping.init; - -import com.google.inject.Inject; -import com.google.inject.name.Named; -import io.fd.honeycomb.data.init.AbstractDataTreeConverter; -import java.util.stream.Collectors; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.Interfaces; -import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesBuilder; -import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesState; -import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceBuilder; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This is an initializer for interfaces plugin. Its main goal is to revers-engineer configuration data (config "true") - * for interfaces model from operational data. In this case, we are trying to recreate Interfaces container from InterfacesState - * container. Thanks to symmetrical nature of the model, it's pretty straightforward. - * - * This is very useful when the lower layer already contains some data that should be revers-engineer to config data - * in honeycomb in order to get HC and lower layer to sync... It makes life of upper layers much easier - * - * However it's not always possible to perform this task so the initializers are optional for plugins - */ -public class InterfacesInitializer extends AbstractDataTreeConverter<InterfacesState, Interfaces> { - - private static final Logger LOG = LoggerFactory.getLogger(InterfacesInitializer.class); - - @Inject - public InterfacesInitializer(@Named("honeycomb-initializer") final DataBroker bindingDataBroker) { - super(bindingDataBroker, - InstanceIdentifier.create(InterfacesState.class), InstanceIdentifier.create(Interfaces.class)); - } - - @Override - protected Interfaces convert(final InterfacesState operationalData) { - // Just convert operational data into config data - // The operational data are queried from lower layer using readerCustomizers from this plugin - - LOG.info("Initializing interfaces config data from: {}", operationalData); - - return new InterfacesBuilder() - .setInterface(operationalData.getInterface().stream() - .map(oper -> new InterfaceBuilder() - .setMtu(oper.getMtu()) - .setInterfaceId(oper.getInterfaceId()) - .build()) - .collect(Collectors.toList())) - .build(); - } -} diff --git a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/CountersReaderCustomizer.java b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/CountersReaderCustomizer.java index b5ec2cbd9..71476baa3 100644 --- a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/CountersReaderCustomizer.java +++ b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/CountersReaderCustomizer.java @@ -44,7 +44,6 @@ public class CountersReaderCustomizer implements ReaderCustomizer<Counters, Coun this.access = access; } - @Nonnull @Override public CountersBuilder getBuilder(@Nonnull final InstanceIdentifier<Counters> id) { diff --git a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/InterfaceReaderCustomizer.java b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/InterfaceReaderCustomizer.java index 7b69005e0..850c1396b 100644 --- a/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/InterfaceReaderCustomizer.java +++ b/samples/interfaces/mapping/src/main/java/io/fd/honeycomb/samples/interfaces/mapping/oper/InterfaceReaderCustomizer.java @@ -19,11 +19,13 @@ package io.fd.honeycomb.samples.interfaces.mapping.oper; import io.fd.honeycomb.samples.interfaces.mapping.LowerLayerAccess; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; +import io.fd.honeycomb.translate.spi.read.Initialized; +import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; import java.util.List; import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfaceId; +import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.Interfaces; import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesStateBuilder; import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.InterfaceBuilder; @@ -37,7 +39,8 @@ import org.slf4j.LoggerFactory; /** * This is a customizer responsible for reading Interface operational data */ -public class InterfaceReaderCustomizer implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> { +public class InterfaceReaderCustomizer implements + InitializingListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> { private static final Logger LOG = LoggerFactory.getLogger(InterfaceReaderCustomizer.class); private final LowerLayerAccess access; @@ -100,4 +103,18 @@ public class InterfaceReaderCustomizer implements ListReaderCustomizer<Interface // However its a good practice to provide a dedicated reader+customizer for every complex node } + @Override + public Initialized<org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.Interface> init( + @Nonnull final InstanceIdentifier<Interface> id, + @Nonnull final Interface readValue, + @Nonnull final ReadContext ctx) { + return Initialized.create(InstanceIdentifier.create(Interfaces.class) + .child(org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.Interface.class, + new org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceKey( + id.firstKeyOf(Interface.class).getInterfaceId())), + new org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceBuilder() + .setMtu(readValue.getMtu()) + .setInterfaceId(readValue.getInterfaceId()) + .build()); + } } |