Update binding-dom adaptation to remove AugmentationNode
[mdsal.git] / yanglib / mdsal-yanglib-rfc8525 / src / main / java / org / opendaylight / mdsal / yanglib / rfc8525 / LegacyContentBuilder.java
1 /*
2  * Copyright (c) 2020 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 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.VisibleForTesting;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
18 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
19 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilderWithLegacy;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.SubmoduleBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
31 import org.opendaylight.yangtools.yang.common.Revision;
32 import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35
36 final class LegacyContentBuilder implements YangLibraryContentBuilderWithLegacy {
37     private static final CommonLeafs.Revision EMPTY_REV = new CommonLeafs.Revision("");
38
39     private final BindingDataObjectCodecTreeNode<ModulesState> legacyCodec;
40     private final YangLibraryContentBuilderImpl delegate;
41
42     LegacyContentBuilder(final YangLibraryContentBuilderImpl delegate, final BindingCodecTree codecTree) {
43         this.delegate = requireNonNull(delegate);
44         legacyCodec = (BindingDataObjectCodecTreeNode<ModulesState>)
45             verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(ModulesState.class)));
46     }
47
48     @Override
49     public LegacyContentBuilder defaultContext(final EffectiveModelContext context) {
50         delegate.defaultContext(context);
51         return this;
52     }
53
54     @Override
55     public YangLibraryContentBuilder addDatastore(final DatastoreIdentifier identifier,
56             final EffectiveModelContext context) {
57         return delegate.addDatastore(identifier, context);
58     }
59
60     @Override
61     public YangLibraryContentBuilderWithLegacy includeLegacy() {
62         return this;
63     }
64
65     @Override
66     public ContainerNode formatYangLibraryContent() {
67         return delegate.formatYangLibrary();
68     }
69
70     @Override
71     public Optional<ContainerNode> formatYangLibraryLegacyContent() {
72         return Optional.of(formatModulesState(requireNonNull(delegate.getModelContext())));
73     }
74
75     @VisibleForTesting
76     ContainerNode formatModulesState(final EffectiveModelContext context) {
77         // Two-step process: we first build the content and then use hashCode() to generate module-set-id
78         final ModulesStateBuilder builder = new ModulesStateBuilder()
79             .setModuleSetId("")
80             .setModule(context.getModules().stream()
81                 .map(module -> new ModuleBuilder()
82                     .setName(new YangIdentifier(module.getName()))
83                     .setNamespace(new Uri(module.getNamespace().toString()))
84                     .setRevision(convertRevision(module.getRevision()))
85                     .setSubmodule(module.getSubmodules().stream()
86                         .map(submodule -> new SubmoduleBuilder()
87                             .setName(new YangIdentifier(submodule.getName()))
88                             .setRevision(convertRevision(submodule.getRevision()))
89                             .build())
90                         .collect(BindingMap.toMap()))
91                     .setFeature(module.getFeatures().stream()
92                         .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
93                         .collect(Collectors.toUnmodifiableSet()))
94                     .setConformanceType(Module.ConformanceType.Implement)
95                     .build())
96                 .collect(BindingMap.toMap()));
97
98         return (ContainerNode) legacyCodec.serialize(builder.setModuleSetId(String.valueOf(builder.build().hashCode()))
99             .build());
100     }
101
102     private static CommonLeafs.Revision convertRevision(final Optional<Revision> revision) {
103         return revision.map(rev -> new CommonLeafs.Revision(new RevisionIdentifier(rev.toString()))).orElse(EMPTY_REV);
104     }
105 }