2 * Copyright (c) 2014 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.controller.sal.connect.netconf.schema.mapping;
10 import java.util.Collections;
11 import java.util.List;
14 import javax.activation.UnsupportedDataTypeException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.sal.common.util.Rpcs;
18 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
19 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
20 import org.opendaylight.controller.sal.connect.util.MessageCounter;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
26 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
27 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
28 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
34 import com.google.common.base.Optional;
36 public class NetconfMessageTransformer implements MessageTransformer<NetconfMessage> {
38 public static final String MESSAGE_ID_PREFIX = "m";
40 private Optional<SchemaContext> schemaContext = Optional.absent();
41 private final MessageCounter counter;
43 public NetconfMessageTransformer() {
44 this.counter = new MessageCounter();
48 public synchronized CompositeNode toNotification(final NetconfMessage message) {
49 if(schemaContext.isPresent()) {
50 return toNotification(message, schemaContext.get());
52 return XmlDocumentUtils.notificationToDomNodes(message.getDocument(), Optional.<Set<NotificationDefinition>>absent());
56 private static CompositeNode toNotification(final NetconfMessage message, final SchemaContext ctx) {
57 final Set<NotificationDefinition> notifications = ctx.getNotifications();
58 final Document document = message.getDocument();
59 return XmlDocumentUtils.notificationToDomNodes(document, Optional.fromNullable(notifications));
63 public NetconfMessage toRpcRequest(final QName rpc, final CompositeNode node) {
64 final CompositeNodeTOImpl rpcPayload = NetconfMessageTransformUtil.wrap(
65 NetconfMessageTransformUtil.NETCONF_RPC_QNAME, NetconfMessageTransformUtil.flattenInput(node));
66 final Document w3cPayload;
68 w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, XmlDocumentUtils.defaultValueCodecProvider());
69 } catch (final UnsupportedDataTypeException e) {
70 throw new IllegalArgumentException("Unable to create message", e);
72 w3cPayload.getDocumentElement().setAttribute("message-id", counter.getNewMessageId(MESSAGE_ID_PREFIX));
73 return new NetconfMessage(w3cPayload);
77 public synchronized RpcResult<CompositeNode> toRpcResult(final NetconfMessage message, final QName rpc) {
78 if(schemaContext.isPresent()) {
79 return toRpcResult(message, rpc, schemaContext.get());
81 final CompositeNode node = (CompositeNode) XmlDocumentUtils.toDomNode(message.getDocument());
82 return Rpcs.getRpcResult(true, node, Collections.<RpcError>emptySet());
86 private static RpcResult<CompositeNode> toRpcResult(final NetconfMessage message, final QName rpc, final SchemaContext context) {
87 final CompositeNode compositeNode;
89 if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpc)) {
91 final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
93 final List<org.opendaylight.yangtools.yang.data.api.Node<?>> dataNodes = XmlDocumentUtils.toDomNodes(xmlData,
94 Optional.of(context.getDataDefinitions()), context);
96 final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder();
97 it.setQName(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME);
98 it.add(ImmutableCompositeNode.create(NetconfMessageTransformUtil.NETCONF_DATA_QNAME, dataNodes));
100 compositeNode = it.toInstance();
102 // TODO map rpc with schema
103 compositeNode = (CompositeNode) XmlDocumentUtils.toDomNode(message.getDocument());
106 return Rpcs.getRpcResult(true, compositeNode, Collections.<RpcError> emptySet());
110 public synchronized void onGlobalContextUpdated(final SchemaContext schemaContext) {
111 this.schemaContext = Optional.of(schemaContext);