Move mapModulesByIetfYangLibraryYang()
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / 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.nb.rfc8040.utils.mapping;
9
10 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.$YangModuleInfoImpl.qnameOf;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.net.URI;
14 import java.time.Instant;
15 import java.time.OffsetDateTime;
16 import java.time.ZoneId;
17 import java.time.format.DateTimeFormatter;
18 import java.util.Collection;
19 import java.util.Optional;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
22 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.RestconfState;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Capabilities;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.streams.Stream;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.streams.stream.Access;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
41
42 /**
43  * Util class for mapping nodes.
44  */
45 public final class RestconfMappingNodeUtil {
46     private static final QName CAPABILITY_QNAME = qnameOf("capability");
47     @VisibleForTesting
48     static final QName DESCRIPTION_QNAME = qnameOf("description");
49     @VisibleForTesting
50     static final QName ENCODING_QNAME = qnameOf("encoding");
51     @VisibleForTesting
52     static final QName LOCATION_QNAME = qnameOf("location");
53     @VisibleForTesting
54     static final QName NAME_QNAME = qnameOf("name");
55     @VisibleForTesting
56     static final QName REPLAY_SUPPORT_QNAME = qnameOf("replay-support");
57     @VisibleForTesting
58     static final QName REPLAY_LOG_CREATION_TIME = qnameOf("replay-log-creation-time");
59
60     private RestconfMappingNodeUtil() {
61         // Hidden on purpose
62     }
63
64     /**
65      * Map capabilites by ietf-restconf-monitoring.
66      *
67      * @param monitoringModule ietf-restconf-monitoring module
68      * @return mapped capabilites
69      */
70     public static ContainerNode mapCapabilites(final Module monitoringModule) {
71         return Builders.containerBuilder()
72             .withNodeIdentifier(new NodeIdentifier(RestconfState.QNAME))
73             .withChild(Builders.containerBuilder()
74                 .withNodeIdentifier(new NodeIdentifier(Capabilities.QNAME))
75                 .withChild(Builders.<String>orderedLeafSetBuilder()
76                     .withNodeIdentifier(new NodeIdentifier(CAPABILITY_QNAME))
77                     .withChildValue(Rfc8040.Capabilities.DEPTH)
78                     .withChildValue(Rfc8040.Capabilities.FIELDS)
79                     .withChildValue(Rfc8040.Capabilities.FILTER)
80                     .withChildValue(Rfc8040.Capabilities.REPLAY)
81                     .withChildValue(Rfc8040.Capabilities.WITH_DEFAULTS)
82                     .build())
83                 .build())
84             .build();
85     }
86
87     /**
88      * Map data of yang notification to normalized node according to ietf-restconf-monitoring.
89      *
90      * @param notifiQName qname of notification from listener
91      * @param notifications list of notifications for find schema of notification by notifiQName
92      * @param start start-time query parameter of notification
93      * @param outputType output type of notification
94      * @param uri location of registered listener for sending data of notification
95      * @return mapped data of notification - map entry node if parent exists,
96      *         container streams with list and map entry node if not
97      */
98     public static MapEntryNode mapYangNotificationStreamByIetfRestconfMonitoring(final QName notifiQName,
99             final Collection<? extends NotificationDefinition> notifications, final Instant start,
100             final String outputType, final URI uri) {
101         for (final NotificationDefinition notificationDefinition : notifications) {
102             if (notificationDefinition.getQName().equals(notifiQName)) {
103                 final String streamName = notifiQName.getLocalName();
104                 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamEntry =
105                     Builders.mapEntryBuilder()
106                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName))
107                         .withChild(ImmutableNodes.leafNode(NAME_QNAME, streamName));
108
109                 notificationDefinition.getDescription().ifPresent(
110                     desc -> streamEntry.withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, desc)));
111                 streamEntry.withChild(ImmutableNodes.leafNode(REPLAY_SUPPORT_QNAME, Boolean.TRUE));
112                 if (start != null) {
113                     streamEntry.withChild(ImmutableNodes.leafNode(REPLAY_LOG_CREATION_TIME,
114                         DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(start,
115                             ZoneId.systemDefault()))));
116                 }
117
118                 return streamEntry
119                     .withChild(createAccessList(outputType, uri))
120                     .build();
121             }
122         }
123
124         throw new RestconfDocumentedException(notifiQName + " doesn't exist in any modul");
125     }
126
127     private static MapNode createAccessList(final String outputType, final URI uriToWebsocketServer) {
128         return Builders.mapBuilder()
129             .withNodeIdentifier(new NodeIdentifier(Access.QNAME))
130             .withChild(Builders.mapEntryBuilder()
131                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(Access.QNAME, ENCODING_QNAME, outputType))
132                 .withChild(ImmutableNodes.leafNode(ENCODING_QNAME, outputType))
133                 .withChild(ImmutableNodes.leafNode(LOCATION_QNAME, uriToWebsocketServer.toString()))
134                 .build())
135             .build();
136     }
137
138     /**
139      * Map data of data change notification to normalized node according to ietf-restconf-monitoring.
140      *
141      * @param path path of data to listen on
142      * @param start start-time query parameter of notification
143      * @param outputType output type of notification
144      * @param uri location of registered listener for sending data of notification
145      * @param schemaContext schemaContext for parsing instance identifier to get schema node of data
146      * @return mapped data of notification - map entry node if parent exists,
147      *         container streams with list and map entry node if not
148      */
149     public static MapEntryNode mapDataChangeNotificationStreamByIetfRestconfMonitoring(
150             final YangInstanceIdentifier path, final Instant start, final String outputType, final URI uri,
151             final EffectiveModelContext schemaContext, final String streamName) {
152         final SchemaNode schemaNode = ParserIdentifier
153                 .toInstanceIdentifier(ParserIdentifier.stringFromYangInstanceIdentifier(path, schemaContext),
154                         schemaContext, Optional.empty())
155                 .getSchemaNode();
156         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamEntry =
157             Builders.mapEntryBuilder()
158                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName))
159                 .withChild(ImmutableNodes.leafNode(NAME_QNAME, streamName));
160
161         schemaNode.getDescription().ifPresent(desc ->
162             streamEntry.withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, desc)));
163
164         return streamEntry
165             .withChild(ImmutableNodes.leafNode(REPLAY_SUPPORT_QNAME, Boolean.TRUE))
166             .withChild(ImmutableNodes.leafNode(REPLAY_LOG_CREATION_TIME,
167                 DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(start, ZoneId.systemDefault()))))
168             .withChild(createAccessList(outputType, uri))
169             .build();
170     }
171 }