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