X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2Fconnect%2Fdom%2FBindingIndependentConnector.java;h=2b40437d6446a9cce652b8245c82c3dc2342e8f9;hb=0555c464b10ce8cac729608196e7af3c6ec1652b;hp=9edea0c2fd59fb9f50b94551fbf5574a174a2a07;hpb=ba1f2b8b1012ca100c84052f082bfa54d8004426;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java index 9edea0c2fd..2b40437d64 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java @@ -62,7 +62,6 @@ import org.opendaylight.controller.sal.core.api.notify.NotificationListener; import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.Registration; -import org.opendaylight.yangtools.concepts.util.ClassLoaderUtils; import org.opendaylight.yangtools.yang.binding.Augmentable; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.BaseIdentity; @@ -73,6 +72,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.yangtools.yang.binding.util.BindingReflections; +import org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; @@ -91,17 +91,15 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; public class BindingIndependentConnector implements // RuntimeDataProvider, // Provider, // AutoCloseable { - - private final Logger LOG = LoggerFactory.getLogger(BindingIndependentConnector.class); - private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier ROOT_BI = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier .builder().toInstance(); @@ -199,30 +197,30 @@ public class BindingIndependentConnector implements // private DataModificationTransaction createBindingToDomTransaction( final DataModification, DataObject> source) { DataModificationTransaction target = biDataService.beginTransaction(); - LOG.debug("Created DOM Transaction {} for {},", target.getIdentifier(),source.getIdentifier()); + LOG.debug("Created DOM Transaction {} for {},", target.getIdentifier(), source.getIdentifier()); for (InstanceIdentifier entry : source.getRemovedConfigurationData()) { org.opendaylight.yangtools.yang.data.api.InstanceIdentifier biEntry = mappingService.toDataDom(entry); target.removeConfigurationData(biEntry); - LOG.debug("Delete of Binding Configuration Data {} is translated to {}",entry,biEntry); + LOG.debug("Delete of Binding Configuration Data {} is translated to {}", entry, biEntry); } for (InstanceIdentifier entry : source.getRemovedOperationalData()) { org.opendaylight.yangtools.yang.data.api.InstanceIdentifier biEntry = mappingService.toDataDom(entry); target.removeOperationalData(biEntry); - LOG.debug("Delete of Binding Operational Data {} is translated to {}",entry,biEntry); + LOG.debug("Delete of Binding Operational Data {} is translated to {}", entry, biEntry); } for (Entry, DataObject> entry : source.getUpdatedConfigurationData() .entrySet()) { Entry biEntry = mappingService .toDataDom(entry); target.putConfigurationData(biEntry.getKey(), biEntry.getValue()); - LOG.debug("Update of Binding Configuration Data {} is translated to {}",entry,biEntry); + LOG.debug("Update of Binding Configuration Data {} is translated to {}", entry, biEntry); } for (Entry, DataObject> entry : source.getUpdatedOperationalData() .entrySet()) { Entry biEntry = mappingService .toDataDom(entry); target.putOperationalData(biEntry.getKey(), biEntry.getValue()); - LOG.debug("Update of Binding Operational Data {} is translated to {}",entry,biEntry); + LOG.debug("Update of Binding Operational Data {} is translated to {}", entry, biEntry); } return target; @@ -278,7 +276,8 @@ public class BindingIndependentConnector implements // return biDataService; } - protected void setDomDataService(final org.opendaylight.controller.sal.core.api.data.DataProviderService biDataService) { + protected void setDomDataService( + final org.opendaylight.controller.sal.core.api.data.DataProviderService biDataService) { this.biDataService = biDataService; } @@ -299,14 +298,14 @@ public class BindingIndependentConnector implements // } public void startDataForwarding() { - if(baDataService instanceof AbstractForwardedDataBroker) { + if (baDataService instanceof AbstractForwardedDataBroker) { dataForwarding = true; return; } final DataProviderService baData; if (baDataService instanceof BindingMountPointImpl) { - baData = ((BindingMountPointImpl)baDataService).getDataBrokerImpl(); + baData = ((BindingMountPointImpl) baDataService).getDataBrokerImpl(); LOG.debug("Extracted BA Data provider {} from mount point {}", baData, baDataService); } else { baData = baDataService; @@ -324,7 +323,7 @@ public class BindingIndependentConnector implements // } public void startRpcForwarding() { - if (baRpcRegistry != null && biRpcRegistry != null && baRpcRegistry instanceof RouteChangePublisher) { + if (biRpcRegistry != null && baRpcRegistry instanceof RouteChangePublisher) { checkState(!rpcForwarding, "Connector is already forwarding RPCs"); domToBindingRpcManager = baRpcRegistry.registerRouteChangeListener(new DomToBindingRpcForwardingManager()); if (baRpcRegistry instanceof RpcProviderRegistryImpl) { @@ -477,8 +476,8 @@ public class BindingIndependentConnector implements // } DataModificationTransaction domTransaction = createBindingToDomTransaction(bindingTransaction); BindingToDomTransaction wrapped = new BindingToDomTransaction(domTransaction, bindingTransaction); - LOG.trace("Forwarding Binding Transaction: {} as DOM Transaction: {} .", bindingTransaction.getIdentifier(), - domTransaction.getIdentifier()); + LOG.trace("Forwarding Binding Transaction: {} as DOM Transaction: {} .", + bindingTransaction.getIdentifier(), domTransaction.getIdentifier()); return wrapped; } } @@ -488,7 +487,8 @@ public class BindingIndependentConnector implements // DataCommitHandler { @Override - public void onRegister(final DataCommitHandlerRegistration, DataObject> registration) { + public void onRegister( + final DataCommitHandlerRegistration, DataObject> registration) { org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = mappingService.toDataDom(registration .getPath()); @@ -496,7 +496,8 @@ public class BindingIndependentConnector implements // } @Override - public void onUnregister(final DataCommitHandlerRegistration, DataObject> registration) { + public void onUnregister( + final DataCommitHandlerRegistration, DataObject> registration) { // NOOP for now // FIXME: do registration based on only active commit handlers. } @@ -530,8 +531,7 @@ public class BindingIndependentConnector implements // * */ private class DomToBindingRpcForwardingManager implements - RouteChangeListener>, - RouterInstantiationListener, + RouteChangeListener>, RouterInstantiationListener, GlobalRpcRegistrationListener { private final Map, DomToBindingRpcForwarder> forwarders = new WeakHashMap<>(); @@ -614,7 +614,7 @@ public class BindingIndependentConnector implements // } } catch (Exception e) { - LOG.error("Could not forward Rpcs of type {}", service.getName(),e); + LOG.error("Could not forward Rpcs of type {}", service.getName(), e); } registrations = ImmutableSet.of(); } @@ -625,7 +625,8 @@ public class BindingIndependentConnector implements // * @param service * @param context */ - public DomToBindingRpcForwarder(final Class service, final Class context) { + public DomToBindingRpcForwarder(final Class service, + final Class context) { this.rpcServiceType = new WeakReference>(service); this.supportedRpcs = mappingService.getRpcQNamesFor(service); Builder registrationsBuilder = ImmutableSet @@ -644,8 +645,8 @@ public class BindingIndependentConnector implements // registrations = registrationsBuilder.build(); } - public void registerPaths(final Class context, final Class service, - final Set> set) { + public void registerPaths(final Class context, + final Class service, final Set> set) { QName ctx = BindingReflections.findQName(context); for (org.opendaylight.yangtools.yang.data.api.InstanceIdentifier path : FluentIterable.from(set).transform( toDOMInstanceIdentifier)) { @@ -655,7 +656,6 @@ public class BindingIndependentConnector implements // } } - @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if (EQUALS_METHOD.equals(method)) { @@ -700,7 +700,7 @@ public class BindingIndependentConnector implements // } @Override - public RpcResult invokeRpc(final QName rpc, final CompositeNode domInput) { + public ListenableFuture> invokeRpc(final QName rpc, final CompositeNode domInput) { checkArgument(rpc != null); checkArgument(domInput != null); @@ -709,10 +709,11 @@ public class BindingIndependentConnector implements // RpcService rpcService = baRpcRegistry.getRpcService(rpcType); checkState(rpcService != null); CompositeNode domUnwrappedInput = domInput.getFirstCompositeByName(QName.create(rpc, "input")); + try { - return resolveInvocationStrategy(rpc).invokeOn(rpcService, domUnwrappedInput); + return Futures.immediateFuture(resolveInvocationStrategy(rpc).invokeOn(rpcService, domUnwrappedInput)); } catch (Exception e) { - throw new IllegalStateException(e); + return Futures.immediateFailedFuture(e); } } @@ -745,12 +746,12 @@ public class BindingIndependentConnector implements // strategy = new DefaultInvocationStrategy(rpc, targetMethod, outputClass.get(), inputClass .get()); } else { - strategy = new NoInputNoOutputInvocationStrategy(rpc, targetMethod); + strategy = new NoInputInvocationStrategy(rpc, targetMethod, outputClass.get()); } - } else if(inputClass.isPresent()){ - strategy = new NoOutputInvocationStrategy(rpc,targetMethod, inputClass.get()); + } else if (inputClass.isPresent()) { + strategy = new NoOutputInvocationStrategy(rpc, targetMethod, inputClass.get()); } else { - strategy = new NoInputNoOutputInvocationStrategy(rpc,targetMethod); + strategy = new NoInputNoOutputInvocationStrategy(rpc, targetMethod); } return strategy; } @@ -774,7 +775,8 @@ public class BindingIndependentConnector implements // public abstract RpcResult uncheckedInvoke(RpcService rpcService, CompositeNode domInput) throws Exception; - public RpcResult invokeOn(final RpcService rpcService, final CompositeNode domInput) throws Exception { + public RpcResult invokeOn(final RpcService rpcService, final CompositeNode domInput) + throws Exception { return uncheckedInvoke(rpcService, domInput); } } @@ -797,7 +799,8 @@ public class BindingIndependentConnector implements // @SuppressWarnings("unchecked") @Override - public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) throws Exception { + public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) + throws Exception { DataContainer bindingInput = mappingService.dataObjectFromDataDom(inputClass.get(), domInput); Future> futureResult = (Future>) targetMethod.invoke(rpcService, bindingInput); if (futureResult == null) { @@ -806,28 +809,85 @@ public class BindingIndependentConnector implements // RpcResult bindingResult = futureResult.get(); final Object resultObj = bindingResult.getResult(); if (resultObj instanceof DataObject) { - final CompositeNode output = mappingService.toDataDom((DataObject)resultObj); - return Rpcs.getRpcResult(true, output, Collections.emptySet()); + final CompositeNode output = mappingService.toDataDom((DataObject) resultObj); + return Rpcs.getRpcResult(true, output, Collections. emptySet()); + } + return Rpcs.getRpcResult(true); + } + + @Override + public ListenableFuture> forwardToDomBroker(final DataObject input) { + if (biRpcRegistry == null) { + return Futures.> immediateFuture(Rpcs.getRpcResult(false)); + } + + CompositeNode xml = mappingService.toDataDom(input); + CompositeNode wrappedXml = ImmutableCompositeNode.create(rpc, ImmutableList.> of(xml)); + + return Futures.transform(biRpcRegistry.invokeRpc(rpc, wrappedXml), + new Function, RpcResult>() { + @Override + public RpcResult apply(final RpcResult input) { + Object baResultValue = null; + if (input.getResult() != null) { + baResultValue = mappingService.dataObjectFromDataDom(outputClass.get(), + input.getResult()); + } + return Rpcs.getRpcResult(input.isSuccessful(), baResultValue, input.getErrors()); + } + }); + } + } + + private class NoInputInvocationStrategy extends RpcInvocationStrategy { + + @SuppressWarnings("rawtypes") + private final WeakReference outputClass; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public NoInputInvocationStrategy(final QName rpc, final Method targetMethod, final Class outputClass) { + super(rpc, targetMethod); + this.outputClass = new WeakReference(outputClass); + } + + @SuppressWarnings("unchecked") + @Override + public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) + throws Exception { + Future> futureResult = (Future>) targetMethod.invoke(rpcService); + if (futureResult == null) { + return Rpcs.getRpcResult(false); + } + RpcResult bindingResult = futureResult.get(); + final Object resultObj = bindingResult.getResult(); + if (resultObj instanceof DataObject) { + final CompositeNode output = mappingService.toDataDom((DataObject) resultObj); + return Rpcs.getRpcResult(true, output, Collections. emptySet()); } return Rpcs.getRpcResult(true); } @Override public Future> forwardToDomBroker(final DataObject input) { - if(biRpcRegistry != null) { + if (biRpcRegistry != null) { CompositeNode xml = mappingService.toDataDom(input); CompositeNode wrappedXml = ImmutableCompositeNode.create(rpc, ImmutableList.> of(xml)); - RpcResult result = biRpcRegistry.invokeRpc(rpc, wrappedXml); - Object baResultValue = null; - if (result.getResult() != null) { - baResultValue = mappingService.dataObjectFromDataDom(outputClass.get(), result.getResult()); - } - RpcResult baResult = Rpcs.getRpcResult(result.isSuccessful(), baResultValue, result.getErrors()); - return Futures.> immediateFuture(baResult); + return Futures.transform(biRpcRegistry.invokeRpc(rpc, wrappedXml), + new Function, RpcResult>() { + @Override + public RpcResult apply(final RpcResult input) { + Object baResultValue = null; + if (input.getResult() != null) { + baResultValue = mappingService.dataObjectFromDataDom(outputClass.get(), + input.getResult()); + } + return Rpcs.getRpcResult(input.isSuccessful(), baResultValue, input.getErrors()); + } + }); + } else { + return Futures.> immediateFuture(Rpcs.getRpcResult(false)); } - return Futures.> immediateFuture(Rpcs.getRpcResult(false)); } - } private class NoInputNoOutputInvocationStrategy extends RpcInvocationStrategy { @@ -837,7 +897,8 @@ public class BindingIndependentConnector implements // } @Override - public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) throws Exception { + public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) + throws Exception { @SuppressWarnings("unchecked") Future> result = (Future>) targetMethod.invoke(rpcService); RpcResult bindingResult = result.get(); @@ -852,20 +913,19 @@ public class BindingIndependentConnector implements // private class NoOutputInvocationStrategy extends RpcInvocationStrategy { - @SuppressWarnings("rawtypes") private final WeakReference inputClass; @SuppressWarnings({ "rawtypes", "unchecked" }) public NoOutputInvocationStrategy(final QName rpc, final Method targetMethod, final Class inputClass) { - super(rpc,targetMethod); + super(rpc, targetMethod); this.inputClass = new WeakReference(inputClass); } - @Override - public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) throws Exception { + public RpcResult uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) + throws Exception { DataContainer bindingInput = mappingService.dataObjectFromDataDom(inputClass.get(), domInput); Future> result = (Future>) targetMethod.invoke(rpcService, bindingInput); if (result == null) { @@ -876,18 +936,22 @@ public class BindingIndependentConnector implements // } @Override - public Future> forwardToDomBroker(final DataObject input) { - if(biRpcRegistry != null) { - CompositeNode xml = mappingService.toDataDom(input); - CompositeNode wrappedXml = ImmutableCompositeNode.create(rpc,ImmutableList.>of(xml)); - RpcResult result = biRpcRegistry.invokeRpc(rpc, wrappedXml); - Object baResultValue = null; - RpcResult baResult = Rpcs.getRpcResult(result.isSuccessful(), null, result.getErrors()); - return Futures.>immediateFuture(baResult); + public ListenableFuture> forwardToDomBroker(final DataObject input) { + if (biRpcRegistry == null) { + return Futures.> immediateFuture(Rpcs.getRpcResult(false)); } - return Futures.>immediateFuture(Rpcs.getRpcResult(false)); - } + CompositeNode xml = mappingService.toDataDom(input); + CompositeNode wrappedXml = ImmutableCompositeNode.create(rpc, ImmutableList.> of(xml)); + + return Futures.transform(biRpcRegistry.invokeRpc(rpc, wrappedXml), + new Function, RpcResult>() { + @Override + public RpcResult apply(final RpcResult input) { + return Rpcs. getRpcResult(input.isSuccessful(), null, input.getErrors()); + } + }); + } } public boolean isRpcForwarding() {