2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.utils.mapping;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
13 import org.opendaylight.restconf.Draft16;
14 import org.opendaylight.restconf.utils.RestconfConstants;
15 import org.opendaylight.restconf.utils.schema.context.RestconfSchemaUtil;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
32 * Util class for mapping nodes
35 public final class RestconfMappingNodeUtil {
37 private RestconfMappingNodeUtil() {
38 throw new UnsupportedOperationException("Util class");
42 * Mapping {@link Module} from {@link Set} of {@link Module} to
43 * {@link ListSchemaNode} of {@link Module} list.
45 * @param restconfModule
49 * @return {@link MapNode}
51 public static MapNode restconfMappingNode(final Module restconfModule, final Set<Module> modules) {
52 final DataSchemaNode modulListSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(restconfModule,
53 Draft16.RestconfModule.MODULE_LIST_SCHEMA_NODE);
54 Preconditions.checkState(modulListSchemaNode instanceof ListSchemaNode);
56 final CollectionNodeBuilder<MapEntryNode, MapNode> listModuleBuilder = Builders
57 .mapBuilder((ListSchemaNode) modulListSchemaNode);
58 for (final Module module : modules) {
59 listModuleBuilder.withChild(toModuleEntryNode(module, modulListSchemaNode));
61 return listModuleBuilder.build();
65 * Mapping {@link MapEntryNode} entries of {@link Module} to
66 * {@link ListSchemaNode}.
69 * - module for mapping
70 * @param modulListSchemaNode
71 * - mapped {@link DataSchemaNode}
72 * @return {@link MapEntryNode}
74 private static MapEntryNode toModuleEntryNode(final Module module, final DataSchemaNode modulListSchemaNode) {
75 final ListSchemaNode listSchemaNode = (ListSchemaNode) modulListSchemaNode;
76 final Collection<DataSchemaNode> childListSchemaNode = listSchemaNode.getChildNodes();
77 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> moduleNodeValues = Builders
78 .mapEntryBuilder(listSchemaNode);
80 // MODULE NAME SCHEMA NODE
81 fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.NAME, module.getName());
83 // MODULE REVISION SCHEMA NODE
84 fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.REVISION,
85 RestconfConstants.REVISION_FORMAT.format(module.getRevision()));
87 // MODULE NAMESPACE SCHEMA NODE
88 fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.NAMESPACE,
89 module.getNamespace().toString());
91 // MODULE FEATURES SCHEMA NODES
92 final DataSchemaNode schemaNode = RestconfSchemaUtil.findSchemaNodeInCollection(childListSchemaNode,
93 RestconfMappingNodeConstants.FEATURE);
94 Preconditions.checkState(schemaNode instanceof LeafListSchemaNode);
95 final ListNodeBuilder<Object, LeafSetEntryNode<Object>> featureBuilder = Builders
96 .leafSetBuilder((LeafListSchemaNode) schemaNode);
97 for (final FeatureDefinition feature : module.getFeatures()) {
98 featureBuilder.withChild(Builders.leafSetEntryBuilder((LeafListSchemaNode) schemaNode)
99 .withValue(feature.getQName().getLocalName()).build());
101 moduleNodeValues.withChild(featureBuilder.build());
103 return moduleNodeValues.build();
107 * Mapping {@link MapEntryNode} stream entries of stream to
108 * {@link ListSchemaNode}
112 * @param streamListSchemaNode
113 * - mapped {@link DataSchemaNode}
114 * @return {@link MapEntryNode}
116 public static MapEntryNode toStreamEntryNode(final String streamName, final DataSchemaNode streamListSchemaNode) {
117 Preconditions.checkState(streamListSchemaNode instanceof ListSchemaNode);
118 final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamListSchemaNode;
119 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues = Builders
120 .mapEntryBuilder(listStreamSchemaNode);
123 fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.NAME, streamName);
125 // STREAM DESCRIPTION
126 fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.DESCRIPTION,
127 RestconfMappingStreamConstants.DESCRIPTION);
129 // STREAM REPLAY_SUPPORT
130 fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.REPLAY_SUPPORT,
131 RestconfMappingStreamConstants.REPLAY_SUPPORT);
134 fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.REPLAY_LOG,
135 RestconfMappingStreamConstants.REPLAY_LOG);
138 fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.EVENTS,
139 RestconfMappingStreamConstants.EVENTS);
141 return streamNodeValues.build();
145 * Method for filling {@link ListSchemaNode} with {@link LeafSchemaNode}
147 * @param listStreamSchemaNode
148 * - {@link ListSchemaNode}
149 * @param streamNodeValues
150 * - filled {@link DataContainerNodeAttrBuilder}
151 * @param nameSchemaNode
152 * - name of mapped leaf
154 * - value for mapped node
156 private static void fillListWithLeaf(
157 final ListSchemaNode listStreamSchemaNode,
158 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues,
159 final String nameSchemaNode, final Object value) {
160 final DataSchemaNode schemaNode = RestconfSchemaUtil
161 .findSchemaNodeInCollection(listStreamSchemaNode.getChildNodes(), nameSchemaNode);
162 Preconditions.checkState(schemaNode instanceof LeafSchemaNode);
163 streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) schemaNode).withValue(value).build());