d2fdaaddb77ccef5627e0cae32265b7da397c3aa
[netconf.git] / netconf / netconf-notifications-impl / src / main / java / org / opendaylight / netconf / notifications / impl / ops / NotificationsTransformUtil.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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Iterables;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.IOException;
17 import java.util.Collections;
18 import java.util.Date;
19 import javassist.ClassPool;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.transform.dom.DOMResult;
22 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
23 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
24 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
25 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
26 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.opendaylight.netconf.notifications.NetconfNotification;
29 import org.opendaylight.netconf.util.NetconfUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.$YangModuleInfoImpl;
31 import org.opendaylight.yangtools.yang.binding.Notification;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39
40 public final class NotificationsTransformUtil {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NotificationsTransformUtil.class);
43
44     private NotificationsTransformUtil() {}
45
46     static final SchemaContext NOTIFICATIONS_SCHEMA_CTX;
47     static final BindingNormalizedNodeCodecRegistry CODEC_REGISTRY;
48     static final RpcDefinition CREATE_SUBSCRIPTION_RPC;
49
50     static {
51
52         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
53         moduleInfoBackedContext.addModuleInfos(Collections.singletonList($YangModuleInfoImpl.getInstance()));
54         moduleInfoBackedContext.addModuleInfos(Collections.singletonList(org.opendaylight.yang.gen.v1.urn.ietf.params
55                 .xml.ns.yang.ietf.netconf.notifications.rev120206.$YangModuleInfoImpl.getInstance()));
56         final Optional<SchemaContext> schemaContextOptional = moduleInfoBackedContext.tryToCreateSchemaContext();
57         Preconditions.checkState(schemaContextOptional.isPresent());
58         NOTIFICATIONS_SCHEMA_CTX = schemaContextOptional.get();
59
60         CREATE_SUBSCRIPTION_RPC = Preconditions.checkNotNull(findCreateSubscriptionRpc());
61
62         Preconditions.checkNotNull(CREATE_SUBSCRIPTION_RPC);
63
64         final JavassistUtils javassist = JavassistUtils.forClassPool(ClassPool.getDefault());
65         CODEC_REGISTRY = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(javassist));
66         CODEC_REGISTRY.onBindingRuntimeContextUpdated(BindingRuntimeContext.create(moduleInfoBackedContext,
67                 NOTIFICATIONS_SCHEMA_CTX));
68     }
69
70     @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Unrecognised NullableDecl")
71     private static RpcDefinition findCreateSubscriptionRpc() {
72         return Iterables.getFirst(Collections2.filter(NOTIFICATIONS_SCHEMA_CTX.getOperations(),
73             input -> input.getQName().getLocalName().equals(CreateSubscription.CREATE_SUBSCRIPTION)), null);
74     }
75
76     /**
77      * Transform base notification for capabilities into NetconfNotification.
78      */
79     public static NetconfNotification transform(final Notification notification, SchemaPath path) {
80         return transform(notification, Optional.<Date>absent(), path);
81     }
82
83     public static NetconfNotification transform(final Notification notification,
84                                                 final Date eventTime, SchemaPath path) {
85         return transform(notification, Optional.fromNullable(eventTime), path);
86     }
87
88     private static NetconfNotification transform(final Notification notification,
89                                                  final Optional<Date> eventTime, SchemaPath path) {
90         final ContainerNode containerNode = CODEC_REGISTRY.toNormalizedNodeNotification(notification);
91         final DOMResult result = new DOMResult(XmlUtil.newDocument());
92         try {
93             NetconfUtil.writeNormalizedNode(containerNode, result, path, NOTIFICATIONS_SCHEMA_CTX);
94         } catch (final XMLStreamException | IOException e) {
95             throw new IllegalStateException("Unable to serialize " + notification, e);
96         }
97         final Document node = (Document) result.getNode();
98         return eventTime.isPresent() ? new NetconfNotification(node, eventTime.get()) : new NetconfNotification(node);
99     }
100
101 }