2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.mdsal.notification.impl.ops;
10 import static com.google.common.base.Verify.verify;
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.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
20 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
21 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeGenerator;
22 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
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.rev190104.YangLibraryChange;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdate;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
35 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
36 import org.w3c.dom.Document;
39 public final class NotificationsTransformUtil {
40 private final EffectiveModelContext schemaContext;
41 private final BindingNormalizedNodeSerializer serializer;
43 public NotificationsTransformUtil(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
44 final BindingDOMCodecFactory codecFactory) throws YangParserException {
45 final BindingRuntimeContext ctx = BindingRuntimeHelpers.createRuntimeContext(parserFactory, generator,
46 Netconf.class, NetconfConfigChange.class, YangLibraryChange.class, YangLibraryUpdate.class);
47 schemaContext = ctx.getEffectiveModelContext();
48 verify(schemaContext.getOperations().stream()
49 .filter(input -> input.getQName().getLocalName().equals(CreateSubscription.CREATE_SUBSCRIPTION))
52 serializer = codecFactory.createBindingDOMCodec(ctx);
56 * Transform base notification for capabilities into NetconfNotification.
58 public NetconfNotification transform(final Notification notification, final SchemaPath path) {
59 return transform(notification, Optional.empty(), path);
62 public NetconfNotification transform(final Notification notification, final Date eventTime, final SchemaPath path) {
63 return transform(notification, Optional.ofNullable(eventTime), path);
66 private NetconfNotification transform(final Notification notification, final Optional<Date> eventTime,
67 final SchemaPath path) {
68 final ContainerNode containerNode = serializer.toNormalizedNodeNotification(notification);
69 final DOMResult result = new DOMResult(XmlUtil.newDocument());
71 NetconfUtil.writeNormalizedNode(containerNode, result, path, schemaContext);
72 } catch (final XMLStreamException | IOException e) {
73 throw new IllegalStateException("Unable to serialize " + notification, e);
75 final Document node = (Document) result.getNode();
76 return eventTime.isPresent() ? new NetconfNotification(node, eventTime.get()) : new NetconfNotification(node);