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