Merge "BUG-421: Define multipart-transaction-aware"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / DataReaderRouter.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.dom.broker.impl
9
10 import java.net.URI
11 import java.util.ArrayList
12 import java.util.Collection
13 import java.util.Collections
14 import java.util.HashMap
15 import java.util.Map
16 import java.util.Map.Entry
17 import java.util.Set
18 import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter
19 import org.opendaylight.yangtools.yang.common.QName
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
24 import org.opendaylight.yangtools.yang.data.api.Node
25 import org.opendaylight.yangtools.yang.data.api.SimpleNode
26 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
27 import org.slf4j.LoggerFactory
28
29 import static com.google.common.base.Preconditions.*
30
31 class DataReaderRouter extends AbstractDataReadRouter<InstanceIdentifier, CompositeNode> {
32     private static val LOG = LoggerFactory.getLogger(DataReaderRouter);
33     private static val NETCONF_NAMESPACE = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
34     private static val NETCONF_DATA = new QName(NETCONF_NAMESPACE,"data");
35
36     override protected merge(InstanceIdentifier path, Iterable<CompositeNode> data) {
37         val pathArgument = path.path.last;
38         var empty = true;
39         var name = pathArgument?.nodeType;
40         val nodes = new ArrayList<Node<?>>();
41         val keyNodes = new HashMap<QName, SimpleNode<?>>();
42         for(dataBit : data) {
43             try {
44                 if(pathArgument != null && dataBit != null) {
45                     empty = false;
46                     val keyNodesLocal = getKeyNodes(pathArgument,dataBit);
47                     nodes.addAll(dataBit.childrenWithout(keyNodesLocal.entrySet));
48                 } else if (dataBit != null) {
49                     empty = false;
50                     nodes.addAll(dataBit.children)
51                 }
52             }   catch (IllegalStateException e) {
53                 LOG.error("BUG: Readed data for path {} was invalid",path,e);
54             }
55         }
56         if(empty) {
57             return null;
58         }
59         /**
60          * Reading from Root
61          * 
62          */
63         if(pathArgument == null) {
64             return new CompositeNodeTOImpl(NETCONF_DATA,null,nodes);
65         }
66         val finalNodes = new ArrayList<Node<?>>();
67         finalNodes.addAll(keyNodes.values);
68         finalNodes.addAll(nodes);
69         return new CompositeNodeTOImpl(name,null,finalNodes);
70     }
71     
72     
73     
74     dispatch def Map<QName, SimpleNode<?>> getKeyNodes(PathArgument argument, CompositeNode node) {
75         return Collections.emptyMap();
76     }
77     
78     dispatch def getKeyNodes(NodeIdentifierWithPredicates argument, CompositeNode node) {
79         val ret = new HashMap<QName, SimpleNode<?>>();
80         for (keyValue : argument.keyValues.entrySet) {
81             val simpleNode = node.getSimpleNodesByName(keyValue.key);
82             if(simpleNode !== null && !simpleNode.empty) {
83                 checkState(simpleNode.size <= 1,"Only one simple node for key $s is allowed in node $s",keyValue.key,node);
84                 checkState(simpleNode.get(0).value == keyValue.value,"Key node must equal to instance identifier value in node $s",node);
85                 ret.put(keyValue.key,simpleNode.get(0));
86             }
87             val compositeNode = node.getCompositesByName(keyValue.key);
88             checkState(compositeNode === null || compositeNode.empty,"Key node must be Simple Node, not composite node.");
89         }
90         return ret;
91     }
92     
93     def Collection<? extends Node<?>> childrenWithout(CompositeNode node, Set<Entry<QName, SimpleNode<?>>> entries) {
94         if(entries.empty) {
95             return node.children;
96         }
97         val filteredNodes = new ArrayList<Node<?>>();
98         for(scannedNode : node.children) {
99             if(!entries.contains(scannedNode.nodeType)) {
100                 filteredNodes.add(scannedNode);
101             }
102         }
103         return filteredNodes;
104     }
105     
106 }