2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.dom.broker.impl
11 import java.util.ArrayList
12 import java.util.Collection
13 import java.util.Collections
14 import java.util.HashMap
16 import java.util.Map.Entry
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
29 import static com.google.common.base.Preconditions.*
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");
36 override protected merge(InstanceIdentifier path, Iterable<CompositeNode> data) {
37 val pathArgument = path.path.last;
39 var name = pathArgument?.nodeType;
40 val nodes = new ArrayList<Node<?>>();
41 val keyNodes = new HashMap<QName, SimpleNode<?>>();
44 if(pathArgument != null && dataBit != null) {
46 val keyNodesLocal = getKeyNodes(pathArgument,dataBit);
47 nodes.addAll(dataBit.childrenWithout(keyNodesLocal.entrySet));
48 } else if (dataBit != null) {
50 nodes.addAll(dataBit.children)
52 } catch (IllegalStateException e) {
53 LOG.error("BUG: Readed data for path {} was invalid",path,e);
63 if(pathArgument == null) {
64 return new CompositeNodeTOImpl(NETCONF_DATA,null,nodes);
66 val finalNodes = new ArrayList<Node<?>>();
67 finalNodes.addAll(keyNodes.values);
68 finalNodes.addAll(nodes);
69 return new CompositeNodeTOImpl(name,null,finalNodes);
74 dispatch def Map<QName, SimpleNode<?>> getKeyNodes(PathArgument argument, CompositeNode node) {
75 return Collections.emptyMap();
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));
87 val compositeNode = node.getCompositesByName(keyValue.key);
88 checkState(compositeNode === null || compositeNode.empty,"Key node must be Simple Node, not composite node.");
93 def Collection<? extends Node<?>> childrenWithout(CompositeNode node, Set<Entry<QName, SimpleNode<?>>> entries) {
97 val filteredNodes = new ArrayList<Node<?>>();
98 for(scannedNode : node.children) {
99 if(!entries.contains(scannedNode.nodeType)) {
100 filteredNodes.add(scannedNode);
103 return filteredNodes;