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.rest.services.impl;
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
14 import javax.ws.rs.core.UriInfo;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
17 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
18 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
22 import org.opendaylight.restconf.common.references.SchemaContextRef;
23 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
24 import org.opendaylight.restconf.handlers.SchemaContextHandler;
25 import org.opendaylight.restconf.rest.services.api.RestconfOperationsService;
26 import org.opendaylight.restconf.utils.RestconfConstants;
27 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Implementation of {@link RestconfOperationsService}
47 public class RestconfOperationsServiceImpl implements RestconfOperationsService {
49 private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
51 private final SchemaContextHandler schemaContextHandler;
52 private final DOMMountPointServiceHandler domMountPointServiceHandler;
55 * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
57 * @param schemaContextHandler
58 * - handling schema context
59 * @param domMountPointServiceHandler
60 * - handling dom mount point service
62 public RestconfOperationsServiceImpl(final SchemaContextHandler schemaContextHandler,
63 final DOMMountPointServiceHandler domMountPointServiceHandler) {
64 this.schemaContextHandler = schemaContextHandler;
65 this.domMountPointServiceHandler = domMountPointServiceHandler;
69 public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
70 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
71 return getOperations(ref.getModules(), null);
75 public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
76 Set<Module> modules = null;
77 DOMMountPoint mountPoint = null;
78 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
79 if (identifier.contains(RestconfConstants.MOUNT)) {
80 final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
82 mountPoint = mountPointIdentifier.getMountPoint();
83 modules = ref.getModules(mountPoint);
86 final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
87 LOG.debug(errMsg + ControllerContext.MOUNT + " for " + identifier);
88 throw new RestconfDocumentedException(errMsg + ControllerContext.MOUNT, ErrorType.PROTOCOL,
89 ErrorTag.INVALID_VALUE);
92 return getOperations(modules, mountPoint);
96 * Special case only for GET restconf/operations use (since moment of old
97 * Yang parser and old Yang model API removal). The method is creating fake
98 * schema context with fake module and fake data by use own implementations
99 * of schema nodes and module.
102 * - set of modules for get RPCs from every module
104 * - mount point, if in use otherwise null
105 * @return {@link NormalizedNodeContext}
107 private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
108 final ContainerSchemaNodeImpl fakeCont = new ContainerSchemaNodeImpl();
109 final List<LeafNode<Object>> listRpcNodes = new ArrayList<>();
110 for (final Module m : modules) {
111 for (final RpcDefinition rpc : m.getRpcs()) {
113 final LeafSchemaNode fakeLeaf = new LeafSchemaNodeImpl(fakeCont.getPath(),
114 QName.create(ModuleImpl.moduleQName, m.getName() + ":" + rpc.getQName().getLocalName()));
115 fakeCont.addNodeChild(fakeLeaf);
116 listRpcNodes.add(Builders.leafBuilder(fakeLeaf).build());
119 final ContainerSchemaNode fakeContSchNode = fakeCont;
120 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
121 .containerBuilder(fakeContSchNode);
123 for (final LeafNode<Object> rpcNode : listRpcNodes) {
124 containerBuilder.withChild(rpcNode);
127 final Module fakeModule = new ModuleImpl(fakeContSchNode);
129 final Set<Module> fakeModules = new HashSet<>();
130 fakeModules.add(fakeModule);
131 final SchemaContext fakeSchemaCtx = EffectiveSchemaContext.resolveSchemaContext(fakeModules);
132 final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext = new InstanceIdentifierContext<>(
133 null, fakeContSchNode, mountPoint, fakeSchemaCtx);
134 return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());