c9d4b944a2588f7488e8814a1ac163449d5a441b
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / EagerJSONCodecFactory.java
1 /*
2  * Copyright (c) 2017 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.yangtools.yang.data.codec.gson;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import java.util.Map;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Pre-computed JSONCodecFactory. All possible codecs are created upfront at instantiation time, after which they
27  * are available for the cost of a constant lookup.
28  *
29  * @author Robert Varga
30  */
31 @ThreadSafe
32 final class EagerJSONCodecFactory extends JSONCodecFactory {
33     private static final Logger LOG = LoggerFactory.getLogger(EagerJSONCodecFactory.class);
34
35     // Weak keys to retire the entry when SchemaContext goes away
36     private static final LoadingCache<SchemaContext, EagerJSONCodecFactory> CACHE = CacheBuilder.newBuilder()
37             .weakKeys().build(new CacheLoader<SchemaContext, EagerJSONCodecFactory>() {
38                 @Override
39                 public EagerJSONCodecFactory load(final SchemaContext key) {
40                     final Stopwatch sw = Stopwatch.createStarted();
41                     final LazyJSONCodecFactory lazy = new LazyJSONCodecFactory(key);
42                     final int visitedLeaves = requestCodecsForChildren(lazy, key);
43                     sw.stop();
44
45                     final Map<TypeDefinition<?>, JSONCodec<?>> simple = lazy.getSimpleCodecs();
46                     final Map<TypedSchemaNode, JSONCodec<?>> complex = lazy.getComplexCodecs();
47
48                     LOG.debug("{} leaf nodes resulted in {} simple and {} complex codecs in {}", visitedLeaves,
49                         simple.size(), complex.size(), sw);
50                     return new EagerJSONCodecFactory(key, simple, complex);
51                 }
52             });
53
54     private final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs;
55     private final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs;
56
57     private EagerJSONCodecFactory(final SchemaContext context, final Map<TypeDefinition<?>, JSONCodec<?>> simpleCodecs,
58         final Map<TypedSchemaNode, JSONCodec<?>> complexCodecs) {
59         super(context);
60         this.simpleCodecs = Preconditions.checkNotNull(simpleCodecs);
61         this.complexCodecs = Preconditions.checkNotNull(complexCodecs);
62     }
63
64     static EagerJSONCodecFactory get(final SchemaContext context) {
65         return CACHE.getUnchecked(context);
66     }
67
68     static EagerJSONCodecFactory getIfPresent(final SchemaContext context) {
69         return CACHE.getIfPresent(context);
70     }
71
72     @Override
73     JSONCodec<?> lookupComplex(final TypedSchemaNode schema) {
74         final JSONCodec<?> ret = complexCodecs.get(schema);
75         Preconditions.checkArgument(ret != null, "No codec available for schema %s", schema);
76         return ret;
77     }
78
79     @Override
80     JSONCodec<?> lookupSimple(final TypeDefinition<?> type) {
81         return simpleCodecs.get(type);
82     }
83
84     @Override
85     JSONCodec<?> getComplex(final TypedSchemaNode schema, final JSONCodec<?> codec) {
86         throw new IllegalStateException("Uncached codec for " + schema);
87     }
88
89     @Override
90     JSONCodec<?> getSimple(final TypeDefinition<?> type, final JSONCodec<?> codec) {
91         throw new IllegalStateException("Uncached codec for " + type);
92     }
93
94     static int requestCodecsForChildren(final LazyJSONCodecFactory lazy, final DataNodeContainer parent) {
95         int ret = 0;
96         for (DataSchemaNode child : parent.getChildNodes()) {
97             if (child instanceof TypedSchemaNode) {
98                 lazy.codecFor((TypedSchemaNode) child);
99                 ++ret;
100             } else if (child instanceof DataNodeContainer) {
101                 ret += requestCodecsForChildren(lazy, (DataNodeContainer) child);
102             }
103         }
104
105         return ret;
106     }
107 }