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;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
16 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
17 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
18 import org.opendaylight.yangtools.yang.common.Empty;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.util.SimpleSchemaContext;
30 public final class OperationsResourceUtils {
31 private OperationsResourceUtils() {
35 public static @NonNull NormalizedNodeContext contextForModelContext(final @NonNull SchemaContext context,
36 final @Nullable DOMMountPoint mountPoint) {
37 // Determine which modules we need and construct leaf schemas to correspond to all RPC definitions
38 final Collection<Module> modules = new ArrayList<>();
39 final ArrayList<OperationsLeafSchemaNode> rpcLeafSchemas = new ArrayList<>();
40 for (final Module m : context.getModules()) {
41 final Collection<? extends RpcDefinition> rpcs = m.getRpcs();
42 if (!rpcs.isEmpty()) {
43 modules.add(new OperationsImportedModule(m));
44 rpcLeafSchemas.ensureCapacity(rpcLeafSchemas.size() + rpcs.size());
45 for (RpcDefinition rpc : rpcs) {
46 rpcLeafSchemas.add(new OperationsLeafSchemaNode(rpc));
51 // Now generate a module for RESTCONF so that operations contain what they need
52 final OperationsContainerSchemaNode operatationsSchema = new OperationsContainerSchemaNode(rpcLeafSchemas);
53 modules.add(new OperationsRestconfModule(operatationsSchema));
55 // Now build the operations container and combine it with the context
56 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> operationsBuilder =
57 Builders.containerBuilder(operatationsSchema);
58 for (final OperationsLeafSchemaNode leaf : rpcLeafSchemas) {
59 operationsBuilder.withChild(ImmutableNodes.leafNode(leaf.getQName(), Empty.getInstance()));
62 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, operatationsSchema, mountPoint,
63 SimpleSchemaContext.forModules(ImmutableSet.copyOf(modules))), operationsBuilder.build());