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