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.base.services.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
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.base.services.api.RestconfOperationsService;
24 import org.opendaylight.restconf.common.references.SchemaContextRef;
25 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
26 import org.opendaylight.restconf.handlers.SchemaContextHandler;
27 import org.opendaylight.restconf.utils.RestconfConstants;
28 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
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.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Implementation of {@link RestconfOperationsService}.
46 public class RestconfOperationsServiceImpl implements RestconfOperationsService {
48 private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
50 private final SchemaContextHandler schemaContextHandler;
51 private final DOMMountPointServiceHandler domMountPointServiceHandler;
54 * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}.
56 * @param schemaContextHandler
57 * handling schema context
58 * @param domMountPointServiceHandler
59 * handling dom mount point service
61 public RestconfOperationsServiceImpl(final SchemaContextHandler schemaContextHandler,
62 final DOMMountPointServiceHandler domMountPointServiceHandler) {
63 this.schemaContextHandler = schemaContextHandler;
64 this.domMountPointServiceHandler = domMountPointServiceHandler;
68 public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
69 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
70 return getOperations(ref.getModules(), null);
74 public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
75 final Set<Module> modules;
76 final DOMMountPoint mountPoint;
77 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
78 if (identifier.contains(RestconfConstants.MOUNT)) {
79 final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
80 ref.get(), Optional.of(this.domMountPointServiceHandler.get()));
81 mountPoint = mountPointIdentifier.getMountPoint();
82 modules = ref.getModules(mountPoint);
85 "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
86 LOG.debug(errMsg + ControllerContext.MOUNT + " for " + identifier);
87 throw new RestconfDocumentedException(errMsg + ControllerContext.MOUNT, ErrorType.PROTOCOL,
88 ErrorTag.INVALID_VALUE);
91 return getOperations(modules, mountPoint);
95 * Special case only for GET restconf/operations use (since moment of old
96 * Yang parser and old Yang model API removal). The method is creating fake
97 * schema context with fake module and fake data by use own implementations
98 * of schema nodes and module.
101 * set of modules for get RPCs from every module
103 * mount point, if in use otherwise null
104 * @return {@link NormalizedNodeContext}
106 private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
107 final Collection<Module> neededModules = new ArrayList<>(modules.size());
108 final ArrayList<LeafSchemaNode> fakeRpcSchema = new ArrayList<>();
110 for (final Module m : modules) {
111 final Set<RpcDefinition> rpcs = m.getRpcs();
112 if (!rpcs.isEmpty()) {
113 neededModules.add(m);
115 fakeRpcSchema.ensureCapacity(fakeRpcSchema.size() + rpcs.size());
116 rpcs.forEach(rpc -> fakeRpcSchema.add(new FakeLeafSchemaNode(rpc.getQName())));
120 final ContainerSchemaNode fakeCont = new FakeContainerSchemaNode(fakeRpcSchema);
121 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder =
122 Builders.containerBuilder(fakeCont);
124 for (final LeafSchemaNode leaf : fakeRpcSchema) {
125 containerBuilder.withChild(Builders.leafBuilder(leaf).build());
128 final Collection<Module> fakeModules = new ArrayList<>(neededModules.size() + 1);
129 neededModules.forEach(imp -> fakeModules.add(new FakeImportedModule(imp)));
130 fakeModules.add(new FakeRestconfModule(neededModules, fakeCont));
132 // FIXME: use a separate sublcass of AbstractSchemaContext
133 final SchemaContext fakeSchemaCtx =
134 EffectiveSchemaContext.resolveSchemaContext(ImmutableSet.copyOf(fakeModules));
135 final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext =
136 new InstanceIdentifierContext<>(null, fakeCont, mountPoint, fakeSchemaCtx);
137 return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());