From: lubos-cicut Date: Tue, 12 Sep 2023 11:45:52 +0000 (+0200) Subject: Do not use RpcService in srm-impl components X-Git-Tag: release/calcium~11 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=ded56e87b38385afc4c1bd24ed262f2009323b2c;p=serviceutils.git Do not use RpcService in srm-impl components Ditch the dependency on OdlSrmRpcsService and define a more natural interface, RegistryControl, for use with the shell. This avoids some amount of boiler plate, as we do not have to deal with the entire RPC dispatch stack. While we are here, also clean up pom.xmls to eliminate dependency warnings. JIRA: SRVUTILS-10 Change-Id: I218d7b6681270ea72fbc103f15199431a64fe50e Signed-off-by: lubos-cicut Signed-off-by: Robert Varga --- diff --git a/srm/api/src/main/java/org/opendaylight/serviceutils/srm/spi/RegistryControl.java b/srm/api/src/main/java/org/opendaylight/serviceutils/srm/spi/RegistryControl.java new file mode 100644 index 00000000..ac19d637 --- /dev/null +++ b/srm/api/src/main/java/org/opendaylight/serviceutils/srm/spi/RegistryControl.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.serviceutils.srm.spi; + +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput; + +/** + * Control interface for ServiceRecoveryRegistry implementation. + */ +public interface RegistryControl { + + RecoverOutput recover(RecoverInput input); + + ReinstallOutput reinstall(ReinstallInput input); +} diff --git a/srm/impl/pom.xml b/srm/impl/pom.xml index e660372f..d0d0ceb0 100644 --- a/srm/impl/pom.xml +++ b/srm/impl/pom.xml @@ -39,10 +39,26 @@ and is available at http://www.eclipse.org/legal/epl-v10.html tools-api ${project.version} + + com.google.guava + guava + org.opendaylight.mdsal mdsal-binding-api + + org.opendaylight.mdsal + mdsal-common-api + + + org.opendaylight.mdsal + yang-binding + + + org.opendaylight.yangtools + concepts + com.guicedee.services javax.inject diff --git a/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcUtils.java b/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/RegistryControlImpl.java similarity index 84% rename from srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcUtils.java rename to srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/RegistryControlImpl.java index 7d2768b0..983db718 100644 --- a/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcUtils.java +++ b/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/RegistryControlImpl.java @@ -7,19 +7,30 @@ */ package org.opendaylight.serviceutils.srm.impl; +import static java.util.Objects.requireNonNull; + +import com.google.common.collect.ImmutableClassToInstanceMap; import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.Futures; import java.util.concurrent.ExecutionException; +import javax.annotation.PreDestroy; +import javax.inject.Singleton; import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.binding.api.WriteTransaction; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; +import org.opendaylight.serviceutils.srm.spi.RegistryControl; +import org.opendaylight.serviceutils.tools.rpc.FutureRpcResults; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.ops.rev180626.ServiceOps; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.ops.rev180626.service.ops.Services; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.ops.rev180626.service.ops.ServicesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.ops.rev180626.service.ops.services.Operations; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.ops.rev180626.service.ops.services.OperationsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.Recover; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.Reinstall; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutputBuilder; @@ -53,22 +64,23 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev1 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.Ofplugin; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.ServiceOpRecover; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.ServiceOpReinstall; +import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.Rpc; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * The Utility class for SRM Shell. - */ -public final class SrmRpcUtils { - private static final Logger LOG = LoggerFactory.getLogger(SrmRpcUtils.class); +@Singleton +@Component(immediate = true, service = RegistryControl.class) +public final class RegistryControlImpl implements RegistryControl, AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(RegistryControlImpl.class); private static final Boolean REINSTALL_FAILED = Boolean.FALSE; private static final Boolean REINSTALL_SUCCESS = Boolean.TRUE; - private SrmRpcUtils() { - // Hidden on purpose - } - private static final ImmutableMap NAME_TO_TYPE_MAP = ImmutableMap.builder() .put(GeniusItm.VALUE, EntityTypeService.VALUE) @@ -115,8 +127,29 @@ public final class SrmRpcUtils { .put(NetvirtQosPolicyInstance.VALUE, NetvirtQos.VALUE) .build(); + private final DataBroker dataBroker; + private final Registration reg; + + @Activate + public RegistryControlImpl(@Reference DataBroker dataBroker, @Reference RpcProviderService rpcProvider) { + this.dataBroker = requireNonNull(dataBroker); + reg = rpcProvider.registerRpcImplementations(ImmutableClassToInstanceMap.>builder() + .put(Recover.class, input -> FutureRpcResults.fromListenableFuture(LOG, "recover", input, + () -> Futures.immediateFuture(recover(input))).build()) + .put(Reinstall.class, input -> FutureRpcResults.fromListenableFuture(LOG, "reinstall", input, + () -> Futures.immediateFuture(reinstall(input))).build()) + .build()); + } + + @Override + @Deactivate + @PreDestroy + public void close() { + reg.close(); + } - public static RecoverOutput callSrmOp(DataBroker broker, RecoverInput input) { + @Override + public RecoverOutput recover(RecoverInput input) { RecoverOutputBuilder outputBuilder = new RecoverOutputBuilder(); if (input.getEntityName() == null) { outputBuilder.setResponse(RpcFailEntityName.VALUE) @@ -159,9 +192,10 @@ public final class SrmRpcUtils { } Operations operation = opsBuilder.build(); InstanceIdentifier opsIid = getInstanceIdentifier(operation, serviceName); - WriteTransaction tx = broker.newWriteOnlyTransaction(); + WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); tx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, opsIid, operation); try { + // FIXME: we are in RPC invocation path, we should not be blocking here tx.commit().get(); } catch (InterruptedException | ExecutionException e) { LOG.error("Error writing RecoveryOp to datastore. path:{}, data:{}", opsIid, operation); @@ -172,7 +206,8 @@ public final class SrmRpcUtils { return outputBuilder.build(); } - public static ReinstallOutput callSrmOp(DataBroker broker, ReinstallInput input) { + @Override + public ReinstallOutput reinstall(ReinstallInput input) { ReinstallOutputBuilder outputBuilder = new ReinstallOutputBuilder(); if (input.getEntityName() == null) { outputBuilder.setSuccessful(REINSTALL_FAILED) @@ -213,9 +248,10 @@ public final class SrmRpcUtils { .setTriggerOperation(ServiceOpReinstall.VALUE); Operations operation = opsBuilder.build(); InstanceIdentifier opsIid = getInstanceIdentifier(operation, serviceName); - WriteTransaction tx = broker.newWriteOnlyTransaction(); + WriteTransaction tx = dataBroker.newWriteOnlyTransaction(); tx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, opsIid, operation); try { + // FIXME: we are in RPC invocation path, we should not be blocking here tx.commit().get(); } catch (InterruptedException | ExecutionException e) { LOG.error("Error writing RecoveryOp to datastore. path:{}, data:{}", opsIid, operation); diff --git a/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcProvider.java b/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcProvider.java deleted file mode 100644 index 0f0bc178..00000000 --- a/srm/impl/src/main/java/org/opendaylight/serviceutils/srm/impl/SrmRpcProvider.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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.serviceutils.srm.impl; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; -import javax.annotation.PreDestroy; -import javax.inject.Inject; -import javax.inject.Singleton; -import org.opendaylight.mdsal.binding.api.DataBroker; -import org.opendaylight.mdsal.binding.api.RpcProviderService; -import org.opendaylight.serviceutils.tools.rpc.FutureRpcResults; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.OdlSrmRpcsService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput; -import org.opendaylight.yangtools.concepts.Registration; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Deactivate; -import org.osgi.service.component.annotations.Reference; -import org.osgi.service.component.annotations.RequireServiceComponentRuntime; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Singleton -@Component(immediate = true, service = OdlSrmRpcsService.class) -@RequireServiceComponentRuntime -public final class SrmRpcProvider implements OdlSrmRpcsService, AutoCloseable { - private static final Logger LOG = LoggerFactory.getLogger(SrmRpcProvider.class); - - private final DataBroker dataBroker; - private final Registration reg; - - @Inject - @Activate - public SrmRpcProvider(@Reference DataBroker dataBroker, @Reference RpcProviderService rpcProvider) { - this.dataBroker = dataBroker; - reg = rpcProvider.registerRpcImplementation(OdlSrmRpcsService.class, this); - } - - @Override - @Deactivate - @PreDestroy - public void close() { - reg.close(); - } - - @Override - public ListenableFuture> recover(RecoverInput input) { - return FutureRpcResults.fromListenableFuture(LOG, "recover", input, - () -> Futures.immediateFuture(SrmRpcUtils.callSrmOp(dataBroker, input))).build(); - } - - @Override - public ListenableFuture> reinstall(ReinstallInput input) { - return FutureRpcResults.fromListenableFuture(LOG, "reinstall", input, - () -> Futures.immediateFuture(SrmRpcUtils.callSrmOp(dataBroker, input))).build(); - } -} diff --git a/srm/shell/pom.xml b/srm/shell/pom.xml index 0f8deac7..f97801f7 100644 --- a/srm/shell/pom.xml +++ b/srm/shell/pom.xml @@ -33,29 +33,25 @@ and is available at http://www.eclipse.org/legal/epl-v10.html provided - org.eclipse.jdt - org.eclipse.jdt.annotation + org.eclipse.jdt + org.eclipse.jdt.annotation - org.opendaylight.mdsal - mdsal-binding-api + org.opendaylight.mdsal + mdsal-binding-api - org.opendaylight.mdsal - mdsal-common-api + org.opendaylight.mdsal + mdsal-common-api - org.opendaylight.mdsal - yang-binding + org.opendaylight.mdsal + yang-binding org.opendaylight.serviceutils srm-api - - org.opendaylight.yangtools - yang-common - diff --git a/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/RecoverCommand.java b/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/RecoverCommand.java index 92ee7275..84ad9263 100644 --- a/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/RecoverCommand.java +++ b/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/RecoverCommand.java @@ -7,20 +7,14 @@ */ package org.opendaylight.serviceutils.srm.shell; -import java.util.concurrent.Future; import org.apache.karaf.shell.api.action.Action; import org.apache.karaf.shell.api.action.Argument; import org.apache.karaf.shell.api.action.Command; import org.apache.karaf.shell.api.action.lifecycle.Reference; import org.apache.karaf.shell.api.action.lifecycle.Service; -import org.eclipse.jdt.annotation.Nullable; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.OdlSrmRpcsService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverInput; +import org.opendaylight.serviceutils.srm.spi.RegistryControl; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RecoverOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityNameBase; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeBase; -import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.RpcSuccess; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,57 +33,44 @@ public class RecoverCommand implements Action { @Argument(index = 2, name = "id", description = "EntityId, optional", required = false, multiValued = false) private String id; @Reference - private OdlSrmRpcsService srmRpcService; + private RegistryControl control; @Override - public @Nullable Object execute() throws Exception { - RecoverInput input = getInput(); - if (input == null || srmRpcService == null) { - // We've already shown the relevant error msg - return null; - } - Future> result = srmRpcService.recover(input); - RpcResult recoverResult = result.get(); - printResult(recoverResult); - return null; - } - - @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private static void printResult(RpcResult recoverResult) { - var sb = new StringBuilder(""); - if (recoverResult.isSuccessful()) { - sb.append("RPC call to recover was successful"); - LOG.trace("RPC Result: {}", recoverResult.getResult()); - } else { - sb.append("RPC Call to recover failed.\n") - .append("ErrorCode: ").append(recoverResult.getResult().getResponse()) - .append("ErrorMsg: ").append(recoverResult.getResult().getMessage()); - LOG.trace("RPC Result: {}", recoverResult.getResult()); - } - System.out.println(sb.toString()); - } - @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private @Nullable RecoverInput getInput() { + public Object execute() { if (type == null || name == null) { return null; } - EntityTypeBase entityType = SrmCliUtils.getEntityType(type); + var entityType = SrmCliUtils.getEntityType(type); if (entityType == null) { System.out.println(SrmCliUtils.getTypeHelp()); return null; } - EntityNameBase entityName = SrmCliUtils.getEntityName(entityType, name); + var entityName = SrmCliUtils.getEntityName(entityType, name); if (entityName == null) { System.out.println(SrmCliUtils.getNameHelp(entityType)); return null; } - var inputBuilder = new RecoverInputBuilder() - .setEntityType(entityType) - .setEntityName(entityName); - if (id != null) { - inputBuilder.setEntityId(id); + + if (control != null) { + var inputBuilder = new RecoverInputBuilder() + .setEntityType(entityType) + .setEntityName(entityName); + if (id != null) { + inputBuilder.setEntityId(id); + } + var output = control.recover(inputBuilder.build()); + + LOG.trace("RPC Result: {}", output); + var response = output.getResponse(); + if (RpcSuccess.VALUE.equals(response)) { + System.out.println("RPC call to recover was successful"); + } else { + System.out.println("RPC call to recover failed."); + System.out.println("ErrorCode: " + response); + System.out.println("ErrorMsg: " + output.getMessage()); + } } - return inputBuilder.build(); + return null; } } diff --git a/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/ReinstallCommand.java b/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/ReinstallCommand.java index a8280b82..68de0e84 100644 --- a/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/ReinstallCommand.java +++ b/srm/shell/src/main/java/org/opendaylight/serviceutils/srm/shell/ReinstallCommand.java @@ -7,21 +7,15 @@ */ package org.opendaylight.serviceutils.srm.shell; -import java.util.concurrent.Future; import org.apache.karaf.shell.api.action.Action; import org.apache.karaf.shell.api.action.Argument; import org.apache.karaf.shell.api.action.Command; import org.apache.karaf.shell.api.action.lifecycle.Reference; import org.apache.karaf.shell.api.action.lifecycle.Service; -import org.eclipse.jdt.annotation.Nullable; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.OdlSrmRpcsService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInput; +import org.opendaylight.serviceutils.srm.spi.RegistryControl; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.rpc.rev180626.ReinstallOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityNameBase; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeBase; import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.EntityTypeService; -import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,44 +32,31 @@ public class ReinstallCommand implements Action { required = false, multiValued = false) private String name; @Reference - private OdlSrmRpcsService srmRpcService; + private RegistryControl control; @Override - public @Nullable Object execute() throws Exception { - ReinstallInput input = getInput(); - if (input == null || srmRpcService == null) { - return null; - } - Future> result = srmRpcService.reinstall(input); - RpcResult reinstallResult = result.get(); - printResult(reinstallResult); - return null; - } - @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private static void printResult(RpcResult reinstallResult) { - var sb = new StringBuilder(""); - if (reinstallResult.isSuccessful()) { - sb.append("RPC call to reinstall was successful"); - LOG.trace("RPC Result: {}", reinstallResult.getResult()); - } else { - sb.append("RPC Call to reinstall failed.\n") - .append("ErrorMsg: ").append(reinstallResult.getResult().getMessage()); - LOG.trace("RPC Result: {}", reinstallResult.getResult()); - } - System.out.println(sb.toString()); - } - - @SuppressWarnings("checkstyle:RegexpSinglelineJava") - private @Nullable ReinstallInput getInput() { - EntityNameBase entityName = SrmCliUtils.getEntityName(ENTITY_TYPE, name); + public Object execute() { + var entityName = SrmCliUtils.getEntityName(ENTITY_TYPE, name); if (entityName == null) { System.out.println(SrmCliUtils.getNameHelp(ENTITY_TYPE)); return null; } - return new ReinstallInputBuilder() - .setEntityType(ENTITY_TYPE) - .setEntityName(entityName) - .build(); + + if (control != null) { + var output = control.reinstall(new ReinstallInputBuilder() + .setEntityType(ENTITY_TYPE) + .setEntityName(entityName) + .build()); + + LOG.trace("RPC Result: {}", output); + if (Boolean.TRUE.equals(output.getSuccessful())) { + System.out.println("RPC call to reinstall was successful"); + } else { + System.out.println("RPC Call to reinstall failed."); + System.out.println("ErrorMsg: " + output.getMessage()); + } + } + return null; } }