X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestconfImpl.xtend;h=f1901d711259f1907aeb9f94817ffbf84694fffd;hp=be170c75b0db23c91e65f4ada96fcc10f919b11c;hb=9b843f3565f84258ebea1b437ae1025dfd4a52d2;hpb=681385ead5cb32ecdc5cd28c319f8f5c8894b138 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend index be170c75b0..f1901d7112 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.xtend @@ -8,6 +8,8 @@ package org.opendaylight.controller.sal.restconf.impl import com.google.common.base.Preconditions +import com.google.common.base.Splitter +import com.google.common.collect.Lists import java.net.URI import java.text.ParseException import java.text.SimpleDateFormat @@ -16,9 +18,12 @@ import java.util.HashMap import java.util.List import java.util.Set import javax.ws.rs.core.Response +import javax.ws.rs.core.UriInfo import org.opendaylight.controller.md.sal.common.api.TransactionStatus import org.opendaylight.controller.sal.core.api.mount.MountInstance import org.opendaylight.controller.sal.rest.api.RestconfService +import org.opendaylight.controller.sal.streams.listeners.Notificator +import org.opendaylight.controller.sal.streams.websockets.WebSocketServer import org.opendaylight.yangtools.yang.common.QName import org.opendaylight.yangtools.yang.common.RpcResult import org.opendaylight.yangtools.yang.data.api.CompositeNode @@ -37,13 +42,11 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition import org.opendaylight.yangtools.yang.model.api.SchemaContext import org.opendaylight.yangtools.yang.model.api.TypeDefinition import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition +import org.opendaylight.yangtools.yang.model.util.EmptyType +import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder +import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder import static javax.ws.rs.core.Response.Status.* -import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder -import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder -import org.opendaylight.yangtools.yang.model.util.EmptyType -import com.google.common.base.Splitter -import com.google.common.collect.Lists class RestconfImpl implements RestconfService { @@ -57,7 +60,11 @@ class RestconfImpl implements RestconfService { val static RESTCONF_MODULE_DRAFT02_RESTCONF_CONTAINER_SCHEMA_NODE = "restconf" val static RESTCONF_MODULE_DRAFT02_MODULES_CONTAINER_SCHEMA_NODE = "modules" val static RESTCONF_MODULE_DRAFT02_MODULE_LIST_SCHEMA_NODE = "module" + val static RESTCONF_MODULE_DRAFT02_STREAMS_CONTAINER_SCHEMA_NODE = "streams" + val static RESTCONF_MODULE_DRAFT02_STREAM_LIST_SCHEMA_NODE = "stream" val static RESTCONF_MODULE_DRAFT02_OPERATIONS_CONTAINER_SCHEMA_NODE = "operations" + val static SAL_REMOTE_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote" + val static SAL_REMOTE_RPC_SUBSRCIBE = "create-data-change-event-subscription" @Property BrokerFacade broker @@ -87,6 +94,17 @@ class RestconfImpl implements RestconfService { return new StructuredData(modulesNode, modulesSchemaNode, null) } + override getAvailableStreams(){ + var Set availableStreams = Notificator.getStreamNames(); + val List> streamsAsData = new ArrayList + val streamSchemaNode = restconfModule.getSchemaNode(RESTCONF_MODULE_DRAFT02_STREAM_LIST_SCHEMA_NODE) + for (String streamName:availableStreams){ + streamsAsData.add(streamName.toStreamCompositeNode(streamSchemaNode)) + } + val streamsSchemaNode = restconfModule.getSchemaNode(RESTCONF_MODULE_DRAFT02_STREAMS_CONTAINER_SCHEMA_NODE) + val streamsNode = NodeFactory.createImmutableCompositeNode(streamsSchemaNode.QName, null, streamsAsData) + return new StructuredData(streamsNode, streamsSchemaNode, null) + } override getModules(String identifier) { var Set modules = null var MountInstance mountPoint = null @@ -177,7 +195,7 @@ class RestconfImpl implements RestconfService { } else ( moduleNameAndRevision = identifier ) - val pathArgs = Lists.newArrayList(Splitter.on("/").omitEmptyStrings.split(moduleNameAndRevision)) + val pathArgs = Lists.newArrayList(Splitter.on("/").omitEmptyStrings.split(moduleNameAndRevision)) if (pathArgs.length < 2) { throw new ResponseException(BAD_REQUEST, "URI has bad format. End of URI should be in format 'moduleName/yyyy-MM-dd'") @@ -191,6 +209,25 @@ class RestconfImpl implements RestconfService { } } + private def CompositeNode toStreamCompositeNode(String streamName, DataSchemaNode streamSchemaNode) { + val List> streamNodeValues = new ArrayList + val nameSchemaNode = (streamSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("name").head + streamNodeValues.add(NodeFactory.createImmutableSimpleNode(nameSchemaNode.QName, null, streamName)) + + val descriptionSchemaNode = (streamSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("description").head + streamNodeValues.add(NodeFactory.createImmutableSimpleNode(descriptionSchemaNode.QName, null, "DESCRIPTION_PLACEHOLDER")) + + val replaySupportSchemaNode = (streamSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("replay-support").head + streamNodeValues.add(NodeFactory.createImmutableSimpleNode(replaySupportSchemaNode.QName, null, true)) + + val replayLogCreationTimeSchemaNode = (streamSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("replay-log-creation-time").head + streamNodeValues.add(NodeFactory.createImmutableSimpleNode(replayLogCreationTimeSchemaNode.QName, null, "")) + + val eventsSchemaNode = (streamSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("events").head + streamNodeValues.add(NodeFactory.createImmutableSimpleNode(eventsSchemaNode.QName, null, "")) + + return NodeFactory.createImmutableCompositeNode(streamSchemaNode.QName, null, streamNodeValues) + } private def CompositeNode toModuleCompositeNode(Module module, DataSchemaNode moduleSchemaNode) { val List> moduleNodeValues = new ArrayList val nameSchemaNode = (moduleSchemaNode as DataNodeContainer).findInstanceDataChildrenByName("name").head @@ -211,7 +248,12 @@ class RestconfImpl implements RestconfService { val restconfContainer = restconfGrouping.findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_RESTCONF_CONTAINER_SCHEMA_NODE).head if (schemaNodeName == RESTCONF_MODULE_DRAFT02_OPERATIONS_CONTAINER_SCHEMA_NODE) { return (restconfContainer as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_OPERATIONS_CONTAINER_SCHEMA_NODE).head - } else if (schemaNodeName == RESTCONF_MODULE_DRAFT02_MODULES_CONTAINER_SCHEMA_NODE) { + } else if (schemaNodeName == RESTCONF_MODULE_DRAFT02_STREAMS_CONTAINER_SCHEMA_NODE) { + return (restconfContainer as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_STREAMS_CONTAINER_SCHEMA_NODE).head + } else if (schemaNodeName == RESTCONF_MODULE_DRAFT02_STREAM_LIST_SCHEMA_NODE) { + val modules = (restconfContainer as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_STREAMS_CONTAINER_SCHEMA_NODE).head + return (modules as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_STREAM_LIST_SCHEMA_NODE).head + }else if (schemaNodeName == RESTCONF_MODULE_DRAFT02_MODULES_CONTAINER_SCHEMA_NODE) { return (restconfContainer as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_MODULES_CONTAINER_SCHEMA_NODE).head } else if (schemaNodeName == RESTCONF_MODULE_DRAFT02_MODULE_LIST_SCHEMA_NODE) { val modules = (restconfContainer as DataNodeContainer).findInstanceDataChildrenByName(RESTCONF_MODULE_DRAFT02_MODULES_CONTAINER_SCHEMA_NODE).head @@ -225,6 +267,33 @@ class RestconfImpl implements RestconfService { } override invokeRpc(String identifier, CompositeNode payload) { + val rpc = resolveIdentifierInInvokeRpc(identifier) + if (rpc.QName.namespace.toString == SAL_REMOTE_NAMESPACE && rpc.QName.localName == SAL_REMOTE_RPC_SUBSRCIBE) { + val value = normalizeNode(payload, rpc.input, null) + val pathNode = value?.getFirstSimpleByName(QName.create(rpc.QName, "path")) + val pathValue = pathNode?.value + if (pathValue === null && !(pathValue instanceof InstanceIdentifier)) { + throw new ResponseException(INTERNAL_SERVER_ERROR, "Instance identifier was not normalized correctly."); + } + val pathIdentifier = (pathValue as InstanceIdentifier) + var String streamName = null + if (!pathIdentifier.path.nullOrEmpty) { + streamName = Notificator.createStreamNameFromUri(pathIdentifier.toFullRestconfIdentifier) + } + if (streamName.nullOrEmpty) { + throw new ResponseException(BAD_REQUEST, "Path is empty or contains data node which is not Container or List build-in type."); + } + val streamNameNode = NodeFactory.createImmutableSimpleNode(QName.create(rpc.output.QName, "stream-name"), null, streamName) + val List> output = new ArrayList + output.add(streamNameNode) + val responseData = NodeFactory.createMutableCompositeNode(rpc.output.QName, null, output, null, null) + + if (!Notificator.existListenerFor(pathIdentifier)) { + Notificator.createListener(pathIdentifier, streamName) + } + + return new StructuredData(responseData, rpc.output, null) + } return callRpc(identifier.rpcDefinition, payload) } @@ -232,7 +301,22 @@ class RestconfImpl implements RestconfService { if (!noPayload.nullOrEmpty) { throw new ResponseException(UNSUPPORTED_MEDIA_TYPE, "Content-Type contains unsupported Media Type."); } - return callRpc(identifier.rpcDefinition, null) + val rpc = resolveIdentifierInInvokeRpc(identifier) + return callRpc(rpc, null) + } + + private def resolveIdentifierInInvokeRpc(String identifier) { + if (identifier.indexOf("/") === -1) { + val identifierDecoded = identifier.urlPathArgDecode + val rpc = identifierDecoded.rpcDefinition + if (rpc !== null) { + return rpc + } + throw new ResponseException(NOT_FOUND, "RPC does not exist."); + } + val slashErrorMsg = String.format( + "Identifier %n%s%ncan't contain slash character (/).%nIf slash is part of identifier name then use %%2F placeholder.", identifier) + throw new ResponseException(NOT_FOUND, slashErrorMsg); } private def StructuredData callRpc(RpcDefinition rpc, CompositeNode payload) { @@ -382,6 +466,21 @@ class RestconfImpl implements RestconfService { } } + override subscribeToStream(String identifier, UriInfo uriInfo) { + val streamName = Notificator.createStreamNameFromUri(identifier) + if (streamName.nullOrEmpty) { + throw new ResponseException(BAD_REQUEST, "Stream name is empty.") + } + val listener = Notificator.getListenerFor(streamName); + if (listener === null) { + throw new ResponseException(BAD_REQUEST, "Stream was not found.") + } + broker.registerToListenDataChanges(listener) + val uriBuilder = uriInfo.getAbsolutePathBuilder() + val uriToWebsocketServer = uriBuilder.port(WebSocketServer.PORT).replacePath(streamName).build() + return Response.status(OK).location(uriToWebsocketServer).build + } + private def dispatch URI namespace(CompositeNode data) { return data.nodeType.namespace } @@ -489,7 +588,11 @@ class RestconfImpl implements RestconfService { } if (node instanceof CompositeNodeWrapper) { if ((node as CompositeNodeWrapper).changeAllowed) { - normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint) + try { + normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint) + } catch (NumberFormatException e) { + throw new ResponseException(BAD_REQUEST,e.message) + } } return (node as CompositeNodeWrapper).unwrap() } @@ -570,7 +673,7 @@ class RestconfImpl implements RestconfService { if (schema.typeDefinition instanceof IdentityrefTypeDefinition) { if (value instanceof String) { - inputValue = new IdentityValuesDTO(nodeBuilder.namespace.toString, value as String, null) + inputValue = new IdentityValuesDTO(nodeBuilder.namespace.toString, value as String, null,value as String); } // else value is already instance of IdentityValuesDTO }