Bug 5528 - Impl Post data
[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 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;
36
37 /**
38  * Implementation of {@link RestconfDataService}
39  */
40 public class RestconfDataServiceImpl implements RestconfDataService {
41
42     private final static Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
43
44     private SchemaContextHandler schemaContextHandler;
45     private TransactionChainHandler transactionChainHandler;
46
47     @Override
48     public NormalizedNodeContext readData(final String identifier, final UriInfo uriInfo) {
49         Preconditions.checkNotNull(identifier);
50         final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
51
52         final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, schemaContextRef.get());
53         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
54         final String value = uriInfo.getQueryParameters().getFirst(RestconfDataServiceConstant.CONTENT);
55
56         DOMDataReadWriteTransaction transaction = null;
57         if (mountPoint == null) {
58             transaction = this.transactionChainHandler.get().newReadWriteTransaction();
59         } else {
60             transaction = transactionOfMountPoint(mountPoint);
61         }
62         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
63                 transaction);
64         final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(value, transactionNode);
65
66         return new NormalizedNodeContext(instanceIdentifier, node);
67     }
68
69     @Override
70     public Response putData(final String identifier, final NormalizedNodeContext payload) {
71         Preconditions.checkNotNull(identifier);
72         Preconditions.checkNotNull(payload);
73
74         final InstanceIdentifierContext<? extends SchemaNode> iid = payload
75                 .getInstanceIdentifierContext();
76
77         PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
78         PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
79         PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
80
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());
87         } else {
88             transaction = transactionOfMountPoint(mountPoint);
89             ref = new SchemaContextRef(mountPoint.getSchemaContext());
90         }
91
92         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
93                 payload.getInstanceIdentifierContext(), mountPoint, transaction);
94         return PutDataTransactionUtil.putData(payload, ref, transactionNode);
95     }
96
97     @Override
98     public Response postData(final String identifier, final NormalizedNodeContext payload, final UriInfo uriInfo) {
99         return postData(payload, uriInfo);
100     }
101
102     @Override
103     public Response postData(final NormalizedNodeContext payload, final UriInfo uriInfo) {
104         Preconditions.checkNotNull(payload);
105
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());
112         } else {
113             transaction = transactionOfMountPoint(mountPoint);
114             ref = new SchemaContextRef(mountPoint.getSchemaContext());
115         }
116         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
117                 payload.getInstanceIdentifierContext(), mountPoint, transaction);
118         return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref);
119     }
120
121     @Override
122     public Response deleteData(final String identifier) {
123         throw new UnsupportedOperationException("Not yet implemented.");
124     }
125
126     @Override
127     public PATCHStatusContext patchData(final String identifier, final PATCHContext context, final UriInfo uriInfo) {
128         throw new UnsupportedOperationException("Not yet implemented.");
129     }
130
131     @Override
132     public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
133         throw new UnsupportedOperationException("Not yet implemented.");
134     }
135
136     /**
137      * Prepare transaction to read data of mount point, if these data are
138      * present.
139      * @param mountPoint
140      *
141      * @param transactionNode
142      *            - {@link TransactionVarsWrapper} - wrapper for variables
143      * @return {@link NormalizedNode}
144      */
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();
149         } else {
150             final String errMsg = "DOM data broker service isn't available for mount point "
151                     + mountPoint.getIdentifier();
152             LOG.warn(errMsg);
153             throw new RestconfDocumentedException(errMsg);
154         }
155     }
156 }