Convert mdsal-netconf-notification to OSGi DS
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / impl / 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;
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.xml.stream.XMLStreamException;
16 import javax.xml.transform.dom.DOMResult;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
19 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
21 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.notifications.NetconfNotification;
24 import org.opendaylight.netconf.util.NetconfUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfigChange;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChange;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdate;
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.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
33 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
34 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
35 import org.w3c.dom.Document;
36
37 public final class NotificationsTransformUtil {
38     private final EffectiveModelContext schemaContext;
39     private final BindingNormalizedNodeSerializer serializer;
40
41     public NotificationsTransformUtil(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
42             final BindingDOMCodecFactory codecFactory) throws YangParserException {
43         final BindingRuntimeContext ctx = BindingRuntimeHelpers.createRuntimeContext(parserFactory, generator,
44             Netconf.class, NetconfConfigChange.class, YangLibraryChange.class, YangLibraryUpdate.class);
45         schemaContext = ctx.getEffectiveModelContext();
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 Absolute path) {
57         return transform(notification, Optional.empty(), path);
58     }
59
60     public NetconfNotification transform(final Notification<?> notification, final Date eventTime,
61             final Absolute path) {
62         return transform(notification, Optional.ofNullable(eventTime), path);
63     }
64
65     private NetconfNotification transform(final Notification<?> notification, final Optional<Date> eventTime,
66             final Absolute path) {
67         final ContainerNode containerNode = serializer.toNormalizedNodeNotification(notification);
68         final DOMResult result = new DOMResult(XmlUtil.newDocument());
69         try {
70             NetconfUtil.writeNormalizedNode(containerNode, result, schemaContext, path);
71         } catch (final XMLStreamException | IOException e) {
72             throw new IllegalStateException("Unable to serialize " + notification, e);
73         }
74         final Document node = (Document) result.getNode();
75         return eventTime.isPresent() ? new NetconfNotification(node, eventTime.get()) : new NetconfNotification(node);
76     }
77 }