CDS: Add stress test RPC to the cars model
[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     //TODO make commit notification optional
41     private final CommitNotifier notificationProducer;
42     private final NetconfMonitoringService cap;
43     private final NetconfOperationRouter operationRouter;
44
45     public DefaultCommit(final CommitNotifier notifier, final NetconfMonitoringService cap,
46                          final String netconfSessionIdForReporting, final NetconfOperationRouter netconfOperationRouter) {
47         super(netconfSessionIdForReporting);
48         this.notificationProducer = notifier;
49         this.cap = cap;
50         this.operationRouter = netconfOperationRouter;
51         this.getConfigMessage = loadGetConfigMessage();
52     }
53
54     private final Document getConfigMessage;
55     public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
56
57     private static Document loadGetConfigMessage() {
58         try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
59             return XmlUtil.readXmlToDocument(asStream);
60         } catch (Exception e) {
61             throw new IllegalStateException("Unable to load getConfig message for notifications from "
62                     + GET_CONFIG_CANDIDATE_XML_LOCATION);
63         }
64     }
65
66     @Override
67     protected String getOperationName() {
68         return XmlNetconfConstants.COMMIT;
69     }
70
71     @Override
72     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
73         Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
74                 "Subsequent netconf operation expected by %s", this);
75
76         if (isCommitWithoutNotification(requestMessage)) {
77             LOG.debug("Skipping commit notification");
78         } else {
79             // Send commit notification if commit was not issued by persister
80             removePersisterAttributes(requestMessage);
81             Element cfgSnapshot = getConfigSnapshot(operationRouter);
82             LOG.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
83             notificationProducer.sendCommitNotification("ok", cfgSnapshot, transformCapabilities(cap.getCapabilities()));
84         }
85
86         return subsequentOperation.execute(requestMessage);
87     }
88
89     // FIXME move somewhere to util since this is required also by negotiatiorFactory
90     public static Set<String> transformCapabilities(final Capabilities capabilities) {
91         return Sets.newHashSet(Collections2.transform(capabilities.getCapability(), new Function<Uri, String>() {
92             @Override
93             public String apply(final Uri uri) {
94                 return uri.getValue();
95             }
96         }));
97     }
98
99     @Override
100     protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
101         throw new UnsupportedOperationException("Never gets called");
102     }
103
104     @Override
105     protected HandlingPriority getHandlingPriority() {
106         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
107     }
108
109     private static void removePersisterAttributes(final Document message) {
110         message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
111     }
112
113     private static boolean isCommitWithoutNotification(final Document message) {
114         XmlElement xmlElement = null;
115         try {
116             xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
117                     XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
118         } catch (NetconfDocumentedException e) {
119             LOG.trace("Commit operation is not valid due to ",e);
120             return false;
121         }
122
123         String attr = xmlElement.getAttribute(NOTIFY_ATTR);
124
125         if (attr == null || attr.equals("")){
126             return false;
127         } else if (attr.equals(Boolean.toString(false))) {
128             LOG.debug("Commit operation received with notify=false attribute {}", message);
129             return true;
130         } else {
131             return false;
132         }
133     }
134
135     private Element getConfigSnapshot(final NetconfOperationRouter opRouter) throws NetconfDocumentedException {
136         final Document responseDocument = opRouter.onNetconfMessage(
137                 getConfigMessage, null);
138
139         XmlElement dataElement;
140         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
141                 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
142         dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
143         return dataElement.getDomElement();
144     }
145
146 }