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 java.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.TimeZone;
15 import javax.annotation.Nonnull;
16 import javax.ws.rs.core.Response;
17 import javax.ws.rs.core.UriInfo;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
22 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
24 import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
27 import org.opendaylight.netconf.sal.restconf.impl.WriterParameters;
28 import org.opendaylight.restconf.RestConnectorProvider;
29 import org.opendaylight.restconf.common.references.SchemaContextRef;
30 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
31 import org.opendaylight.restconf.handlers.SchemaContextHandler;
32 import org.opendaylight.restconf.handlers.TransactionChainHandler;
33 import org.opendaylight.restconf.restful.services.api.RestconfDataService;
34 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
35 import org.opendaylight.restconf.restful.utils.DeleteDataTransactionUtil;
36 import org.opendaylight.restconf.restful.utils.PatchDataTransactionUtil;
37 import org.opendaylight.restconf.restful.utils.PostDataTransactionUtil;
38 import org.opendaylight.restconf.restful.utils.PutDataTransactionUtil;
39 import org.opendaylight.restconf.restful.utils.ReadDataTransactionUtil;
40 import org.opendaylight.restconf.restful.utils.RestconfDataServiceConstant;
41 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Implementation of {@link RestconfDataService}
50 public class RestconfDataServiceImpl implements RestconfDataService {
52 private final static Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
54 private final SchemaContextHandler schemaContextHandler;
55 private final TransactionChainHandler transactionChainHandler;
56 private final DOMMountPointServiceHandler mountPointServiceHandler;
58 public RestconfDataServiceImpl(final SchemaContextHandler schemaContextHandler,
59 final TransactionChainHandler transactionChainHandler,
60 final DOMMountPointServiceHandler mountPointServiceHandler) {
61 this.schemaContextHandler = schemaContextHandler;
62 this.transactionChainHandler = transactionChainHandler;
63 this.mountPointServiceHandler = mountPointServiceHandler;
67 public Response readData(final String identifier, final UriInfo uriInfo) {
68 Preconditions.checkNotNull(identifier);
70 final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(uriInfo);
71 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
73 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
74 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
75 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
77 final DOMTransactionChain transactionChain;
78 if (mountPoint == null) {
79 transactionChain = this.transactionChainHandler.get();
81 transactionChain = transactionChainOfMountPoint(mountPoint);
84 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
85 instanceIdentifier, mountPoint, transactionChain);
86 final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(parameters.getContent(), transactionNode);
88 throw new RestconfDocumentedException(
89 "Request could not be completed because the relevant data model content does not exist",
90 RestconfError.ErrorType.PROTOCOL,
91 RestconfError.ErrorTag.DATA_MISSING);
93 final SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
94 dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
95 final String etag = '"' + node.getNodeType().getModule().getFormattedRevision()
96 + node.getNodeType().getLocalName() + '"';
99 if ((parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL))
100 || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
101 resp = Response.status(200)
102 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
103 .header("ETag", etag)
104 .header("Last-Modified", dateFormatGmt.format(new Date()))
107 resp = Response.status(200)
108 .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
116 public Response putData(final String identifier, final NormalizedNodeContext payload) {
117 Preconditions.checkNotNull(payload);
119 final InstanceIdentifierContext<? extends SchemaNode> iid = payload
120 .getInstanceIdentifierContext();
122 PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
123 PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
124 PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
126 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
127 final DOMTransactionChain transactionChain;
128 final SchemaContextRef ref;
129 if (mountPoint == null) {
130 transactionChain = this.transactionChainHandler.get();
131 ref = new SchemaContextRef(this.schemaContextHandler.get());
133 transactionChain = transactionChainOfMountPoint(mountPoint);
134 ref = new SchemaContextRef(mountPoint.getSchemaContext());
137 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
138 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
139 return PutDataTransactionUtil.putData(payload, ref, transactionNode);
143 public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
144 return postData(payload, uriInfo);
148 public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
149 Preconditions.checkNotNull(payload);
151 final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
152 final DOMTransactionChain transactionChain;
153 final SchemaContextRef ref;
154 if (mountPoint == null) {
155 transactionChain = this.transactionChainHandler.get();
156 ref = new SchemaContextRef(this.schemaContextHandler.get());
158 transactionChain = transactionChainOfMountPoint(mountPoint);
159 ref = new SchemaContextRef(mountPoint.getSchemaContext());
161 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
162 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
163 return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref);
167 public Response deleteData(final String identifier) {
168 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
169 final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
170 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
172 final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
173 final DOMTransactionChain transactionChain;
174 if (mountPoint == null) {
175 transactionChain = this.transactionChainHandler.get();
177 transactionChain = transactionChainOfMountPoint(mountPoint);
180 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
182 return DeleteDataTransactionUtil.deleteData(transactionNode);
186 public PATCHStatusContext patchData(final String identifier, final PATCHContext context, final UriInfo uriInfo) {
187 return patchData(context, uriInfo);
191 public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
192 Preconditions.checkNotNull(context);
193 final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
195 final DOMTransactionChain transactionChain;
196 final SchemaContextRef ref;
197 if (mountPoint == null) {
198 transactionChain = this.transactionChainHandler.get();
199 ref = new SchemaContextRef(this.schemaContextHandler.get());
201 transactionChain = transactionChainOfMountPoint(mountPoint);
202 ref = new SchemaContextRef(mountPoint.getSchemaContext());
205 final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
206 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
208 return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
212 * Prepare transaction chain to access data of mount point
214 * - mount point reference
215 * @return {@link DOMTransactionChain}
217 private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
218 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
219 if (domDataBrokerService.isPresent()) {
220 return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.transactionListener);
222 final String errMsg = "DOM data broker service isn't available for mount point "
223 + mountPoint.getIdentifier();
225 throw new RestconfDocumentedException(errMsg);