2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
14 import java.net.URISyntaxException;
15 import java.util.Collections;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
22 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
23 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
25 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
26 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
27 import org.opendaylight.controller.netconf.util.xml.XmlElement;
28 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
34 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
35 import org.opendaylight.yangtools.yang.data.operations.DataModificationException;
36 import org.opendaylight.yangtools.yang.data.operations.DataModificationException.DataExistsException;
37 import org.opendaylight.yangtools.yang.data.operations.DataModificationException.DataMissingException;
38 import org.opendaylight.yangtools.yang.data.operations.DataOperations;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
48 public class EditConfig extends AbstractLastNetconfOperation {
50 private static final Logger LOG = LoggerFactory.getLogger(EditConfig.class);
52 private static final String OPERATION_NAME = "edit-config";
53 private static final String CONFIG_KEY = "config";
55 private final CurrentSchemaContext schemaContext;
56 private final TransactionProvider transactionProvider;
58 public EditConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
59 super(netconfSessionIdForReporting);
60 this.schemaContext = schemaContext;
61 this.transactionProvider = transactionProvider;
65 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
66 final XmlElement configElement = getConfigElement(operationElement);
68 for (XmlElement element : configElement.getChildElements()) {
69 final String ns = element.getNamespace();
70 final DataSchemaNode schemaNode = getSchemaNodeFromNamespace(ns, element).get();
71 YangInstanceIdentifier ident = YangInstanceIdentifier.of(schemaNode.getQName());
73 final NormalizedNode storedNode = readStoredNode(LogicalDatastoreType.CONFIGURATION, ident);
75 final Optional<NormalizedNode<?,?>> newNode = modifyNode(schemaNode, element, storedNode);
76 final DOMDataReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
77 if (newNode.isPresent()) {
78 rwTx.put(LogicalDatastoreType.CONFIGURATION, ident, newNode.get());
80 rwTx.delete(LogicalDatastoreType.CONFIGURATION, ident);
82 } catch (final DataModificationException e) {
83 if (e instanceof DataExistsException) {
84 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.protocol, ErrorTag.data_exists, ErrorSeverity.error);
85 } else if (e instanceof DataMissingException) {
86 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.protocol, ErrorTag.data_missing, ErrorSeverity.error);
88 //should never happen, since in edit-config only the 2 previous cases can happen
89 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.protocol, ErrorTag.operation_failed, ErrorSeverity.error);
94 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
97 private NormalizedNode readStoredNode(final LogicalDatastoreType logicalDatastoreType, final YangInstanceIdentifier path) throws NetconfDocumentedException{
98 final DOMDataReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
99 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture = rwTx.read(logicalDatastoreType, path);
101 if (readFuture.checkedGet().isPresent()) {
102 final NormalizedNode node = readFuture.checkedGet().get();
105 LOG.warn("Unable to read node : {} from {} datastore", path, logicalDatastoreType);
107 } catch (final ReadFailedException e) {
108 //only log this since DataOperations.modify will handle throwing an exception or writing the node.
109 LOG.warn("Unable to read stored data: {}", path, e);
112 //we can return null here since DataOperations.modify handles null as input
116 private Optional<DataSchemaNode> getSchemaNodeFromNamespace(final String namespace, final XmlElement element){
117 Optional<DataSchemaNode> dataSchemaNode = Optional.absent();
119 //returns module with newest revision since findModuleByNamespace returns a set of modules and we only need the newest one
120 final Module module = schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(new URI(namespace), null);
121 dataSchemaNode = Optional.of(module.getDataChildByName(element.getName()));
122 } catch (URISyntaxException e) {
123 LOG.debug("Unable to create URI for namespace : {}", namespace);
126 return dataSchemaNode;
129 private Optional<NormalizedNode<?, ?>> modifyNode(final DataSchemaNode schemaNode, final XmlElement element, final NormalizedNode storedNode) throws DataModificationException{
130 if (schemaNode instanceof ContainerSchemaNode) {
131 final ContainerNode modifiedNode =
132 DomToNormalizedNodeParserFactory
133 .getInstance(DomUtils.defaultValueCodecProvider())
134 .getContainerNodeParser()
135 .parse(Collections.singletonList(element.getDomElement()), (ContainerSchemaNode) schemaNode);
137 final Optional<ContainerNode> oNode = DataOperations.modify((ContainerSchemaNode) schemaNode, (ContainerNode) storedNode, modifiedNode);
138 if (!oNode.isPresent()) {
139 return Optional.absent();
142 final NormalizedNode<?,?> node = oNode.get();
143 return Optional.<NormalizedNode<?,?>>of(node);
144 } else if (schemaNode instanceof ListSchemaNode) {
145 final MapNode modifiedNode =
146 DomToNormalizedNodeParserFactory
147 .getInstance(DomUtils.defaultValueCodecProvider())
149 .parse(Collections.singletonList(element.getDomElement()), (ListSchemaNode) schemaNode);
151 final Optional<MapNode> oNode = DataOperations.modify((ListSchemaNode) schemaNode, (MapNode) storedNode, modifiedNode);
152 if (!oNode.isPresent()) {
153 return Optional.absent();
156 final NormalizedNode<?, ?> node = oNode.get();
157 return Optional.<NormalizedNode<?,?>>of(node);
159 //this should never happen since edit-config on any other node type should not be possible nor makes sense
160 LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
161 return Optional.absent();
166 private XmlElement getConfigElement(final XmlElement operationElement) throws NetconfDocumentedException{
167 final Optional<XmlElement> configChildNode = operationElement.getOnlyChildElementOptionally(CONFIG_KEY);
168 if (!configChildNode.isPresent()) {
169 throw new NetconfDocumentedException("Can't get child element with name: " + CONFIG_KEY,
170 ErrorType.application,
171 ErrorTag.unknown_element,
172 ErrorSeverity.error);
175 return configChildNode.get();
179 protected String getOperationName() {
180 return OPERATION_NAME;