018a7da5bb7af1493b0425b838056a65b16aefdc
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AdjRIBOutEntry.java
1 /*
2  * Copyright (c) 2014 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
12 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
13
14 import org.opendaylight.protocol.bgp.rib.spi.RouteEncoder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18
19 final class AdjRIBOutEntry<K, V extends Route> {
20     @SuppressWarnings("rawtypes")
21     private static final AtomicReferenceFieldUpdater<AdjRIBOutEntry, Route> NLRIENTRY_ADV_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AdjRIBOutEntry.class, Route.class, "advertisedValue");
22     @SuppressWarnings("rawtypes")
23     private static final AtomicReferenceFieldUpdater<AdjRIBOutEntry, Route> NLRIENTRY_CUR_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AdjRIBOutEntry.class, Route.class, "currentValue");
24
25     /*
26      * Marker object for uninitialized value. Distinct from null.
27      */
28     private static final Route NLRIENTRY_NONE_VALUE = new Route() {
29         @Override
30         public Class<? extends DataContainer> getImplementedInterface() {
31             throw new IllegalStateException("This method should never be invoked");
32         }
33
34         @Override
35         public Attributes getAttributes() {
36             throw new IllegalStateException("This method should never be invoked");
37         }
38     };
39
40     // Referenced via AtomicReferenceFieldUpdaters
41     @SuppressWarnings("unused")
42     private volatile Route currentValue = NLRIENTRY_NONE_VALUE;
43     @SuppressWarnings("unused")
44     private volatile Route advertisedValue = NLRIENTRY_NONE_VALUE;
45     private final RouteEncoder ribOut;
46
47     AdjRIBOutEntry(final RouteEncoder ribOut) {
48         this.ribOut = Preconditions.checkNotNull(ribOut);
49     }
50
51     Route getAdverised() {
52         return NLRIENTRY_ADV_UPDATER.get(this);
53     }
54
55     void setAdverised(final V value) {
56         NLRIENTRY_ADV_UPDATER.set(this, value);
57     }
58
59     @SuppressWarnings("unchecked")
60     V getCurrent() {
61         final Route o = NLRIENTRY_CUR_UPDATER.get(this);
62         Preconditions.checkState(!isNone(o), "Value cannot be NONE here");
63         return (V) o;
64     }
65
66     Route getAndSetCurrent(final V value) {
67         return NLRIENTRY_CUR_UPDATER.getAndSet(this, value);
68     }
69
70     RouteEncoder getRibOut() {
71         return ribOut;
72     }
73
74     static final boolean isNone(final Object o) {
75         return o == NLRIENTRY_NONE_VALUE;
76     }
77 }