BUG-832 Refactor netconf connector
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / schema / mapping / NetconfMessageTransformer.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.connect.netconf.schema.mapping;
9
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Set;
13
14 import javax.activation.UnsupportedDataTypeException;
15
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;
33
34 import com.google.common.base.Optional;
35
36 public class NetconfMessageTransformer implements MessageTransformer<NetconfMessage> {
37
38     public static final String MESSAGE_ID_PREFIX = "m";
39
40     private Optional<SchemaContext> schemaContext = Optional.absent();
41     private final MessageCounter counter;
42
43     public NetconfMessageTransformer() {
44         this.counter = new MessageCounter();
45     }
46
47     @Override
48     public synchronized CompositeNode toNotification(final NetconfMessage message) {
49         if(schemaContext.isPresent()) {
50             return toNotification(message, schemaContext.get());
51         } else {
52             return XmlDocumentUtils.notificationToDomNodes(message.getDocument(), Optional.<Set<NotificationDefinition>>absent());
53         }
54     }
55
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));
60     }
61
62     @Override
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;
67         try {
68             w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, XmlDocumentUtils.defaultValueCodecProvider());
69         } catch (final UnsupportedDataTypeException e) {
70             throw new IllegalArgumentException("Unable to create message", e);
71         }
72         w3cPayload.getDocumentElement().setAttribute("message-id", counter.getNewMessageId(MESSAGE_ID_PREFIX));
73         return new NetconfMessage(w3cPayload);
74     }
75
76     @Override
77     public synchronized RpcResult<CompositeNode> toRpcResult(final NetconfMessage message, final QName rpc) {
78         if(schemaContext.isPresent()) {
79             return toRpcResult(message, rpc, schemaContext.get());
80         } else {
81             final CompositeNode node = (CompositeNode) XmlDocumentUtils.toDomNode(message.getDocument());
82             return Rpcs.getRpcResult(true, node, Collections.<RpcError>emptySet());
83         }
84     }
85
86     private static RpcResult<CompositeNode> toRpcResult(final NetconfMessage message, final QName rpc, final SchemaContext context) {
87         final CompositeNode compositeNode;
88
89         if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpc)) {
90
91             final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
92
93             final List<org.opendaylight.yangtools.yang.data.api.Node<?>> dataNodes = XmlDocumentUtils.toDomNodes(xmlData,
94                     Optional.of(context.getDataDefinitions()), context);
95
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));
99
100             compositeNode = it.toInstance();
101         } else {
102             // TODO map rpc with schema
103             compositeNode = (CompositeNode) XmlDocumentUtils.toDomNode(message.getDocument());
104         }
105
106         return Rpcs.getRpcResult(true, compositeNode, Collections.<RpcError> emptySet());
107     }
108
109     @Override
110     public synchronized void onGlobalContextUpdated(final SchemaContext schemaContext) {
111         this.schemaContext = Optional.of(schemaContext);
112     }
113 }