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