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.restful.utils;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
15 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
16 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
17 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
20 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
21 import org.opendaylight.restconf.common.references.SchemaContextRef;
22 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Util class for streams
45 public final class CreateStreamUtil {
47 private static final Logger LOG = LoggerFactory.getLogger(CreateStreamUtil.class);
49 private CreateStreamUtil() {
50 throw new UnsupportedOperationException("Util class");
54 * Create stream with POST operation via RPC
57 * - input of rpc - example in JSON:
63 * "path": "/toaster:toaster/toaster:toasterStatus",
64 * "sal-remote-augment:datastore": "OPERATIONAL",
65 * "sal-remote-augment:scope": "ONE"
72 * - reference to {@link SchemaContext} -
73 * {@link SchemaContextRef}
74 * @return {@link CheckedFuture} with {@link DOMRpcResult} - This mean
75 * output of RPC - example in JSON:
81 * "stream-name": "toaster:toaster/toaster:toasterStatus/datastore=OPERATIONAL/scope=ONE"
88 public static DOMRpcResult createStream(final NormalizedNodeContext payload,
89 final SchemaContextRef refSchemaCtx) {
90 final ContainerNode data = (ContainerNode) payload.getData();
91 final QName qname = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
92 final YangInstanceIdentifier path = preparePath(payload, data, qname);
93 final String streamName = prepareStream(path, refSchemaCtx.get(), data);
95 final QName outputQname = QName.create(qname, "output");
96 final QName streamNameQname = QName.create(qname, "stream-name");
98 final ContainerNode output = ImmutableContainerNodeBuilder.create()
99 .withNodeIdentifier(new NodeIdentifier(outputQname))
100 .withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build();
102 if (!Notificator.existListenerFor(streamName)) {
103 Notificator.createListener(path, streamName);
106 return new DefaultDOMRpcResult(output);
109 private static String prepareStream(final YangInstanceIdentifier path, final SchemaContext schemaContext,
110 final ContainerNode data) {
111 LogicalDatastoreType ds = parseEnum(data, LogicalDatastoreType.class,
112 RestconfStreamsConstants.DATASTORE_PARAM_NAME);
113 ds = ds == null ? RestconfStreamsConstants.DEFAULT_DS : ds;
115 DataChangeScope scope = parseEnum(data, DataChangeScope.class, RestconfStreamsConstants.SCOPE_PARAM_NAME);
116 scope = scope == null ? RestconfStreamsConstants.DEFAULT_SCOPE : scope;
118 final String streamName = Notificator
119 .createStreamNameFromUri(ParserIdentifier.stringFromYangInstanceIdentifier(path, schemaContext)
120 + RestconfStreamsConstants.DS_URI + ds + RestconfStreamsConstants.SCOPE_URI + scope);
121 if((streamName == null) || streamName.equals("")){
122 final String errMsg = "Path is empty or contains value node which is not Container or List build-in type.";
123 LOG.debug(errMsg + path);
124 throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
129 private static <T> T parseEnum(final ContainerNode data, final Class<T> clazz, final String paramName) {
130 final Optional<DataContainerChild<? extends PathArgument, ?>> augNode = data
131 .getChild(RestconfStreamsConstants.SAL_REMOTE_AUG_IDENTIFIER);
132 if (!augNode.isPresent() && !(augNode instanceof AugmentationNode)) {
135 final Optional<DataContainerChild<? extends PathArgument, ?>> enumNode =
136 ((AugmentationNode) augNode.get()).getChild(
137 new NodeIdentifier(QName.create(RestconfStreamsConstants.SAL_REMOTE_AUGMENT, paramName)));
138 if (!enumNode.isPresent()) {
141 final Object value = enumNode.get().getValue();
142 if (!(value instanceof String)) {
146 return StreamUtil.resolveEnum(clazz, (String) value);
149 private static YangInstanceIdentifier preparePath(final NormalizedNodeContext payload, final ContainerNode data,
151 final Optional<DataContainerChild<? extends PathArgument, ?>> path = data
152 .getChild(new YangInstanceIdentifier.NodeIdentifier(QName.create(qName, "path")));
153 Object pathValue = null;
154 if (path.isPresent()) {
155 pathValue = path.get().getValue();
157 if (!(pathValue instanceof YangInstanceIdentifier)) {
158 final String errMsg = "Instance identifier was not normalized correctly ";
159 LOG.debug(errMsg + qName);
160 throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED);
162 return (YangInstanceIdentifier) pathValue;