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