BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleRSVPExtensionProviderContext.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
9 package org.opendaylight.protocol.rsvp.parser.spi.pojo;
10
11 import com.google.common.cache.Cache;
12 import com.google.common.cache.CacheBuilder;
13 import java.util.concurrent.atomic.AtomicReference;
14 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.rsvp.parser.spi.LabelParser;
17 import org.opendaylight.protocol.rsvp.parser.spi.LabelSerializer;
18 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
20 import org.opendaylight.protocol.rsvp.parser.spi.RSVPExtensionProviderContext;
21 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectParser;
22 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectSerializer;
23 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
24 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
25 import org.opendaylight.protocol.util.ReferenceCache;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
29
30 public class SimpleRSVPExtensionProviderContext extends SimpleRSVPExtensionConsumerContext implements RSVPExtensionProviderContext {
31
32     public static final int DEFAULT_MAXIMUM_CACHED_OBJECTS = 100000;
33
34     private final AtomicReference<Cache<Object, Object>> cacheRef;
35     private final ReferenceCache referenceCache = new ReferenceCache() {
36         @Override
37         public <T> T getSharedReference(final T object) {
38             final Cache<Object, Object> cache = SimpleRSVPExtensionProviderContext.this.cacheRef.get();
39
40             @SuppressWarnings("unchecked")
41             final T ret = (T) cache.getIfPresent(object);
42             if (ret == null) {
43                 cache.put(object, object);
44                 return object;
45             }
46
47             return ret;
48         }
49     };
50     private final int maximumCachedObjects;
51
52     public SimpleRSVPExtensionProviderContext() {
53         this(DEFAULT_MAXIMUM_CACHED_OBJECTS);
54     }
55
56     public SimpleRSVPExtensionProviderContext(final int maximumCachedObjects) {
57         this.maximumCachedObjects = maximumCachedObjects;
58
59         final Cache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(maximumCachedObjects).build();
60         this.cacheRef = new AtomicReference<Cache<Object,Object>>(cache);
61     }
62
63     @Override
64     public ReferenceCache getReferenceCache() {
65         return this.referenceCache;
66     }
67
68
69     @Override
70     public void registerRsvpObjectParser(final int classNum, final int cType, final RSVPTeObjectParser parser) {
71         this.getRsvpRegistry().registerRsvpObjectParser(classNum, cType, parser);
72     }
73
74     @Override
75     public void registerRsvpObjectSerializer(final Class<? extends RsvpTeObject> objectClass, final RSVPTeObjectSerializer serializer) {
76         this.getRsvpRegistry().registerRsvpObjectSerializer(objectClass, serializer);
77     }
78
79     @Override
80     public AutoCloseable registerXROSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass, final XROSubobjectSerializer serializer) {
81         return this.getXROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
82     }
83
84     @Override
85     public AutoCloseable registerXROSubobjectParser(final int subobjectType, final XROSubobjectParser parser) {
86         return this.getXROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
87     }
88
89     @Override
90     public AutoCloseable registerRROSubobjectSerializer(final Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.SubobjectType> subobjectClass, final RROSubobjectSerializer serializer) {
91         return this.getRROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
92     }
93
94     @Override
95     public AutoCloseable registerRROSubobjectParser(final int subobjectType, final RROSubobjectParser parser) {
96         return this.getRROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
97     }
98
99     @Override
100     public AutoCloseable registerEROSubobjectSerializer(final Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType> subobjectClass, final EROSubobjectSerializer serializer) {
101         return this.getEROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
102     }
103
104     @Override
105     public AutoCloseable registerEROSubobjectParser(final int subobjectType, final EROSubobjectParser parser) {
106         return this.getEROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
107     }
108
109     @Override
110     public AutoCloseable registerLabelSerializer(final Class<? extends LabelType> labelClass, final LabelSerializer serializer) {
111         return this.getLabelHandlerRegistry().registerLabelSerializer(labelClass, serializer);
112     }
113
114     @Override
115     public AutoCloseable registerLabelParser(final int cType, final LabelParser parser) {
116         return this.getLabelHandlerRegistry().registerLabelParser(cType, parser);
117     }
118 }