Reduce exception guard
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfOperationsServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.core.UriInfo;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
23 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
28 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
29 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfOperationsService;
30 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
31 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
32 import org.opendaylight.yangtools.yang.common.Empty;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Implementation of {@link RestconfOperationsService}.
50  */
51 @Path("/")
52 public class RestconfOperationsServiceImpl implements RestconfOperationsService {
53     private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
54
55     private final SchemaContextHandler schemaContextHandler;
56     private final DOMMountPointService mountPointService;
57
58     /**
59      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}.
60      *
61      * @param schemaContextHandler handling schema context
62      * @param mountPointService handling dom mount point service
63      */
64     public RestconfOperationsServiceImpl(final SchemaContextHandler schemaContextHandler,
65             final DOMMountPointService mountPointService) {
66         this.schemaContextHandler = requireNonNull(schemaContextHandler);
67         this.mountPointService = requireNonNull(mountPointService);
68     }
69
70     @Override
71     public String getOperationsJSON() {
72         return OperationsContent.JSON.bodyFor(schemaContextHandler.get());
73     }
74
75     @Override
76     public String getOperationsXML() {
77         return OperationsContent.XML.bodyFor(schemaContextHandler.get());
78     }
79
80     @Override
81     public NormalizedNodePayload getOperations(final String identifier, final UriInfo uriInfo) {
82         if (!identifier.contains(RestconfConstants.MOUNT)) {
83             final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to "
84                     + " end with " + RestconfConstants.MOUNT;
85             LOG.debug("{} for {}", errMsg, identifier);
86             throw new RestconfDocumentedException(errMsg + RestconfConstants.MOUNT, ErrorType.PROTOCOL,
87                     ErrorTag.INVALID_VALUE);
88         }
89
90         final InstanceIdentifierContext mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
91             schemaContextHandler.get(), Optional.of(mountPointService));
92         final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
93         final var entry = contextForModelContext(modelContext(mountPoint), mountPoint);
94         return NormalizedNodePayload.of(entry.getKey(), entry.getValue());
95     }
96
97     private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
98         return mountPoint.getService(DOMSchemaService.class)
99             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
100             .orElse(null);
101     }
102
103     // FIXME: remove this method and everything it uses
104     @Deprecated(forRemoval = true, since = "4.0.0")
105     private static @NonNull Entry<InstanceIdentifierContext, ContainerNode> contextForModelContext(
106             final @NonNull EffectiveModelContext context, final @Nullable DOMMountPoint mountPoint) {
107         // Determine which modules we need and construct leaf schemas to correspond to all RPC definitions
108         final Collection<Module> modules = new ArrayList<>();
109         final ArrayList<OperationsLeafSchemaNode> rpcLeafSchemas = new ArrayList<>();
110         for (final Module m : context.getModules()) {
111             final Collection<? extends RpcDefinition> rpcs = m.getRpcs();
112             if (!rpcs.isEmpty()) {
113                 modules.add(new OperationsImportedModule(m));
114                 rpcLeafSchemas.ensureCapacity(rpcLeafSchemas.size() + rpcs.size());
115                 for (RpcDefinition rpc : rpcs) {
116                     rpcLeafSchemas.add(new OperationsLeafSchemaNode(rpc));
117                 }
118             }
119         }
120
121         // Now generate a module for RESTCONF so that operations contain what they need
122         final OperationsContainerSchemaNode operatationsSchema = new OperationsContainerSchemaNode(rpcLeafSchemas);
123         modules.add(new OperationsRestconfModule(operatationsSchema));
124
125         // Now build the operations container and combine it with the context
126         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> operationsBuilder = Builders.containerBuilder()
127                 .withNodeIdentifier(new NodeIdentifier(OperationsContainerSchemaNode.QNAME));
128         for (final OperationsLeafSchemaNode leaf : rpcLeafSchemas) {
129             operationsBuilder.withChild(ImmutableNodes.leafNode(leaf.getQName(), Empty.value()));
130         }
131
132         final var opContext = new OperationsEffectiveModuleContext(ImmutableSet.copyOf(modules));
133         final var stack = SchemaInferenceStack.of(opContext);
134         stack.enterSchemaTree(operatationsSchema.getQName());
135
136         return Map.entry(InstanceIdentifierContext.ofStack(stack, mountPoint), operationsBuilder.build());
137     }
138
139 }