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.nb.rfc8040.services.simple.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.restconf.common.context.InstanceIdentifierContext;
18 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
21 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
24 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
25 import org.opendaylight.restconf.nb.rfc8040.services.simple.api.RestconfOperationsService;
26 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
27 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
28 import org.opendaylight.yangtools.yang.common.Empty;
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.model.util.SimpleSchemaContext;
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 SchemaContextHandler schemaContextHandler;
51 private 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 synchronized void updateHandlers(final Object... handlers) {
69 for (final Object object : handlers) {
70 if (object instanceof SchemaContextHandler) {
71 schemaContextHandler = (SchemaContextHandler) object;
72 } else if (object instanceof DOMMountPointServiceHandler) {
73 domMountPointServiceHandler = (DOMMountPointServiceHandler) object;
79 public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
80 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
81 return getOperations(ref.getModules(), null);
85 public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
86 final Set<Module> modules;
87 final DOMMountPoint mountPoint;
88 final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
89 if (identifier.contains(RestconfConstants.MOUNT)) {
90 final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
91 ref.get(), Optional.of(this.domMountPointServiceHandler.get()));
92 mountPoint = mountPointIdentifier.getMountPoint();
93 modules = ref.getModules(mountPoint);
96 "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
97 LOG.debug(errMsg + RestconfConstants.MOUNT + " for " + identifier);
98 throw new RestconfDocumentedException(errMsg + RestconfConstants.MOUNT, ErrorType.PROTOCOL,
99 ErrorTag.INVALID_VALUE);
102 return getOperations(modules, mountPoint);
106 * Special case only for GET restconf/operations use (since moment of old
107 * Yang parser and old Yang model API removal). The method is creating fake
108 * schema context with fake module and fake data by use own implementations
109 * of schema nodes and module.
112 * set of modules for get RPCs from every module
114 * mount point, if in use otherwise null
115 * @return {@link NormalizedNodeContext}
117 private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
118 final Collection<Module> neededModules = new ArrayList<>(modules.size());
119 final ArrayList<LeafSchemaNode> fakeRpcSchema = new ArrayList<>();
121 for (final Module m : modules) {
122 final Set<RpcDefinition> rpcs = m.getRpcs();
123 if (!rpcs.isEmpty()) {
124 neededModules.add(m);
126 fakeRpcSchema.ensureCapacity(fakeRpcSchema.size() + rpcs.size());
127 rpcs.forEach(rpc -> fakeRpcSchema.add(new FakeLeafSchemaNode(rpc.getQName())));
131 final ContainerSchemaNode fakeCont = new FakeContainerSchemaNode(fakeRpcSchema);
132 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder =
133 Builders.containerBuilder(fakeCont);
135 for (final LeafSchemaNode leaf : fakeRpcSchema) {
136 containerBuilder.withChild(Builders.leafBuilder(leaf).withValue(Empty.getInstance()).build());
139 final Collection<Module> fakeModules = new ArrayList<>(neededModules.size() + 1);
140 neededModules.forEach(imp -> fakeModules.add(new FakeImportedModule(imp)));
141 fakeModules.add(new FakeRestconfModule(neededModules, fakeCont));
143 final SchemaContext fakeSchemaCtx = SimpleSchemaContext.forModules(ImmutableSet.copyOf(fakeModules));
144 final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext =
145 new InstanceIdentifierContext<>(null, fakeCont, mountPoint, fakeSchemaCtx);
146 return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());