Revert "BUG-3347 : fix receiving updates only on first route"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / CachingImportPolicy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.IdentityHashMap;
12 import java.util.Map;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
15
16 /**
17  * A caching decorator for {@link AbstractImportPolicy}. Performs caching of effective
18  * attributes using an {@link IdentityHashMap} for fast lookup and reuse of resulting
19  * objects.
20  */
21 @NotThreadSafe
22 final class CachingImportPolicy extends AbstractImportPolicy {
23     private final Map<ContainerNode, ContainerNode> cache = new IdentityHashMap<>();
24     private final AbstractImportPolicy delegate;
25
26     CachingImportPolicy(final AbstractImportPolicy delegate) {
27         this.delegate = Preconditions.checkNotNull(delegate);
28     }
29
30     @Override
31     ContainerNode effectiveAttributes(final ContainerNode attributes) {
32         ContainerNode ret = this.cache.get(attributes);
33         if (ret == null) {
34             ret = this.delegate.effectiveAttributes(attributes);
35             if (ret != null) {
36                 this.cache.put(attributes, ret);
37             }
38         }
39
40         return ret;
41     }
42 }