e55f852a89c76cc50870c1920d2181d0c2f04221
[netconf.git] / netconf / netconf-notifications-impl / src / main / java / org / opendaylight / netconf / notifications / impl / ops / Get.java
1 /*
2  * Copyright (c) 2015 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.netconf.notifications.impl.ops;
10
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.netconf.api.DocumentedException;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.mapping.api.HandlingPriority;
19 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
20 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
21 import org.opendaylight.netconf.util.NetconfUtil;
22 import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.NetconfBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31
32 /**
33  * Serialize the subtree for netconf notifications into the response of get rpc.
34  * This operation just adds its subtree into the common response of get rpc.
35  */
36 public class Get extends AbstractNetconfOperation implements AutoCloseable {
37
38     private static final String GET = "get";
39     private static final InstanceIdentifier<Netconf> NETCONF_SUBTREE_INSTANCE_IDENTIFIER =
40             InstanceIdentifier.builder(Netconf.class).build();
41
42     private final NetconfNotificationRegistry notificationRegistry;
43
44     public Get(final String netconfSessionIdForReporting, final NetconfNotificationRegistry notificationRegistry) {
45         super(netconfSessionIdForReporting);
46         Preconditions.checkNotNull(notificationRegistry);
47         this.notificationRegistry = notificationRegistry;
48     }
49
50     @Override
51     protected String getOperationName() {
52         return GET;
53     }
54
55     @Override
56     protected Element handle(final Document document, final XmlElement message,
57                              final NetconfOperationChainedExecution subsequentOperation) {
58         throw new UnsupportedOperationException("Never gets called");
59     }
60
61     @Override
62     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
63             throws DocumentedException {
64         final Document partialResponse = subsequentOperation.execute(requestMessage);
65         final Streams availableStreams = notificationRegistry.getNotificationPublishers();
66         if (!availableStreams.getStream().isEmpty()) {
67             serializeStreamsSubtree(partialResponse, availableStreams);
68         }
69         return partialResponse;
70     }
71
72     static void serializeStreamsSubtree(final Document partialResponse, final Streams availableStreams)
73             throws DocumentedException {
74         final Netconf netconfSubtree = new NetconfBuilder().setStreams(availableStreams).build();
75         final NormalizedNode<?, ?> normalized = toNormalized(netconfSubtree);
76
77         final DOMResult result = new DOMResult(getPlaceholder(partialResponse));
78
79         try {
80             NetconfUtil.writeNormalizedNode(normalized, result, SchemaPath.ROOT,
81                     NotificationsTransformUtil.NOTIFICATIONS_SCHEMA_CTX);
82         } catch (final XMLStreamException | IOException e) {
83             throw new IllegalStateException("Unable to serialize " + netconfSubtree, e);
84         }
85     }
86
87     private static Element getPlaceholder(final Document innerResult)
88             throws DocumentedException {
89         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
90                 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
91         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
92     }
93
94     private static NormalizedNode<?, ?> toNormalized(final Netconf netconfSubtree) {
95         return NotificationsTransformUtil.CODEC_REGISTRY
96                 .toNormalizedNode(NETCONF_SUBTREE_INSTANCE_IDENTIFIER, netconfSubtree).getValue();
97     }
98
99     @Override
100     protected HandlingPriority getHandlingPriority() {
101         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(2);
102     }
103
104     @Override
105     public void close() {
106
107     }
108 }