Add support for formatting EffectiveModelContext
[mdsal.git] / yanglib / mdsal-yanglib-rfc8525 / src / main / java / org / opendaylight / mdsal / yanglib / rfc8525 / YangLibraryContentBuilderImpl.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.Preconditions.checkState;
11 import static com.google.common.base.Verify.verifyNotNull;
12
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Objects;
16 import java.util.Optional;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
22 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilder;
23 import org.opendaylight.mdsal.yanglib.api.YangLibraryContentBuilderWithLegacy;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.Module;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.ModuleBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.module.Submodule;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.module.SubmoduleBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSet;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSetBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSetKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.Revision;
38 import org.opendaylight.yangtools.yang.data.api.DatastoreIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41
42 class YangLibraryContentBuilderImpl implements YangLibraryContentBuilder {
43     private static final String MODULE_SET_NAME = "ODL_modules";
44
45     private final Map<DatastoreIdentifier, EffectiveModelContext> datastores = new HashMap<>();
46     private final BindingDataObjectCodecTreeNode<YangLibrary> codec;
47     private final BindingCodecTree codecTree;
48
49     private EffectiveModelContext modelContext;
50
51     YangLibraryContentBuilderImpl(final BindingCodecTree codecTree) {
52         this.codecTree = Objects.requireNonNull(codecTree);
53         this.codec = verifyNotNull(codecTree.getSubtreeCodec(InstanceIdentifier.create(YangLibrary.class)));
54     }
55
56     @Override
57     public YangLibraryContentBuilder defaultContext(final EffectiveModelContext context) {
58         modelContext = context;
59         return this;
60     }
61
62     EffectiveModelContext getModelContext() {
63         return modelContext;
64     }
65
66     @Override
67     public YangLibraryContentBuilderWithLegacy includeLegacy() {
68         return new LegacyContentBuilder(this, codecTree);
69     }
70
71     @Override
72     public YangLibraryContentBuilder addDatastore(final DatastoreIdentifier identifier,
73                                                   final EffectiveModelContext context) {
74         datastores.put(identifier, context);
75         return this;
76     }
77
78     @Override
79     public ContainerNode formatYangLibraryContent() {
80         checkState(modelContext != null, "EffectiveModelContext is required to format YangLibrary content");
81         return formatYangLibrary();
82     }
83
84     @NonNull ContainerNode formatYangLibrary() {
85         // Two-step process: we first build the content and then use hashCode() to generate module-set-id
86         final YangLibraryBuilder builder = new YangLibraryBuilder().setContentId("");
87         final ModuleSetBuilder moduleSetBuilder = new ModuleSetBuilder()
88                 .setModule(modelContext.getModules().stream()
89                         .map(this::buildModule)
90                         .collect(Collectors.toUnmodifiableMap(Module::key, Function.identity())))
91                 .setName(MODULE_SET_NAME);
92         final ModuleSet moduleSet = moduleSetBuilder.build();
93
94         builder.setModuleSet(Map.of(new ModuleSetKey(moduleSet.getName()), moduleSet));
95         return (ContainerNode) codec.serialize(builder.setContentId(String.valueOf(builder.build().hashCode()))
96             .build());
97     }
98
99     private Module buildModule(final org.opendaylight.yangtools.yang.model.api.Module module) {
100         return new ModuleBuilder()
101                 .setName(new YangIdentifier(module.getName()))
102                 .setNamespace(new Uri(module.getQNameModule().getNamespace().toString()))
103                 .setRevision(convertRevision(module.getRevision()))
104                 .setSubmodule(module.getSubmodules().stream()
105                         .map(submodule -> new SubmoduleBuilder()
106                                 .setName(new YangIdentifier(submodule.getName()))
107                                 .setRevision(convertRevision(submodule.getRevision()))
108                                 .build())
109                         .collect(Collectors.toUnmodifiableMap(Submodule::key, Function.identity())))
110                 .setFeature(module.getFeatures().stream()
111                         .map(feat -> new YangIdentifier(feat.getQName().getLocalName()))
112                         .collect(Collectors.toUnmodifiableList()))
113                 .build();
114     }
115
116     private static RevisionIdentifier convertRevision(final Optional<Revision> revision) {
117         return revision.map(rev -> new RevisionIdentifier(rev.toString())).orElse(null);
118     }
119 }