*/
package org.opendaylight.restconf.restful.services.impl;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
import org.opendaylight.netconf.sal.restconf.impl.PATCHStatusContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.restconf.common.references.SchemaContextRef;
-import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
import org.opendaylight.restconf.handlers.SchemaContextHandler;
import org.opendaylight.restconf.handlers.TransactionChainHandler;
import org.opendaylight.restconf.restful.services.api.RestconfDataService;
import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
+import org.opendaylight.restconf.restful.utils.PutDataTransactionUtil;
import org.opendaylight.restconf.restful.utils.ReadDataTransactionUtil;
import org.opendaylight.restconf.restful.utils.RestconfDataServiceConstant;
import org.opendaylight.restconf.utils.parser.ParserIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Implementation of {@link RestconfDataService}
*/
public class RestconfDataServiceImpl implements RestconfDataService {
+ private final static Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
+
private SchemaContextHandler schemaContextHandler;
- private DOMMountPointServiceHandler domMountPointServiceHandler;
private TransactionChainHandler transactionChainHandler;
@Override
final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
final String value = uriInfo.getQueryParameters().getFirst(RestconfDataServiceConstant.CONTENT);
+ DOMDataReadWriteTransaction transaction = null;
+ if (mountPoint == null) {
+ transaction = this.transactionChainHandler.get().newReadWriteTransaction();
+ } else {
+ transaction = transactionOfMountPoint(mountPoint);
+ }
final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
- this.transactionChainHandler.get(), this.domMountPointServiceHandler.get(), schemaContextRef.get());
+ transaction);
final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(value, transactionNode);
return new NormalizedNodeContext(instanceIdentifier, node);
@Override
public Response putData(final String identifier, final NormalizedNodeContext payload) {
- throw new UnsupportedOperationException("Not yet implemented.");
+ Preconditions.checkNotNull(identifier);
+ Preconditions.checkNotNull(payload);
+
+ final InstanceIdentifierContext<? extends SchemaNode> iid = payload
+ .getInstanceIdentifierContext();
+
+ PutDataTransactionUtil.validInputData(iid.getSchemaNode(), payload);
+ PutDataTransactionUtil.validTopLevelNodeName(iid.getInstanceIdentifier(), payload);
+ PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
+
+ final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
+ DOMDataReadWriteTransaction transaction = null;
+ SchemaContextRef ref = null;
+ if (mountPoint == null) {
+ transaction = this.transactionChainHandler.get().newReadWriteTransaction();
+ ref = new SchemaContextRef(this.schemaContextHandler.get());
+ } else {
+ transaction = transactionOfMountPoint(mountPoint);
+ ref = new SchemaContextRef(mountPoint.getSchemaContext());
+ }
+
+ final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
+ payload.getInstanceIdentifierContext(), mountPoint, transaction);
+ return PutDataTransactionUtil.putData(payload, ref, transactionNode);
}
@Override
public PATCHStatusContext patchData(final PATCHContext context, final UriInfo uriInfo) {
throw new UnsupportedOperationException("Not yet implemented.");
}
+
+ /**
+ * Prepare transaction to read data of mount point, if these data are
+ * present.
+ * @param mountPoint
+ *
+ * @param transactionNode
+ * - {@link TransactionVarsWrapper} - wrapper for variables
+ * @return {@link NormalizedNode}
+ */
+ private static DOMDataReadWriteTransaction transactionOfMountPoint(final DOMMountPoint mountPoint) {
+ final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
+ if (domDataBrokerService.isPresent()) {
+ return domDataBrokerService.get().newReadWriteTransaction();
+ } else {
+ final String errMsg = "DOM data broker service isn't available for mount point "
+ + mountPoint.getIdentifier();
+ LOG.warn(errMsg);
+ throw new RestconfDocumentedException(errMsg);
+ }
+ }
}