dfa708274b17faa6431ad0a6bb9c889fb49b6130
[netconf.git] / restconf / restconf-nb-rfc8040 / 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 java.util.Optional;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.core.UriInfo;
15 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
16 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.restconf.common.OperationsContent;
19 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.util.OperationsResourceUtils;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
23 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
24 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfOperationsService;
25 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
26 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Implementation of {@link RestconfOperationsService}.
36  */
37 @Path("/")
38 public class RestconfOperationsServiceImpl implements RestconfOperationsService {
39     private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
40
41     private final SchemaContextHandler schemaContextHandler;
42     private final DOMMountPointService mountPointService;
43
44     /**
45      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}.
46      *
47      * @param schemaContextHandler handling schema context
48      * @param mountPointService handling dom mount point service
49      */
50     public RestconfOperationsServiceImpl(final SchemaContextHandler schemaContextHandler,
51             final DOMMountPointService mountPointService) {
52         this.schemaContextHandler = requireNonNull(schemaContextHandler);
53         this.mountPointService = requireNonNull(mountPointService);
54     }
55
56     @Override
57     public String getOperationsJSON() {
58         return OperationsContent.JSON.bodyFor(schemaContextHandler.get());
59     }
60
61     @Override
62     public String getOperationsXML() {
63         return OperationsContent.XML.bodyFor(schemaContextHandler.get());
64     }
65
66     @Override
67     public NormalizedNodePayload getOperations(final String identifier, final UriInfo uriInfo) {
68         if (!identifier.contains(RestconfConstants.MOUNT)) {
69             final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to "
70                     + " end with " + RestconfConstants.MOUNT;
71             LOG.debug("{} for {}", errMsg, identifier);
72             throw new RestconfDocumentedException(errMsg + RestconfConstants.MOUNT, ErrorType.PROTOCOL,
73                     ErrorTag.INVALID_VALUE);
74         }
75
76         final InstanceIdentifierContext mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
77             schemaContextHandler.get(), Optional.of(mountPointService));
78         final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
79         final var entry = OperationsResourceUtils.contextForModelContext(modelContext(mountPoint), mountPoint);
80         return NormalizedNodePayload.of(entry.getKey(), entry.getValue());
81     }
82
83     private static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
84         return mountPoint.getService(DOMSchemaService.class)
85             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
86             .orElse(null);
87     }
88 }