/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.dom.adapter; import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; import org.opendaylight.mdsal.binding.api.DataTreeListener; import org.opendaylight.mdsal.binding.api.DataTreeListeningException; import org.opendaylight.mdsal.binding.api.DataTreeModification; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; import org.opendaylight.mdsal.dom.api.DOMDataTreeListener; import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; public class BindingDOMDataTreeListenerAdapter implements DOMDataTreeListener { private final DataTreeListener delegate; private final BindingToNormalizedNodeCodec codec; private final LogicalDatastoreType store; protected BindingDOMDataTreeListenerAdapter(final DataTreeListener delegate, final BindingToNormalizedNodeCodec codec, final LogicalDatastoreType store) { this.delegate = Preconditions.checkNotNull(delegate, "delegate"); this.codec = Preconditions.checkNotNull(codec, "codec"); this.store = Preconditions.checkNotNull(store, "store"); } @Override public void onDataTreeChanged(final Collection domChanges, final Map> domSubtrees) { final Collection> changes = toBinding(domChanges); final Map, DataObject> subtrees = toBinding(domSubtrees); delegate.onDataTreeChanged(changes, subtrees); } private Map, DataObject> toBinding( final Map> domSubtrees) { // FIXME: Introduce lazy translating map final Map, DataObject> ret = Maps.newHashMapWithExpectedSize(domSubtrees.size()); for (final Entry> domEntry : domSubtrees.entrySet()) { final Entry, DataObject> bindingEntry = codec.fromNormalizedNode(domEntry.getKey().getRootIdentifier(), domEntry.getValue()); ret.put(DataTreeIdentifier.create(store, bindingEntry.getKey()), bindingEntry.getValue()); } return ret; } @SuppressWarnings("unchecked") private Collection> toBinding(final Collection domChanges) { return Collection.class.cast(LazyDataTreeModification.from(codec, domChanges, store)); } @Override public void onDataTreeFailed(final Collection causes) { List bindingCauses = new ArrayList<>(causes.size()); for (DOMDataTreeListeningException cause : causes) { bindingCauses.add(mapException(cause)); } delegate.onDataTreeFailed(bindingCauses); } private static DataTreeListeningException mapException(final DOMDataTreeListeningException cause) { // FIXME: Extend logic return new DataTreeListeningException(cause.getMessage(), cause); } }