2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.notifications.impl.ops;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.transform.dom.DOMResult;
15 import org.opendaylight.controller.config.util.xml.DocumentedException;
16 import org.opendaylight.controller.config.util.xml.XmlElement;
17 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.mapping.api.HandlingPriority;
20 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
21 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
22 import org.opendaylight.netconf.util.NetconfUtil;
23 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.NetconfBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
34 * Serialize the subtree for netconf notifications into the response of get rpc.
35 * This operation just adds its subtree into the common response of get rpc.
37 public class Get extends AbstractNetconfOperation implements AutoCloseable {
39 private static final String GET = "get";
40 private static final InstanceIdentifier<Netconf> NETCONF_SUBTREE_INSTANCE_IDENTIFIER =
41 InstanceIdentifier.builder(Netconf.class).build();
43 private final NetconfNotificationRegistry notificationRegistry;
45 public Get(final String netconfSessionIdForReporting, final NetconfNotificationRegistry notificationRegistry) {
46 super(netconfSessionIdForReporting);
47 Preconditions.checkNotNull(notificationRegistry);
48 this.notificationRegistry = notificationRegistry;
52 protected String getOperationName() {
57 protected Element handle(final Document document, final XmlElement message,
58 final NetconfOperationChainedExecution subsequentOperation)
59 throws DocumentedException {
60 throw new UnsupportedOperationException("Never gets called");
64 public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
65 throws DocumentedException {
66 final Document partialResponse = subsequentOperation.execute(requestMessage);
67 final Streams availableStreams = notificationRegistry.getNotificationPublishers();
68 if (!availableStreams.getStream().isEmpty()) {
69 serializeStreamsSubtree(partialResponse, availableStreams);
71 return partialResponse;
74 static void serializeStreamsSubtree(final Document partialResponse, final Streams availableStreams)
75 throws DocumentedException {
76 final Netconf netconfSubtree = new NetconfBuilder().setStreams(availableStreams).build();
77 final NormalizedNode<?, ?> normalized = toNormalized(netconfSubtree);
79 final DOMResult result = new DOMResult(getPlaceholder(partialResponse));
82 NetconfUtil.writeNormalizedNode(normalized, result, SchemaPath.ROOT,
83 NotificationsTransformUtil.NOTIFICATIONS_SCHEMA_CTX);
84 } catch (final XMLStreamException | IOException e) {
85 throw new IllegalStateException("Unable to serialize " + netconfSubtree, e);
89 private static Element getPlaceholder(final Document innerResult)
90 throws DocumentedException {
91 final XmlElement rootElement = XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
92 XmlMappingConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
93 return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
96 private static NormalizedNode<?, ?> toNormalized(final Netconf netconfSubtree) {
97 return NotificationsTransformUtil.CODEC_REGISTRY
98 .toNormalizedNode(NETCONF_SUBTREE_INSTANCE_IDENTIFIER, netconfSubtree).getValue();
102 protected HandlingPriority getHandlingPriority() {
103 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(2);
107 public void close() throws Exception {