- Added exi capability utilities, handlers and necessary modifications
[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.api.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.NetconfOperationFilter;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
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.collect.Maps;
30
31 public class DefaultCommit implements NetconfOperationFilter {
32
33     private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
34
35     private static final String NOTIFY_ATTR = "notify";
36
37     private final DefaultCommitNotificationProducer notificationProducer;
38     private final CapabilityProvider cap;
39     private final String netconfSessionIdForReporting;
40
41     public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
42             String netconfSessionIdForReporting) {
43         this.notificationProducer = notifier;
44         this.cap = cap;
45         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
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     public Document doFilter(Document message, NetconfOperationRouter operationRouter,
63             NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
64         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
65         if (canHandle(operationNameAndNamespace)) {
66             if (isCommitWithoutNotification(message)) {
67                 message = removePersisterAttributes(message);
68                 logger.debug("Skipping commit notification");
69                 // fall back to filter chain
70             } else {
71                 Document innerResult = filterChain.execute(message, operationRouter);
72                 Element cfgSnapshot = getConfigSnapshot(operationRouter);
73                 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
74                 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
75                 return innerResult;
76             }
77         }
78         return filterChain.execute(message, operationRouter);
79     }
80
81     @Override
82     public int getSortingOrder() {
83         return 0;
84     }
85
86     @Override
87     public int compareTo(NetconfOperationFilter o) {
88         return Integer.compare(getSortingOrder(), o.getSortingOrder());
89     }
90
91     private boolean canHandle(OperationNameAndNamespace operationNameAndNamespace) {
92         if (operationNameAndNamespace.getOperationName().equals("commit") == false)
93             return false;
94         return operationNameAndNamespace.getNamespace().equals(
95                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
96     }
97
98     private Document removePersisterAttributes(Document message) {
99         final Element documentElement = message.getDocumentElement();
100         documentElement.removeAttribute(NOTIFY_ATTR);
101         return message;
102     }
103
104     private boolean isCommitWithoutNotification(Document message) {
105         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
106                 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
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         try {
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         } catch (IllegalArgumentException e) {
130             final String msg = "Unexpected response from get-config operation";
131             logger.warn(msg, e);
132             Map<String, String> info = Maps.newHashMap();
133             info.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
134             throw new NetconfDocumentedException(msg, e, NetconfDocumentedException.ErrorType.application,
135                     NetconfDocumentedException.ErrorTag.operation_failed,
136                     NetconfDocumentedException.ErrorSeverity.error, info);
137         }
138
139         return dataElement.getDomElement();
140     }
141
142     @Override
143     public String toString() {
144         return "DefaultCommit{" + netconfSessionIdForReporting + '}';
145     }
146 }