ded6d9739a71e36eca2857d088e4054d4cf3aa74
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / utils / mapping / RestconfMappingNodeUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.restconf.utils.mapping;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Set;
13 import org.opendaylight.restconf.Draft17;
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;
30
31 /**
32  * Util class for mapping nodes
33  *
34  */
35 public final class RestconfMappingNodeUtil {
36
37     private RestconfMappingNodeUtil() {
38         throw new UnsupportedOperationException("Util class");
39     }
40
41     /**
42      * Mapping {@link Module} from {@link Set} of {@link Module} to
43      * {@link ListSchemaNode} of {@link Module} list.
44      *
45      * @param restconfModule
46      *            - restconf module
47      * @param modules
48      *            - all modules
49      * @return {@link MapNode}
50      */
51     public static MapNode restconfMappingNode(final Module restconfModule, final Set<Module> modules) {
52         final DataSchemaNode modulListSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(restconfModule,
53                 Draft17.RestconfModule.MODULE_LIST_SCHEMA_NODE);
54         Preconditions.checkState(modulListSchemaNode instanceof ListSchemaNode);
55
56         final CollectionNodeBuilder<MapEntryNode, MapNode> listModuleBuilder = Builders
57                 .mapBuilder((ListSchemaNode) modulListSchemaNode);
58         for (final Module module : modules) {
59             listModuleBuilder.withChild(toModuleEntryNode(module, modulListSchemaNode));
60         }
61         return listModuleBuilder.build();
62     }
63
64     /**
65      * Mapping {@link MapEntryNode} entries of {@link Module} to
66      * {@link ListSchemaNode}.
67      *
68      * @param module
69      *            - module for mapping
70      * @param modulListSchemaNode
71      *            - mapped {@link DataSchemaNode}
72      * @return {@link MapEntryNode}
73      */
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);
79
80         // MODULE NAME SCHEMA NODE
81         fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.NAME, module.getName());
82
83         // MODULE REVISION SCHEMA NODE
84         fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.REVISION,
85                 RestconfConstants.REVISION_FORMAT.format(module.getRevision()));
86
87         // MODULE NAMESPACE SCHEMA NODE
88         fillListWithLeaf(listSchemaNode, moduleNodeValues, RestconfMappingNodeConstants.NAMESPACE,
89                 module.getNamespace().toString());
90
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());
100         }
101         moduleNodeValues.withChild(featureBuilder.build());
102
103         return moduleNodeValues.build();
104     }
105
106     /**
107      * Mapping {@link MapEntryNode} stream entries of stream to
108      * {@link ListSchemaNode}
109      *
110      * @param streamName
111      *            - stream name
112      * @param streamListSchemaNode
113      *            - mapped {@link DataSchemaNode}
114      * @return {@link MapEntryNode}
115      */
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);
121
122         // STREAM NAME
123         fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.NAME, streamName);
124
125         // STREAM DESCRIPTION
126         fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.DESCRIPTION,
127                 RestconfMappingStreamConstants.DESCRIPTION);
128
129         // STREAM REPLAY_SUPPORT
130         fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.REPLAY_SUPPORT,
131                 RestconfMappingStreamConstants.REPLAY_SUPPORT);
132
133         // STREAM REPLAY_LOG
134         fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.REPLAY_LOG,
135                 RestconfMappingStreamConstants.REPLAY_LOG);
136
137         // STREAM EVENTS
138         fillListWithLeaf(listStreamSchemaNode, streamNodeValues, RestconfMappingNodeConstants.EVENTS,
139                 RestconfMappingStreamConstants.EVENTS);
140
141         return streamNodeValues.build();
142     }
143
144     /**
145      * Method for filling {@link ListSchemaNode} with {@link LeafSchemaNode}
146      *
147      * @param listStreamSchemaNode
148      *            - {@link ListSchemaNode}
149      * @param streamNodeValues
150      *            - filled {@link DataContainerNodeAttrBuilder}
151      * @param nameSchemaNode
152      *            - name of mapped leaf
153      * @param value
154      *            - value for mapped node
155      */
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());
164     }
165 }