Decouple config and netconf subsystems.
[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.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.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
21 import org.opendaylight.controller.netconf.notifications.NetconfNotificationRegistry;
22 import org.opendaylight.controller.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 = InstanceIdentifier.builder(Netconf.class).build();
40
41     private final NetconfNotificationRegistry notificationRegistry;
42
43     public Get(final String netconfSessionIdForReporting, final NetconfNotificationRegistry notificationRegistry) {
44         super(netconfSessionIdForReporting);
45         Preconditions.checkNotNull(notificationRegistry);
46         this.notificationRegistry = notificationRegistry;
47     }
48
49     @Override
50     protected String getOperationName() {
51         return GET;
52     }
53
54     @Override
55     public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
56         final Document partialResponse = subsequentOperation.execute(requestMessage);
57         final Streams availableStreams = notificationRegistry.getNotificationPublishers();
58         if(availableStreams.getStream().isEmpty() == false) {
59             serializeStreamsSubtree(partialResponse, availableStreams);
60         }
61         return partialResponse;
62     }
63
64     static void serializeStreamsSubtree(final Document partialResponse, final Streams availableStreams) throws DocumentedException {
65         final Netconf netconfSubtree = new NetconfBuilder().setStreams(availableStreams).build();
66         final NormalizedNode<?, ?> normalized = toNormalized(netconfSubtree);
67
68         final DOMResult result = new DOMResult(getPlaceholder(partialResponse));
69
70         try {
71             NotificationsTransformUtil.writeNormalizedNode(normalized, result, SchemaPath.ROOT);
72         } catch (final XMLStreamException | IOException e) {
73             throw new IllegalStateException("Unable to serialize " + netconfSubtree, e);
74         }
75     }
76
77     private static Element getPlaceholder(final Document innerResult)
78             throws DocumentedException {
79         final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
80                 innerResult.getDocumentElement(), XmlMappingConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
81         return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
82     }
83
84     private static NormalizedNode<?, ?> toNormalized(final Netconf netconfSubtree) {
85         return NotificationsTransformUtil.CODEC_REGISTRY.toNormalizedNode(NETCONF_SUBTREE_INSTANCE_IDENTIFIER, netconfSubtree).getValue();
86     }
87
88     @Override
89     protected Element handle(final Document document, final XmlElement message, final NetconfOperationChainedExecution subsequentOperation)
90             throws DocumentedException {
91         throw new UnsupportedOperationException("Never gets called");
92     }
93
94     @Override
95     protected HandlingPriority getHandlingPriority() {
96         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(2);
97     }
98
99     @Override
100     public void close() throws Exception {
101
102     }
103 }