Merge "Bug 615: Removed xtend from Topology Manager"
[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 java.io.InputStream;
12 import java.util.Map;
13
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
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.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Maps;
31
32 public class DefaultCommit extends AbstractNetconfOperation {
33
34     private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
35
36     private static final String NOTIFY_ATTR = "notify";
37
38     private final DefaultCommitNotificationProducer notificationProducer;
39     private final CapabilityProvider cap;
40     private final NetconfOperationRouter operationRouter;
41
42     public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
43                          String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
44         super(netconfSessionIdForReporting);
45         this.notificationProducer = notifier;
46         this.cap = cap;
47         this.operationRouter = netconfOperationRouter;
48         this.getConfigMessage = loadGetConfigMessage();
49     }
50
51     private final Document getConfigMessage;
52     public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
53
54     private static Document loadGetConfigMessage() {
55         try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
56             return XmlUtil.readXmlToDocument(asStream);
57         } catch (Exception e) {
58             throw new IllegalStateException("Unable to load getConfig message for notifications from "
59                     + GET_CONFIG_CANDIDATE_XML_LOCATION);
60         }
61     }
62
63     @Override
64     protected String getOperationName() {
65         return XmlNetconfConstants.COMMIT;
66     }
67
68     @Override
69     public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
70         Preconditions.checkArgument(subsequentOperation.isExecutionTermination() == false,
71                 "Subsequent netconf operation expected by %s", this);
72
73         if (isCommitWithoutNotification(requestMessage)) {
74             logger.debug("Skipping commit notification");
75         } else {
76             // Send commit notification if commit was not issued by persister
77             requestMessage = removePersisterAttributes(requestMessage);
78             Element cfgSnapshot = getConfigSnapshot(operationRouter);
79             logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
80             notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
81         }
82
83         return subsequentOperation.execute(requestMessage);
84     }
85
86     @Override
87     protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
88         throw new UnsupportedOperationException("Never gets called");
89     }
90
91     @Override
92     protected HandlingPriority getHandlingPriority() {
93         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
94     }
95
96     private Document removePersisterAttributes(Document message) {
97         final Element documentElement = message.getDocumentElement();
98         documentElement.removeAttribute(NOTIFY_ATTR);
99         return message;
100     }
101
102     private boolean isCommitWithoutNotification(Document message) {
103         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
104                 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
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             logger.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         try {
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         } catch (IllegalArgumentException e) {
128             final String msg = "Unexpected response from get-config operation";
129             logger.warn(msg, e);
130             Map<String, String> info = Maps.newHashMap();
131             info.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
132             throw new NetconfDocumentedException(msg, e, NetconfDocumentedException.ErrorType.application,
133                     NetconfDocumentedException.ErrorTag.operation_failed,
134                     NetconfDocumentedException.ErrorSeverity.error, info);
135         }
136
137         return dataElement.getDomElement();
138     }
139
140 }