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 --- infra/translate-api/pom.xml | 4 +++ .../translate/read/InitFailedException.java | 30 ++++++++++++++++ .../honeycomb/translate/read/InitListReader.java | 31 ++++++++++++++++ .../io/fd/honeycomb/translate/read/InitReader.java | 29 +++++++++++++++ .../fd/honeycomb/translate/read/Initializer.java | 42 ++++++++++++++++++++++ .../translate/read/ReadFailedException.java | 8 ++++- .../translate/read/registry/InitRegistry.java | 34 ++++++++++++++++++ .../translate/read/registry/ReaderRegistry.java | 2 +- 8 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java create mode 100644 infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java (limited to 'infra/translate-api') diff --git a/infra/translate-api/pom.xml b/infra/translate-api/pom.xml index 374ed6c3e..40b37cc3f 100644 --- a/infra/translate-api/pom.xml +++ b/infra/translate-api/pom.xml @@ -38,6 +38,10 @@ org.opendaylight.mdsal mdsal-binding-api + + org.opendaylight.controller + sal-binding-api + junit diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java new file mode 100644 index 000000000..4e057c51e --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitFailedException.java @@ -0,0 +1,30 @@ +/* + * 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.read; + +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Exception indicating a failure during the initialization process. + */ +public class InitFailedException extends ReadFailedException { + + public InitFailedException(@Nonnull final InstanceIdentifier failedId, final Throwable cause) { + super("Failed to initialize: ", failedId, cause); + } +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java new file mode 100644 index 000000000..a7d3ee2ac --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitListReader.java @@ -0,0 +1,31 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +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; + +/** + * Special type of list reader capable of participating in the initialization process. + */ +@Beta +public interface InitListReader, K extends Identifier, B extends Builder> + extends ListReader, Initializer { +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java new file mode 100644 index 000000000..e5b81b947 --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/InitReader.java @@ -0,0 +1,29 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import org.opendaylight.yangtools.concepts.Builder; +import org.opendaylight.yangtools.yang.binding.DataObject; + +/** + * Special type of reader capable of participating in the initialization process. + */ +@Beta +public interface InitReader> + extends Reader, Initializer { +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java new file mode 100644 index 000000000..e26ab55bc --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/Initializer.java @@ -0,0 +1,42 @@ +/* + * 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.read; + +import com.google.common.annotations.Beta; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Generic initializer. + *

+ * Capable of invoking 0..n edits as a result of existing operational data. + */ +@Beta +public interface Initializer { + + /** + * Transform operational data located under provided (keyed) id. + * + * @param broker DataBroker that accepts the resulting config data + * @param id InstanceIdentifier of the operational data to initialize from + * @param ctx Standard read context to assist during initialization e.g. caching data between customizers + */ + void init(@Nonnull DataBroker broker, @Nonnull InstanceIdentifier id, @Nonnull ReadContext ctx) + throws InitFailedException; +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java index fc429b1d0..349e27451 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/ReadFailedException.java @@ -36,7 +36,13 @@ public class ReadFailedException extends TranslationException { * @param cause the cause of read failure */ public ReadFailedException(@Nonnull final InstanceIdentifier failedId, final Throwable cause) { - super("Failed to read " + failedId, cause); + this("Failed to read: ", failedId, cause); + } + + protected ReadFailedException(@Nonnull final String msgPrefix, + @Nonnull final InstanceIdentifier failedId, + @Nonnull final Throwable cause) { + super(msgPrefix + failedId, cause); this.failedId = checkNotNull(failedId, "failedId should not be null"); } diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java new file mode 100644 index 000000000..8a97a7141 --- /dev/null +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/InitRegistry.java @@ -0,0 +1,34 @@ +/* + * 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.read.registry; + + +import io.fd.honeycomb.translate.read.InitFailedException; +import io.fd.honeycomb.translate.read.ReadContext; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; + +/** + * Root initializer. + */ +public interface InitRegistry { + + /** + * Perform initialization on top of the data root. + */ + void initAll(@Nonnull DataBroker broker, @Nonnull ReadContext ctx) throws InitFailedException; +} diff --git a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java index 9a1c99508..6436fd738 100644 --- a/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java +++ b/infra/translate-api/src/main/java/io/fd/honeycomb/translate/read/registry/ReaderRegistry.java @@ -29,7 +29,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; * Simple delegating reader suitable as a holder for all other root readers, providing readAll feature. */ @Beta -public interface ReaderRegistry { +public interface ReaderRegistry extends InitRegistry { /** * Performs read on all registered root readers and merges the results into a Multimap. Keys represent identifiers -- cgit 1.2.3-korg