Merge "Resolve Bug:853 - remove groovy from config code generator."
[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(),
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             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 void removePersisterAttributes(Document message) {
94         message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
95     }
96
97     private boolean isCommitWithoutNotification(Document message) {
98         XmlElement xmlElement = null;
99         try {
100             xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
101                     XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
102         } catch (NetconfDocumentedException e) {
103             logger.trace("Commit operation is not valid due to  {}",e);
104             return false;
105         }
106
107         String attr = xmlElement.getAttribute(NOTIFY_ATTR);
108
109         if (attr == null || attr.equals("")){
110             return false;
111         } else if (attr.equals(Boolean.toString(false))) {
112             logger.debug("Commit operation received with notify=false attribute {}", message);
113             return true;
114         } else {
115             return false;
116         }
117     }
118
119     private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
120         final Document responseDocument = opRouter.onNetconfMessage(
121                 getConfigMessage, null);
122
123         XmlElement dataElement;
124         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
125                 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
126         dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
127         return dataElement.getDomElement();
128     }
129
130 }