6c526d1faaf7763bf79178f712e9412514f46b9e
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.v3po.util.cache;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.ModificationCache;
23 import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException;
24 import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException;
25 import io.fd.honeycomb.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction;
26 import javax.annotation.Nonnull;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Manager responsible for returning Data object dumps<br> either from cache or by invoking specified {@link
32  * EntityDumpExecutor}
33  */
34 public final class DumpCacheManager<T, U> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class);
37
38     private final EntityDumpExecutor<T, U> dumpExecutor;
39     private final EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck;
40     private final EntityDumpPostProcessingFunction<T> postProcessor;
41
42     private DumpCacheManager(DumpCacheManagerBuilder<T, U> builder) {
43         this.dumpExecutor = builder.dumpExecutor;
44         this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck;
45         this.postProcessor = builder.postProcessingFunction;
46     }
47
48     /**
49      * Returns {@link Optional<T>} of dump
50      */
51     public Optional<T> getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache, final U dumpParams)
52             throws DumpExecutionFailedException {
53
54         // this key binding to every log has its logic ,because every customizer have its own cache manager and if
55         // there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs
56         LOG.debug("Loading dump for KEY[{}]", entityKey);
57
58         T dump = (T) cache.get(entityKey);
59
60         if (dump == null) {
61             LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey);
62             // binds and execute dump to be thread-save
63             dump = dumpExecutor.executeDump(dumpParams);
64
65             // TODO (HONEYCOMB-210): remove empty check (empty dump is normal state, special handling is not needed)
66             try {
67                 dumpNonEmptyCheck.assertNotEmpty(dump);
68             } catch (DumpCheckFailedException e) {
69                 LOG.debug("Dump for KEY[{}] has been resolved as empty: {}", entityKey, e.getMessage());
70                 return Optional.absent();
71             }
72
73             // no need to check if post processor active,if wasn't set,default no-op will be used
74             LOG.debug("Post-processing dump for KEY[{}]", entityKey);
75             dump = postProcessor.apply(dump);
76
77             LOG.debug("Caching dump for KEY[{}]", entityKey);
78             cache.put(entityKey, dump);
79             return Optional.of(dump);
80         } else {
81             return Optional.of(dump);
82         }
83     }
84
85     public static final class DumpCacheManagerBuilder<T, U> {
86
87         private EntityDumpExecutor<T, U> dumpExecutor;
88         private EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck;
89         private EntityDumpPostProcessingFunction<T> postProcessingFunction;
90
91         public DumpCacheManagerBuilder() {
92             // for cases when user does not set specific post-processor
93             postProcessingFunction = new NoopDumpPostProcessingFunction<T>();
94         }
95
96         public DumpCacheManagerBuilder<T, U> withExecutor(@Nonnull EntityDumpExecutor<T, U> executor) {
97             this.dumpExecutor = executor;
98             return this;
99         }
100
101         public DumpCacheManagerBuilder<T, U> withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck<T> check) {
102             this.dumpNonEmptyCheck = check;
103             return this;
104         }
105
106         public DumpCacheManagerBuilder<T, U> withPostProcessingFunction(
107                 EntityDumpPostProcessingFunction<T> postProcessingFunction) {
108             this.postProcessingFunction = postProcessingFunction;
109             return this;
110         }
111
112         public DumpCacheManager<T, U> build() {
113             checkNotNull(dumpExecutor, "Dump executor cannot be null");
114             checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null");
115             checkNotNull(postProcessingFunction,
116                     "Dump post-processor cannot be null cannot be null, default implementation is used if its not set");
117
118             return new DumpCacheManager<>(this);
119         }
120     }
121 }
122
123