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