2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.common.util;
10 import com.google.common.collect.ImmutableSet;
11 import java.util.ArrayList;
12 import java.util.Collection;
14 import java.util.Map.Entry;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
19 import org.opendaylight.yangtools.yang.common.Empty;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 // FIXME: remove this class
31 public final class OperationsResourceUtils {
32 private OperationsResourceUtils() {
36 public static @NonNull Entry<InstanceIdentifierContext, ContainerNode>
37 contextForModelContext(final @NonNull SchemaContext context, final @Nullable DOMMountPoint mountPoint) {
38 // Determine which modules we need and construct leaf schemas to correspond to all RPC definitions
39 final Collection<Module> modules = new ArrayList<>();
40 final ArrayList<OperationsLeafSchemaNode> rpcLeafSchemas = new ArrayList<>();
41 for (final Module m : context.getModules()) {
42 final Collection<? extends RpcDefinition> rpcs = m.getRpcs();
43 if (!rpcs.isEmpty()) {
44 modules.add(new OperationsImportedModule(m));
45 rpcLeafSchemas.ensureCapacity(rpcLeafSchemas.size() + rpcs.size());
46 for (RpcDefinition rpc : rpcs) {
47 rpcLeafSchemas.add(new OperationsLeafSchemaNode(rpc));
52 // Now generate a module for RESTCONF so that operations contain what they need
53 final OperationsContainerSchemaNode operatationsSchema = new OperationsContainerSchemaNode(rpcLeafSchemas);
54 modules.add(new OperationsRestconfModule(operatationsSchema));
56 // Now build the operations container and combine it with the context
57 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> operationsBuilder = Builders.containerBuilder()
58 .withNodeIdentifier(new NodeIdentifier(OperationsContainerSchemaNode.QNAME));
59 for (final OperationsLeafSchemaNode leaf : rpcLeafSchemas) {
60 operationsBuilder.withChild(ImmutableNodes.leafNode(leaf.getQName(), Empty.value()));
63 return Map.entry(InstanceIdentifierContext.ofDataSchemaNode(
64 new OperationsEffectiveModuleContext(ImmutableSet.copyOf(modules)), operatationsSchema, mountPoint),
65 operationsBuilder.build());