Merge "Add message to update the schema context of the InMemoryDOMDataStore"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCommit.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.impl.mapping.operations;
10
11 import com.google.common.base.Preconditions;
12
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 import java.io.InputStream;
29
30 public class DefaultCommit extends AbstractNetconfOperation {
31
32     private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
33
34     private static final String NOTIFY_ATTR = "notify";
35
36     private final DefaultCommitNotificationProducer notificationProducer;
37     private final CapabilityProvider cap;
38     private final NetconfOperationRouter operationRouter;
39
40     public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
41                          String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
42         super(netconfSessionIdForReporting);
43         this.notificationProducer = notifier;
44         this.cap = cap;
45         this.operationRouter = netconfOperationRouter;
46         this.getConfigMessage = loadGetConfigMessage();
47     }
48
49     private final Document getConfigMessage;
50     public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
51
52     private static Document loadGetConfigMessage() {
53         try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
54             return XmlUtil.readXmlToDocument(asStream);
55         } catch (Exception e) {
56             throw new IllegalStateException("Unable to load getConfig message for notifications from "
57                     + GET_CONFIG_CANDIDATE_XML_LOCATION);
58         }
59     }
60
61     @Override
62     protected String getOperationName() {
63         return XmlNetconfConstants.COMMIT;
64     }
65
66     @Override
67     public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
68         Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
69                 "Subsequent netconf operation expected by %s", this);
70
71         if (isCommitWithoutNotification(requestMessage)) {
72             logger.debug("Skipping commit notification");
73         } else {
74             // Send commit notification if commit was not issued by persister
75             removePersisterAttributes(requestMessage);
76             Element cfgSnapshot = getConfigSnapshot(operationRouter);
77             logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
78             notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
79         }
80
81         return subsequentOperation.execute(requestMessage);
82     }
83
84     @Override
85     protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
86         throw new UnsupportedOperationException("Never gets called");
87     }
88
89     @Override
90     protected HandlingPriority getHandlingPriority() {
91         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
92     }
93
94     private void removePersisterAttributes(Document message) {
95         message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
96     }
97
98     private boolean isCommitWithoutNotification(Document message) {
99         XmlElement xmlElement = null;
100         try {
101             xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
102                     XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
103         } catch (NetconfDocumentedException e) {
104             logger.trace("Commit operation is not valid due to  {}",e);
105             return false;
106         }
107
108         String attr = xmlElement.getAttribute(NOTIFY_ATTR);
109
110         if (attr == null || attr.equals("")){
111             return false;
112         } else if (attr.equals(Boolean.toString(false))) {
113             logger.debug("Commit operation received with notify=false attribute {}", message);
114             return true;
115         } else {
116             return false;
117         }
118     }
119
120     private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
121         final Document responseDocument = opRouter.onNetconfMessage(
122                 getConfigMessage, null);
123
124         XmlElement dataElement;
125         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
126                 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
127         dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
128         return dataElement.getDomElement();
129     }
130
131 }