BUG-6647 Increase code coverage and clean up IV
[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     private 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
51     public SimpleRSVPExtensionProviderContext() {
52         this(DEFAULT_MAXIMUM_CACHED_OBJECTS);
53     }
54
55     public SimpleRSVPExtensionProviderContext(final int maximumCachedObjects) {
56         final Cache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(maximumCachedObjects).build();
57         this.cacheRef = new AtomicReference<Cache<Object,Object>>(cache);
58     }
59
60     @Override
61     public ReferenceCache getReferenceCache() {
62         return this.referenceCache;
63     }
64
65
66     @Override
67     public void registerRsvpObjectParser(final int classNum, final int cType, final RSVPTeObjectParser parser) {
68         this.getRsvpRegistry().registerRsvpObjectParser(classNum, cType, parser);
69     }
70
71     @Override
72     public void registerRsvpObjectSerializer(final Class<? extends RsvpTeObject> objectClass, final RSVPTeObjectSerializer serializer) {
73         this.getRsvpRegistry().registerRsvpObjectSerializer(objectClass, serializer);
74     }
75
76     @Override
77     public AutoCloseable registerXROSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass, final XROSubobjectSerializer serializer) {
78         return this.getXROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
79     }
80
81     @Override
82     public AutoCloseable registerXROSubobjectParser(final int subobjectType, final XROSubobjectParser parser) {
83         return this.getXROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
84     }
85
86     @Override
87     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) {
88         return this.getRROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
89     }
90
91     @Override
92     public AutoCloseable registerRROSubobjectParser(final int subobjectType, final RROSubobjectParser parser) {
93         return this.getRROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
94     }
95
96     @Override
97     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) {
98         return this.getEROSubobjectHandlerRegistry().registerSubobjectSerializer(subobjectClass, serializer);
99     }
100
101     @Override
102     public AutoCloseable registerEROSubobjectParser(final int subobjectType, final EROSubobjectParser parser) {
103         return this.getEROSubobjectHandlerRegistry().registerSubobjectParser(subobjectType, parser);
104     }
105
106     @Override
107     public AutoCloseable registerLabelSerializer(final Class<? extends LabelType> labelClass, final LabelSerializer serializer) {
108         return this.getLabelHandlerRegistry().registerLabelSerializer(labelClass, serializer);
109     }
110
111     @Override
112     public AutoCloseable registerLabelParser(final int cType, final LabelParser parser) {
113         return this.getLabelHandlerRegistry().registerLabelParser(cType, parser);
114     }
115 }