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