e5a24fcf63914f35492f50e7186ff2ed844761e4
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfMapping.xtend
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
9
10 import com.google.common.base.Optional
11 import com.google.common.base.Preconditions
12 import com.google.common.collect.ImmutableList
13 import java.net.URI
14 import java.util.ArrayList
15 import java.util.Collections
16 import java.util.List
17 import java.util.Set
18 import java.util.concurrent.atomic.AtomicInteger
19 import org.opendaylight.controller.netconf.api.NetconfMessage
20 import org.opendaylight.controller.sal.common.util.Rpcs
21 import org.opendaylight.yangtools.yang.common.QName
22 import org.opendaylight.yangtools.yang.common.RpcResult
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
27 import org.opendaylight.yangtools.yang.data.api.Node
28 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
29 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
30 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
31 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils
32 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext
34 import org.w3c.dom.Document
35 import org.w3c.dom.Element
36
37 class NetconfMapping {
38
39     public static val NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
40     public static val NETCONF_MONITORING_URI = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"
41     public static val NETCONF_NOTIFICATION_URI = URI.create("urn:ietf:params:xml:ns:netconf:notification:1.0")
42     
43     
44     public static val NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
45     public static val NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
46     public static val NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
47     public static val NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
48     public static val NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
49     public static val NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
50     public static val NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
51     public static val NETCONF_DELETE_CONFIG_QNAME = QName.create(NETCONF_QNAME, "delete-config");
52     public static val NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation");
53     public static val NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
54     
55     public static val NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
56     public static val NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
57     public static val NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
58     
59     public static val NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
60     public static val NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
61     
62     
63     public static val NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
64     public static val NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok");
65     public static val NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
66     public static val NETCONF_CREATE_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"create-subscription");
67     public static val NETCONF_CANCEL_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"cancel-subscription");
68     public static val IETF_NETCONF_MONITORING_MODULE = QName.create(NETCONF_MONITORING_URI, "2010-10-04","ietf-netconf-monitoring");
69
70     static List<Node<?>> RUNNING = Collections.<Node<?>>singletonList(
71         new SimpleNodeTOImpl(NETCONF_RUNNING_QNAME, null, null));
72     public static val CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING);
73
74     static val messageId = new AtomicInteger(0);
75
76     static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
77         var Node<?> previous = null;
78         if(identifier.path.empty) {
79             return null;
80         }
81         
82         for (component : identifier.path.reverseView) {
83             val Node<?> current = component.toNode(previous);
84             previous = current;
85         }
86         return filter("subtree",previous);
87     }
88
89     static def dispatch Node<?> toNode(NodeIdentifierWithPredicates argument, Node<?> node) {
90         val list = new ArrayList<Node<?>>();
91         for (arg : argument.keyValues.entrySet) {
92             list.add = new SimpleNodeTOImpl(arg.key, null, arg.value);
93         }
94         if (node != null) {
95             list.add(node);
96         }
97         return new CompositeNodeTOImpl(argument.nodeType, null, list)
98     }
99
100     static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
101         if (node != null) {
102             return new CompositeNodeTOImpl(argument.nodeType, null, Collections.singletonList(node));
103         } else {
104             return new SimpleNodeTOImpl(argument.nodeType, null, null);
105         }
106     }
107
108     static def CompositeNode toCompositeNode(NetconfMessage message,Optional<SchemaContext> ctx) {
109         //TODO: implement general normalization to normalize incoming Netconf Message 
110         // for Schema Context counterpart
111         return null
112     }
113     
114     static def CompositeNode toNotificationNode(NetconfMessage message,Optional<SchemaContext> ctx) {
115         if (ctx.present) {
116             val schemaContext = ctx.get
117             val notifications = schemaContext.notifications
118             val document = message.document
119             return XmlDocumentUtils.notificationToDomNodes(document, Optional.<Set<NotificationDefinition>>fromNullable(notifications))
120         }
121         return null
122     }
123
124     static def NetconfMessage toRpcMessage(QName rpc, CompositeNode node,Optional<SchemaContext> ctx) {
125         val rpcPayload = wrap(NETCONF_RPC_QNAME, flattenInput(node))
126         val w3cPayload = XmlDocumentUtils.toDocument(rpcPayload, XmlDocumentUtils.defaultValueCodecProvider)
127         w3cPayload.documentElement.setAttribute("message-id", "m-" + messageId.andIncrement)
128         return new NetconfMessage(w3cPayload);
129     }
130     
131     def static flattenInput(CompositeNode node) {
132         val inputQName = QName.create(node.nodeType,"input");
133         val input = node.getFirstCompositeByName(inputQName);
134         if(input == null) return node;
135         if(input instanceof CompositeNode) {
136             
137             val nodes = ImmutableList.builder() //
138                 .addAll(input.children) //
139                 .addAll(node.children.filter[nodeType != inputQName]) //
140                 .build()
141             return ImmutableCompositeNode.create(node.nodeType,nodes);
142         } 
143         
144     }
145
146     static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message,QName rpc,Optional<SchemaContext> context) {
147         var CompositeNode rawRpc;
148         if(context.present) {
149             if(isDataRetrievalReply(rpc)) {
150                 
151                 val xmlData = message.document.dataSubtree
152                 val dataNodes = XmlDocumentUtils.toDomNodes(xmlData, Optional.of(context.get.dataDefinitions))
153                 
154                 val it = ImmutableCompositeNode.builder()
155                 setQName(NETCONF_RPC_REPLY_QNAME)
156                 add(ImmutableCompositeNode.create(NETCONF_DATA_QNAME, dataNodes));
157                 
158                 rawRpc = it.toInstance;
159                 //sys(xmlData)
160             } else {
161                 val rpcSchema = context.get.operations.findFirst[QName == rpc]
162                 rawRpc = message.document.toCompositeNode() as CompositeNode;
163             }
164             
165             
166             
167         } else {
168             rawRpc = message.document.toCompositeNode() as CompositeNode;
169         }
170         //rawRpc.
171         return Rpcs.getRpcResult(true, rawRpc, Collections.emptySet());
172     }
173     
174     def static Element getDataSubtree(Document doc) {
175         doc.getElementsByTagNameNS(NETCONF_URI.toString,"data").item(0) as Element
176     }
177     
178     def static boolean isDataRetrievalReply(QName it) {
179         return NETCONF_URI == namespace && ( localName == NETCONF_GET_CONFIG_QNAME.localName || localName == NETCONF_GET_QNAME.localName) 
180     }
181
182     static def wrap(QName name, Node<?> node) {
183         if (node != null) {
184             return new CompositeNodeTOImpl(name, null, Collections.singletonList(node));
185         } else {
186             return new CompositeNodeTOImpl(name, null, Collections.emptyList());
187         }
188     }
189
190     static def wrap(QName name, Node<?> additional, Node<?> node) {
191         if (node != null) {
192             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
193         } else {
194             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional));
195         }
196     }
197
198     static def filter(String type, Node<?> node) {
199         val it = ImmutableCompositeNode.builder(); //
200         setQName(NETCONF_FILTER_QNAME);
201         setAttribute(NETCONF_TYPE_QNAME,type);
202         if (node != null) {
203             return add(node).toInstance();
204         } else {
205             return toInstance();
206         }
207     }
208
209     public static def Node<?> toCompositeNode(Document document) {
210         return XmlDocumentUtils.toDomNode(document) as Node<?>
211     }
212     
213     public static def checkValidReply(NetconfMessage input, NetconfMessage output) {
214         val inputMsgId = input.document.documentElement.getAttribute("message-id")
215         val outputMsgId = output.document.documentElement.getAttribute("message-id")
216         Preconditions.checkState(inputMsgId == outputMsgId,"Rpc request and reply message IDs must be same.");
217         
218     }
219     
220 }