f57185551259d24c9e0d6907257166f8cd6de16e
[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.parser.api.YangParserException;
33 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37
38 @Beta
39 @NonNullByDefault
40 @Singleton
41 @Component(immediate = true)
42 public final class YangModuleLibrarySupport implements YangLibSupport {
43     private static final Revision REVISION = ModulesState.QNAME.getRevision().orElseThrow();
44
45     private final BindingDataObjectCodecTreeNode<ModulesState> codec;
46     private final EffectiveModelContext context;
47     private final BindingCodecTree codecTree;
48
49     @Inject
50     @Activate
51     public YangModuleLibrarySupport(@Reference final YangParserFactory parserFactory,
52             @Reference final BindingRuntimeGenerator generator, @Reference final BindingCodecTreeFactory codecFactory)
53                 throws YangParserException {
54         final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder(parserFactory)
55                 .add(ModulesState.class)
56                 .build();
57         context = snapshot.getEffectiveModelContext();
58
59         codecTree = codecFactory.create(new DefaultBindingRuntimeContext(
60             generator.generateTypeMapping(context), snapshot));
61
62         this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
63     }
64
65     @Override
66     public MountPointContextFactory createMountPointContextFactory(final MountPointIdentifier mountId,
67             final SchemaContextResolver resolver) {
68         return new MountPointContextFactoryImpl(mountId, resolver, context, codec);
69     }
70
71     @Override
72     public Revision implementedRevision() {
73         return REVISION;
74     }
75
76     @Override
77     public YangLibraryContentBuilder newContentBuilder() {
78         return new Rfc7895ContentBuilder(codecTree);
79     }
80 }