From 007d4542388ca89be409ce1a4a4c7a36ddcb538f Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Wed, 27 Jul 2016 11:13:11 +0200 Subject: HONEYCOMB-106 - Support for generic cache management Changed exception handling/logging Removed useless utility class/its logic shifted to respective classes Cleanup of not needed code Change-Id: Id2fe4ab60b541067c3d0dc6fa442fbfa66d1e618 Signed-off-by: Jan Srnicek --- .../v3po/util/cache/DumpCacheManager.java | 124 ++++++++++++++++++ .../v3po/util/cache/EntityDumpExecutor.java | 41 ++++++ .../v3po/util/cache/EntityDumpNonEmptyCheck.java | 33 +++++ .../cache/EntityDumpPostProcessingFunction.java | 36 ++++++ .../exceptions/check/DumpCheckFailedException.java | 31 +++++ .../exceptions/check/i/DumpEmptyException.java | 40 ++++++ .../execution/DumpExecutionFailedException.java | 27 ++++ .../execution/i/DumpCallFailedException.java | 34 +++++ .../execution/i/DumpTimeoutException.java | 35 +++++ .../cache/noop/NoopDumpPostProcessingFunction.java | 33 +++++ .../v3po/util/cache/DumpCacheManagerTest.java | 144 +++++++++++++++++++++ 11 files changed, 578 insertions(+) create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java create mode 100644 v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java new file mode 100644 index 000000000..9486ce171 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java @@ -0,0 +1,124 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction; +import javax.annotation.Nonnull; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager responsible for returning Data object dumps
either from cache or by invoking specified {@link + * EntityDumpExecutor} + */ +public final class DumpCacheManager { + + private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class); + + private final EntityDumpExecutor dumpExecutor; + private final EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private final EntityDumpPostProcessingFunction postProcessor; + + private DumpCacheManager(DumpCacheManagerBuilder builder) { + this.dumpExecutor = builder.dumpExecutor; + this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck; + this.postProcessor = builder.postProcessingFunction; + } + + /** + * Returns {@link Optional} of dump + */ + public Optional getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache) + throws DumpExecutionFailedException { + + //this key binding to every log has its logic ,because every customizer have its own cache manager and if + //there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs + LOG.debug("Loading dump for KEY[{}]", entityKey); + + T dump = (T) cache.get(entityKey); + + if (dump == null) { + LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey); + + dump = dumpExecutor.executeDump(); + + //this is not a critical exception, so its only logged here + try { + dumpNonEmptyCheck.assertNotEmpty(dump); + } catch (DumpCheckFailedException e) { + LOG.warn("Dump for KEY[{}] has been resolved as empty", entityKey, e); + return Optional.absent(); + } + + //no need to check if post processor active,if wasnt set,default no-op will be used + LOG.debug("Post-processing dump for KEY[{}]", entityKey); + dump = postProcessor.apply(dump); + + LOG.debug("Caching dump for KEY[{}]", entityKey); + cache.put(entityKey, dump); + return Optional.of(dump); + } else { + return Optional.of(dump); + } + } + + public static final class DumpCacheManagerBuilder { + + private EntityDumpExecutor dumpExecutor; + private EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private EntityDumpPostProcessingFunction postProcessingFunction; + + public DumpCacheManagerBuilder() { + //for cases when user does not set specific post-processor + postProcessingFunction = new NoopDumpPostProcessingFunction(); + } + + public DumpCacheManagerBuilder withExecutor(@Nonnull EntityDumpExecutor executor) { + this.dumpExecutor = executor; + return this; + } + + public DumpCacheManagerBuilder withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck check) { + this.dumpNonEmptyCheck = check; + return this; + } + + public DumpCacheManagerBuilder withPostProcessingFunction( + EntityDumpPostProcessingFunction postProcessingFunction) { + this.postProcessingFunction = postProcessingFunction; + return this; + } + + public DumpCacheManager build() { + checkNotNull(dumpExecutor, "Dump executor cannot be null"); + checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null"); + checkNotNull(postProcessingFunction, + "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + + return new DumpCacheManager(this); + } + } +} + + diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java new file mode 100644 index 000000000..82b30cf26 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java @@ -0,0 +1,41 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that return dumps for Data objects + */ +public interface EntityDumpExecutor { + + /** + * Performs dump on {link T} entity + * + * @return dump of specified {@link T} entity + * @throws DumpExecutionFailedException when dump fails + */ + public T executeDump() throws DumpExecutionFailedException; + + /** + * Bind dumping params for executor + */ + default public void bindDumpParams(U params) { + throw new UnsupportedOperationException("You should override this method if you need to bind dumping params"); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java new file mode 100644 index 000000000..6fb404a7a --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java @@ -0,0 +1,33 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that verifies if dump of data object is non-empty + */ +public interface EntityDumpNonEmptyCheck { + + /** + * Verifies if data are non-empty,if not throws {@link DumpEmptyException} + * @throws DumpEmptyException + */ + public void assertNotEmpty(T data) throws DumpCheckFailedException; +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java new file mode 100644 index 000000000..3d0040ea6 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java @@ -0,0 +1,36 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import java.util.function.Function; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for class that are post-processing data dumped from vpp + */ +@FunctionalInterface +public interface EntityDumpPostProcessingFunction extends Function { + + + /** + * Performs postprocessing on dumped data + * + * @return Post-processed data + */ + @Override + T apply(T t); +} \ No newline at end of file diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java new file mode 100644 index 000000000..f3f8f7fef --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.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.v3po.translate.v3po.util.cache.exceptions.check; + +/** + * Abstract parent of exceptions thrown while checking if dump is not empty + */ +public abstract class DumpCheckFailedException extends Exception { + + public DumpCheckFailedException(String message) { + super(message); + } + + public DumpCheckFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java new file mode 100644 index 000000000..a5a228823 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java @@ -0,0 +1,40 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.check.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.EntityDumpNonEmptyCheck; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; + +/** + * This exception occurs when dump is resolved as empty by {@link EntityDumpNonEmptyCheck} + */ +public class DumpEmptyException extends DumpCheckFailedException { + + /** + * Creates {@link DumpEmptyException} with specified reason + */ + public DumpEmptyException(String reason) { + super(reason); + } + + /** + * Creates {@link DumpEmptyException} with specified reason and sub-exception + */ + public DumpEmptyException(String reason, Exception e) { + super(reason, e); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java new file mode 100644 index 000000000..5f4939cfb --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java @@ -0,0 +1,27 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.execution; + +/** + * Default parent for all exceptions connected to dumping of VPP data + */ +public abstract class DumpExecutionFailedException extends Exception { + + public DumpExecutionFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java new file mode 100644 index 000000000..9b92409f5 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.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.v3po.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.VppBaseCallException; + +/** + * Wrapper exception for {@link org.openvpp.jvpp.VppBaseCallException} during dumping + */ +public class DumpCallFailedException extends DumpExecutionFailedException { + + public DumpCallFailedException(String message, VppBaseCallException cause) { + super(message, cause); + } + + public static final DumpCallFailedException wrapFailedCallException(String message, VppBaseCallException cause) { + return new DumpCallFailedException(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java new file mode 100644 index 000000000..afb278c9d --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java @@ -0,0 +1,35 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import java.util.concurrent.TimeoutException; + +/** + * Exception thrown when dump call ends in timeout + */ +public class DumpTimeoutException extends DumpExecutionFailedException { + + public DumpTimeoutException(String message, TimeoutException cause) { + super(message, cause); + + } + + public static final DumpTimeoutException wrapTimeoutException(String message, TimeoutException cause) { + return new DumpTimeoutException(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java new file mode 100644 index 000000000..5efe439ab --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java @@ -0,0 +1,33 @@ +/* + * 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.v3po.translate.v3po.util.cache.noop; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.EntityDumpPostProcessingFunction; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NoopDumpPostProcessingFunction implements EntityDumpPostProcessingFunction { + + private static final Logger LOG = LoggerFactory.getLogger(NoopDumpPostProcessingFunction.class); + + @Override + public T apply(final T t) { + LOG.debug("Default post processing function called for {}", t); + return t; + } +} diff --git a/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java new file mode 100644 index 000000000..9cbbda8ad --- /dev/null +++ b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java @@ -0,0 +1,144 @@ +/* + * 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.v3po.translate.v3po.util.cache; + + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openvpp.jvpp.dto.IpDetails; +import org.openvpp.jvpp.dto.IpDetailsReplyDump; + +public class DumpCacheManagerTest { + + private static final String KEY = "cacheKey"; + + @Mock + private EntityDumpExecutor executor; + + private DumpCacheManager managerPositive; + private DumpCacheManager managerPositiveWithPostProcessing; + private DumpCacheManager managerNegative; + private ModificationCache cache; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + managerPositive = + new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .build(); + + managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .withPostProcessingFunction(createPostProcessor()) + .build(); + + managerNegative = + new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createNegativePredicate()) + .build(); + + cache = new ModificationCache(); + } + + /** + * This test verify full dump-caching cycle + */ + @Test + public void testCaching() throws DumpExecutionFailedException { + + + Optional stage1Optional = managerNegative.getDump(KEY, cache); + + //this is first call so instance should be from executor + assertEquals(false, stage1Optional.isPresent()); + assertEquals(false, cache.containsKey(KEY)); + + //rebind executor with other data + IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage2LoadedDump); + + Optional stage2Optional = managerPositive.getDump(KEY, cache); + + assertEquals(true, stage2Optional.isPresent()); + assertEquals(stage2LoadedDump, stage2Optional.get()); + + //rebind executor with other data + IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage3LoadedDump); + + Optional stage3Optional = managerPositive.getDump(KEY, cache); + assertEquals(true, stage3Optional.isPresent()); + //check if it returns instance cached from previous stage + assertEquals(stage2LoadedDump, stage3Optional.get()); + } + + @Test + public void testPostprocessing() throws DumpExecutionFailedException { + IpDetailsReplyDump dump = new IpDetailsReplyDump(); + IpDetails details = new IpDetails(); + details.swIfIndex = 2; + dump.ipDetails.add(details); + + when(executor.executeDump()).thenReturn(dump); + + Optional optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache); + + assertEquals(true, optionalDump.isPresent()); + assertEquals(1, optionalDump.get().ipDetails.size()); + assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex); + } + + private EntityDumpNonEmptyCheck createNegativePredicate() { + return data -> { + throw new DumpEmptyException("Empty dump", new IllegalArgumentException()); + }; + } + + private EntityDumpNonEmptyCheck createPositivePredicate() { + return data -> { + //DO NOTHING + }; + } + + private EntityDumpPostProcessingFunction createPostProcessor() { + return ipDetailsReplyDump -> { + IpDetailsReplyDump modified = new IpDetailsReplyDump(); + + for (IpDetails detail : ipDetailsReplyDump.ipDetails) { + IpDetails modifiedDetail = new IpDetails(); + modifiedDetail.swIfIndex = detail.swIfIndex + 5; + + modified.ipDetails.add(modifiedDetail); + } + + return modified; + }; + } +} \ No newline at end of file -- cgit 1.2.3-korg