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