Simplify boolean expressions
[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 =
41             InstanceIdentifier.builder(Netconf.class).build();
42
43     private final NetconfNotificationRegistry notificationRegistry;
44
45     public Get(final String netconfSessionIdForReporting, final NetconfNotificationRegistry notificationRegistry) {
46         super(netconfSessionIdForReporting);
47         Preconditions.checkNotNull(notificationRegistry);
48         this.notificationRegistry = notificationRegistry;
49     }
50
51     @Override
52     protected String getOperationName() {
53         return GET;
54     }
55
56     @Override
57     protected Element handle(final Document document, final XmlElement message,
58                              final NetconfOperationChainedExecution subsequentOperation)
59             throws DocumentedException {
60         throw new UnsupportedOperationException("Never gets called");
61     }
62
63     @Override
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);
70         }
71         return partialResponse;
72     }
73
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);
78
79         final DOMResult result = new DOMResult(getPlaceholder(partialResponse));
80
81         try {
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);
86         }
87     }
88
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();
94     }
95
96     private static NormalizedNode<?, ?> toNormalized(final Netconf netconfSubtree) {
97         return NotificationsTransformUtil.CODEC_REGISTRY
98                 .toNormalizedNode(NETCONF_SUBTREE_INSTANCE_IDENTIFIER, netconfSubtree).getValue();
99     }
100
101     @Override
102     protected HandlingPriority getHandlingPriority() {
103         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(2);
104     }
105
106     @Override
107     public void close() throws Exception {
108
109     }
110 }