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