Add support for formatting EffectiveModelContext
[mdsal.git] / yanglib / mdsal-yanglib-rfc7895 / src / main / java / org / opendaylight / mdsal / yanglib / rfc7895 / YangModuleLibrarySupport.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. 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.mdsal.yanglib.rfc7895;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
19 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
20 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
21 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
22 import org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotBuilder;
23 import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
24 import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
25 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
27 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
28 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
34
35 @Beta
36 @NonNullByDefault
37 @Singleton
38 public final class YangModuleLibrarySupport implements YangLibSupport {
39     private static final Revision REVISION = ModulesState.QNAME.getRevision().orElseThrow();
40
41     private final BindingDataObjectCodecTreeNode<ModulesState> codec;
42     private final EffectiveModelContext context;
43     private final BindingCodecTree codecTree;
44
45     @Inject
46     public YangModuleLibrarySupport(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
47             final BindingCodecTreeFactory codecFactory) throws YangParserException {
48         final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder(parserFactory)
49                 .add(ModulesState.class)
50                 .build();
51         context = snapshot.getEffectiveModelContext();
52
53         codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
54             generator.generateTypeMapping(context), snapshot));
55
56         this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
57     }
58
59     @Override
60     public MountPointContextFactory createMountPointContextFactory(final MountPointIdentifier mountId,
61             final SchemaContextResolver resolver) {
62         return new MountPointContextFactoryImpl(mountId, resolver, context, codec);
63     }
64
65     @Override
66     public Revision implementedRevision() {
67         return REVISION;
68     }
69
70     @Override
71     public YangLibraryContentBuilder newContentBuilder() {
72         return new Rfc7895ContentBuilder(codecTree);
73     }
74 }