Code cleanup: unify writeNormalizedNode functionality
[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.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;
32
33 /**
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.
36  */
37 public class Get extends AbstractNetconfOperation implements AutoCloseable {
38
39     private static final String GET = "get";
40     private static final InstanceIdentifier<Netconf> NETCONF_SUBTREE_INSTANCE_IDENTIFIER = 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     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
57         final Document partialResponse = subsequentOperation.execute(requestMessage);
58         final Streams availableStreams = notificationRegistry.getNotificationPublishers();
59         if(availableStreams.getStream().isEmpty() == false) {
60             serializeStreamsSubtree(partialResponse, availableStreams);
61         }
62         return partialResponse;
63     }
64
65     static void serializeStreamsSubtree(final Document partialResponse, final Streams availableStreams) throws DocumentedException {
66         final Netconf netconfSubtree = new NetconfBuilder().setStreams(availableStreams).build();
67         final NormalizedNode<?, ?> normalized = toNormalized(netconfSubtree);
68
69         final DOMResult result = new DOMResult(getPlaceholder(partialResponse));
70
71         try {
72             NetconfUtil.writeNormalizedNode(normalized, result, SchemaPath.ROOT, NotificationsTransformUtil.NOTIFICATIONS_SCHEMA_CTX);
73         } catch (final XMLStreamException | IOException e) {
74             throw new IllegalStateException("Unable to serialize " + netconfSubtree, e);
75         }
76     }
77
78     private static Element getPlaceholder(final Document innerResult)
79             throws DocumentedException {
80         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
81                 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
82         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
83     }
84
85     private static NormalizedNode<?, ?> toNormalized(final Netconf netconfSubtree) {
86         return NotificationsTransformUtil.CODEC_REGISTRY.toNormalizedNode(NETCONF_SUBTREE_INSTANCE_IDENTIFIER, netconfSubtree).getValue();
87     }
88
89     @Override
90     protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
91             throws DocumentedException {
92         throw new UnsupportedOperationException("Never gets called");
93     }
94
95     @Override
96     protected HandlingPriority getHandlingPriority() {
97         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(2);
98     }
99
100     @Override
101     public void close() throws Exception {
102
103     }
104 }