Add support for formatting EffectiveModelContext
[mdsal.git] / yanglib / mdsal-yanglib-rfc8525 / src / main / java / org / opendaylight / mdsal / yanglib / rfc8525 / YangLibrarySupport.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.rfc8525;
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.dom.codec.api.BindingIdentityCodec;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
21 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
22 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
23 import org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotBuilder;
24 import org.opendaylight.mdsal.yanglib.api.SchemaContextResolver;
25 import org.opendaylight.mdsal.yanglib.api.YangLibSupport;
26 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
30 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
31 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
37
38 @Beta
39 @NonNullByDefault
40 @Singleton
41 public final class YangLibrarySupport implements YangLibSupport {
42     private static final Revision REVISION = YangLibrary.QNAME.getRevision().orElseThrow();
43     private static final RevisionIdentifier EMPTY_REV = new RevisionIdentifier("1970-01-01");
44     private static final String MODULE_SET_NAME = "ODL_modules";
45
46     private final BindingDataObjectCodecTreeNode<YangLibrary> codec;
47     @SuppressWarnings("deprecation")
48     private final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec;
49     private final BindingIdentityCodec identityCodec;
50     private final EffectiveModelContext context;
51     private final BindingCodecTree codecTree;
52
53     @Inject
54     public YangLibrarySupport(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
55             final BindingCodecTreeFactory codecFactory) throws YangParserException {
56         final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder(parserFactory)
57                 .add(YangLibrary.class)
58                 .build();
59         context = snapshot.getEffectiveModelContext();
60
61         codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
62             generator.generateTypeMapping(context), snapshot));
63
64         this.identityCodec = codecTree.getIdentityCodec();
65         this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class)));
66         this.legacyCodec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
67     }
68
69     @Override
70     public MountPointContextFactory createMountPointContextFactory(final MountPointIdentifier mountId,
71             final SchemaContextResolver resolver) {
72         return new MountPointContextFactoryImpl(mountId, resolver, context, identityCodec, codec, legacyCodec);
73     }
74
75     @Override
76     public Revision implementedRevision() {
77         return REVISION;
78     }
79
80     @Override
81     public YangLibraryContentBuilder newContentBuilder() {
82         return new YangLibraryContentBuilderImpl(codecTree);
83     }
84 }