Bug 6895 - Implement Query parameters - depth
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / services / impl / RestconfDataServiceImpl.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.restful.services.impl;
9
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;
46
47 /**
48  * Implementation of {@link RestconfDataService}
49  */
50 public class RestconfDataServiceImpl implements RestconfDataService {
51
52     private final static Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
53
54     private final SchemaContextHandler schemaContextHandler;
55     private final TransactionChainHandler transactionChainHandler;
56     private final DOMMountPointServiceHandler mountPointServiceHandler;
57
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;
64     }
65
66     @Override
67     public Response readData(final String identifier, final UriInfo uriInfo) {
68         Preconditions.checkNotNull(identifier);
69
70         final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(uriInfo);
71         final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
72
73         final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
74                 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
75         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
76
77         final DOMTransactionChain transactionChain;
78         if (mountPoint == null) {
79             transactionChain = this.transactionChainHandler.get();
80         } else {
81             transactionChain = transactionChainOfMountPoint(mountPoint);
82         }
83
84         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
85                 instanceIdentifier, mountPoint, transactionChain);
86         final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(parameters.getContent(), transactionNode);
87         if (node == null) {
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);
92         }
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() + '"';
97         final Response resp;
98
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()))
105                     .build();
106         } else {
107             resp = Response.status(200)
108                     .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
109                     .build();
110         }
111
112         return resp;
113     }
114
115     @Override
116     public Response putData(final String identifier, final NormalizedNodeContext payload) {
117         Preconditions.checkNotNull(payload);
118
119         final InstanceIdentifierContext<? extends SchemaNode> iid = payload
120                 .getInstanceIdentifierContext();
121
122         PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
123         PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
124         PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
125
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());
132         } else {
133             transactionChain = transactionChainOfMountPoint(mountPoint);
134             ref = new SchemaContextRef(mountPoint.getSchemaContext());
135         }
136
137         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
138                 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
139         return PutDataTransactionUtil.putData(payload, ref, transactionNode);
140     }
141
142     @Override
143     public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
144         return postData(payload, uriInfo);
145     }
146
147     @Override
148     public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
149         Preconditions.checkNotNull(payload);
150
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());
157         } else {
158             transactionChain = transactionChainOfMountPoint(mountPoint);
159             ref = new SchemaContextRef(mountPoint.getSchemaContext());
160         }
161         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
162                 payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
163         return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref);
164     }
165
166     @Override
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()));
171
172         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
173         final DOMTransactionChain transactionChain;
174         if (mountPoint == null) {
175             transactionChain = this.transactionChainHandler.get();
176         } else {
177             transactionChain = transactionChainOfMountPoint(mountPoint);
178         }
179
180         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
181                 transactionChain);
182         return DeleteDataTransactionUtil.deleteData(transactionNode);
183     }
184
185     @Override
186     public PATCHStatusContext patchData(final String identifier, final PATCHContext context, final UriInfo uriInfo) {
187         return patchData(context, uriInfo);
188     }
189
190     @Override
191     public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
192         Preconditions.checkNotNull(context);
193         final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
194
195         final DOMTransactionChain transactionChain;
196         final SchemaContextRef ref;
197         if (mountPoint == null) {
198             transactionChain = this.transactionChainHandler.get();
199             ref = new SchemaContextRef(this.schemaContextHandler.get());
200         } else {
201             transactionChain = transactionChainOfMountPoint(mountPoint);
202             ref = new SchemaContextRef(mountPoint.getSchemaContext());
203         }
204
205         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
206                 context.getInstanceIdentifierContext(), mountPoint, transactionChain);
207
208         return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
209     }
210
211     /**
212      * Prepare transaction chain to access data of mount point
213      * @param mountPoint
214      *            - mount point reference
215      * @return {@link DOMTransactionChain}
216      */
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);
221         } else {
222             final String errMsg = "DOM data broker service isn't available for mount point "
223                     + mountPoint.getIdentifier();
224             LOG.warn(errMsg);
225             throw new RestconfDocumentedException(errMsg);
226         }
227     }
228 }