2 * Copyright (c) 2016 Cisco Systems, Inc. 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.restful.services.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import javax.ws.rs.core.Response;
13 import javax.ws.rs.core.UriInfo;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
18 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
19 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
20 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.references.SchemaContextRef;
23 import org.opendaylight.restconf.handlers.SchemaContextHandler;
24 import org.opendaylight.restconf.handlers.TransactionChainHandler;
25 import org.opendaylight.restconf.restful.services.api.RestconfDataService;
26 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
27 import org.opendaylight.restconf.restful.utils.PostDataTransactionUtil;
28 import org.opendaylight.restconf.restful.utils.PutDataTransactionUtil;
29 import org.opendaylight.restconf.restful.utils.ReadDataTransactionUtil;
30 import org.opendaylight.restconf.restful.utils.RestconfDataServiceConstant;
31 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Implementation of {@link RestconfDataService}
40 public class RestconfDataServiceImpl implements RestconfDataService {
42 private final static Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
44 private SchemaContextHandler schemaContextHandler;
45 private TransactionChainHandler transactionChainHandler;
48 public NormalizedNodeContext readData(final String identifier, final UriInfo uriInfo) {
49 Preconditions.checkNotNull(identifier);
50 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
52 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, schemaContextRef.get());
53 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
54 final String value = uriInfo.getQueryParameters().getFirst(RestconfDataServiceConstant.CONTENT);
56 DOMDataReadWriteTransaction transaction = null;
57 if (mountPoint == null) {
58 transaction = this.transactionChainHandler.get().newReadWriteTransaction();
60 transaction = transactionOfMountPoint(mountPoint);
62 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
64 final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(value, transactionNode);
66 return new NormalizedNodeContext(instanceIdentifier, node);
70 public Response putData(final String identifier, final NormalizedNodeContext payload) {
71 Preconditions.checkNotNull(identifier);
72 Preconditions.checkNotNull(payload);
74 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
75 .getInstanceIdentifierContext();
77 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
78 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
79 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
81 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
82 DOMDataReadWriteTransaction transaction = null;
83 SchemaContextRef ref = null;
84 if (mountPoint == null) {
85 transaction = this.transactionChainHandler.get().newReadWriteTransaction();
86 ref = new SchemaContextRef(this.schemaContextHandler.get());
88 transaction = transactionOfMountPoint(mountPoint);
89 ref = new SchemaContextRef(mountPoint.getSchemaContext());
92 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
93 payload.getInstanceIdentifierContext(), mountPoint, transaction);
94 return PutDataTransactionUtil.putData(payload, ref, transactionNode);
98 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
99 return postData(payload, uriInfo);
103 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
104 Preconditions.checkNotNull(payload);
106 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
107 DOMDataReadWriteTransaction transaction = null;
108 SchemaContextRef ref = null;
109 if (mountPoint == null) {
110 transaction = this.transactionChainHandler.get().newReadWriteTransaction();
111 ref = new SchemaContextRef(this.schemaContextHandler.get());
113 transaction = transactionOfMountPoint(mountPoint);
114 ref = new SchemaContextRef(mountPoint.getSchemaContext());
116 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
117 payload.getInstanceIdentifierContext(), mountPoint, transaction);
118 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref);
122 public Response deleteData(final String identifier) {
123 throw new UnsupportedOperationException("Not yet implemented.");
127 public PATCHStatusContext patchData(final String identifier, final PATCHContext context, final UriInfo uriInfo) {
128 throw new UnsupportedOperationException("Not yet implemented.");
132 public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
133 throw new UnsupportedOperationException("Not yet implemented.");
137 * Prepare transaction to read data of mount point, if these data are
141 * @param transactionNode
142 * - {@link TransactionVarsWrapper} - wrapper for variables
143 * @return {@link NormalizedNode}
145 private static DOMDataReadWriteTransaction transactionOfMountPoint(final DOMMountPoint mountPoint) {
146 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
147 if (domDataBrokerService.isPresent()) {
148 return domDataBrokerService.get().newReadWriteTransaction();
150 final String errMsg = "DOM data broker service isn't available for mount point "
151 + mountPoint.getIdentifier();
153 throw new RestconfDocumentedException(errMsg);