/* * 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.collect.ImmutableSet; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier; import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration; import org.opendaylight.mdsal.dom.api.DOMRpcProviderService; import org.opendaylight.yangtools.concepts.ObjectRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaPath; public class BindingDOMRpcProviderServiceAdapter extends AbstractBindingAdapter implements RpcProviderService { private static final ImmutableSet GLOBAL = ImmutableSet.of(YangInstanceIdentifier.empty()); public BindingDOMRpcProviderServiceAdapter(final DOMRpcProviderService domRpcRegistry, final BindingToNormalizedNodeCodec codec) { super(codec, domRpcRegistry); } @Override public ObjectRegistration registerRpcImplementation(final Class type, final T implementation) { return register(type, implementation, GLOBAL); } @Override public ObjectRegistration registerRpcImplementation(final Class type, final T implementation, final Set> paths) { return register(type, implementation, toYangInstanceIdentifiers(paths)); } private ObjectRegistration register(final Class type, final T implementation, final Collection rpcContextPaths) { final Map rpcs = getCodec().getRpcMethodToSchemaPath(type).inverse(); final BindingDOMRpcImplementationAdapter adapter = new BindingDOMRpcImplementationAdapter( getCodec().getCodecRegistry(), type, rpcs, implementation); final Set domRpcs = createDomRpcIdentifiers(rpcs.keySet(), rpcContextPaths); final DOMRpcImplementationRegistration domReg = getDelegate().registerRpcImplementation(adapter, domRpcs); return new BindingRpcAdapterRegistration<>(implementation, domReg); } private static Set createDomRpcIdentifiers(final Set rpcs, final Collection paths) { final Set ret = new HashSet<>(); for (final YangInstanceIdentifier path : paths) { for (final SchemaPath rpc : rpcs) { ret.add(DOMRpcIdentifier.create(rpc, path)); } } return ret; } private Collection toYangInstanceIdentifiers(final Set> identifiers) { final Collection ret = new ArrayList<>(identifiers.size()); for (final InstanceIdentifier binding : identifiers) { ret.add(getCodec().toYangInstanceIdentifierCached(binding)); } return ret; } }