Update MRI projects for Aluminium
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / 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 package org.opendaylight.netconf.mdsal.notification.impl.ops;
9
10 import static com.google.common.base.Verify.verify;
11
12 import java.io.IOException;
13 import java.util.Date;
14 import java.util.Optional;
15 import javax.inject.Singleton;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.transform.dom.DOMResult;
18 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
19 import org.opendaylight.binding.runtime.api.BindingRuntimeGenerator;
20 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
22 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.netconf.util.NetconfUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfigChange;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.YangLibraryChange;
29 import org.opendaylight.yangtools.yang.binding.Notification;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
34 import org.w3c.dom.Document;
35
36 @Singleton
37 public final class NotificationsTransformUtil {
38     private final SchemaContext schemaContext;
39     private final BindingNormalizedNodeSerializer serializer;
40
41     public NotificationsTransformUtil(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
42             final BindingDOMCodecFactory codecFactory) {
43         final BindingRuntimeContext ctx = BindingRuntimeHelpers.createRuntimeContext(parserFactory, generator,
44             Netconf.class, NetconfConfigChange.class, YangLibraryChange.class);
45         schemaContext = ctx.getSchemaContext();
46         verify(schemaContext.getOperations().stream()
47                 .filter(input -> input.getQName().getLocalName().equals(CreateSubscription.CREATE_SUBSCRIPTION))
48                 .findFirst()
49                 .isPresent());
50         serializer = codecFactory.createBindingDOMCodec(ctx);
51     }
52
53     /**
54      * Transform base notification for capabilities into NetconfNotification.
55      */
56     public NetconfNotification transform(final Notification notification, final SchemaPath path) {
57         return transform(notification, Optional.empty(), path);
58     }
59
60     public NetconfNotification transform(final Notification notification, final Date eventTime, final SchemaPath path) {
61         return transform(notification, Optional.ofNullable(eventTime), path);
62     }
63
64     private NetconfNotification transform(final Notification notification, final Optional<Date> eventTime,
65             final SchemaPath path) {
66         final ContainerNode containerNode = serializer.toNormalizedNodeNotification(notification);
67         final DOMResult result = new DOMResult(XmlUtil.newDocument());
68         try {
69             NetconfUtil.writeNormalizedNode(containerNode, result, path, schemaContext);
70         } catch (final XMLStreamException | IOException e) {
71             throw new IllegalStateException("Unable to serialize " + notification, e);
72         }
73         final Document node = (Document) result.getNode();
74         return eventTime.isPresent() ? new NetconfNotification(node, eventTime.get()) : new NetconfNotification(node);
75     }
76 }