From 55e8b2261d7e1183869483cb29582c37c83bfb7e Mon Sep 17 00:00:00 2001 From: Ruslan Kashapov Date: Wed, 25 Jan 2023 11:29:31 +0200 Subject: [PATCH] Remove blueprint XML from mapping service implementation Convert it to OSGi DS Change-Id: I4241b55118aee648e410bece97df8f67373befd1 Signed-off-by: Ruslan Kashapov --- mappingservice/implementation/pom.xml | 15 +++++ .../implementation/LispMappingService.java | 66 ++++++++++++------- .../implementation/MappingService.java | 39 +++++++---- .../implementation/MappingServiceShell.java | 11 +++- .../OSGI-INF/blueprint/mappingservice.xml | 58 ---------------- .../LispMappingServiceTest.java | 20 +++++- .../lisp/MappingServiceTest.java | 8 ++- 7 files changed, 116 insertions(+), 101 deletions(-) delete mode 100644 mappingservice/implementation/src/main/resources/OSGI-INF/blueprint/mappingservice.xml diff --git a/mappingservice/implementation/pom.xml b/mappingservice/implementation/pom.xml index 71721286d..97901f1aa 100644 --- a/mappingservice/implementation/pom.xml +++ b/mappingservice/implementation/pom.xml @@ -70,6 +70,21 @@ mappingservice.inmemorydb test + + + com.guicedee.services + javax.inject + true + + + jakarta.annotation + jakarta.annotation-api + true + + + org.osgi + org.osgi.service.component.annotations + org.opendaylight.mdsal diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java index b8ba330f0..bec9825fe 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java @@ -11,6 +11,9 @@ package org.opendaylight.lispflowmapping.implementation; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import java.util.List; +import javax.annotation.PreDestroy; +import javax.inject.Inject; +import javax.inject.Singleton; import org.apache.commons.lang3.tuple.MutablePair; import org.apache.commons.lang3.tuple.Pair; import org.opendaylight.lispflowmapping.config.ConfigIni; @@ -27,6 +30,7 @@ import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServic import org.opendaylight.lispflowmapping.lisp.type.LispMessage; import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier; import org.opendaylight.mdsal.binding.api.NotificationService; +import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier; @@ -57,18 +61,24 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLi import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder; +import org.opendaylight.yangtools.concepts.Registration; +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; +@Singleton +@Component(service = {IFlowMapping.class, IMapRequestResultHandler.class, IMapNotifyHandler.class}, + immediate = true, property = "type=default") public class LispMappingService implements IFlowMapping, IMapRequestResultHandler, IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService { private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping"; private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create( LISPFLOWMAPPING_ENTITY_NAME); - private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class); - private final ClusterSingletonServiceProvider clusterSingletonService; private volatile boolean smr = ConfigIni.getInstance().smrIsSet(); private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy(); @@ -77,22 +87,39 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle private ThreadLocal>> tlsMapNotify = new ThreadLocal<>(); private ThreadLocal> tlsMapRequest = new ThreadLocal<>(); - private final OdlLispSbService lispSB; private IMapResolverAsync mapResolver; private IMapServerAsync mapServer; private final IMappingService mapService; + private final OdlLispSbService lispSB; + private final ClusterSingletonServiceProvider clusterSingletonService; + private final RpcProviderService rpcProviderService; private final NotificationService notificationService; - - public LispMappingService(final NotificationService notificationService, - final IMappingService mappingService, - final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) { - - this.notificationService = notificationService; + private final Registration rpcRegistration; + private final Registration listenerRegistration; + + @Inject + @Activate + public LispMappingService(@Reference final IMappingService mappingService, + @Reference final OdlLispSbService odlLispService, + @Reference final ClusterSingletonServiceProvider clusterSingletonService, + @Reference final RpcProviderService rpcProviderService, + @Reference final NotificationService notificationService) { this.mapService = mappingService; this.lispSB = odlLispService; this.clusterSingletonService = clusterSingletonService; - LOG.debug("LispMappingService Module constructed!"); + this.rpcProviderService = rpcProviderService; + this.notificationService = notificationService; + + // initialize + listenerRegistration = notificationService.registerNotificationListener(this); + rpcRegistration = rpcProviderService.registerRpcImplementation(OdlLispSbService.class, lispSB); + + mapResolver = new MapResolver(mapService, smr, elpPolicy, this); + mapServer = new MapServer(mapService, smr, this, notificationService); + clusterSingletonService.registerClusterSingletonService(this); + mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer); + LOG.info("LISP (RFC6830) Mapping Service initialized"); } public boolean shouldUseSmr() { @@ -115,20 +142,6 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle return this.notificationService; } - public void initialize() { - mapResolver = new MapResolver(mapService, smr, elpPolicy, this); - mapServer = new MapServer(mapService, smr, this, notificationService); - this.clusterSingletonService.registerClusterSingletonService(this); - mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer); - LOG.info("LISP (RFC6830) Mapping Service init finished"); - } - - public void basicInit() { - mapResolver = new MapResolver(mapService, smr, elpPolicy, this); - mapServer = new MapServer(mapService, smr, this, notificationService); - mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer); - } - @Override public MapReply handleMapRequest(MapRequest request) { if (LOG.isDebugEnabled()) { @@ -283,10 +296,14 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle mapServer = null; } + @Deactivate + @PreDestroy @Override public void close() throws Exception { destroy(); clusterSingletonService.close(); + rpcRegistration.close(); + listenerRegistration.close(); } @Override @@ -307,3 +324,4 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle return SERVICE_GROUP_IDENTIFIER; } } + diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java index c4cf63f57..25f6f1301 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingService.java @@ -13,6 +13,8 @@ import com.google.common.util.concurrent.ListenableFuture; import java.util.ArrayList; import java.util.List; import java.util.Set; +import javax.annotation.PreDestroy; +import javax.inject.Singleton; import org.opendaylight.lispflowmapping.config.ConfigIni; import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd; import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener; @@ -26,6 +28,7 @@ import org.opendaylight.lispflowmapping.lisp.type.MappingData; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid; @@ -90,10 +93,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsOutput; +import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.common.ErrorTag; import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +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; @@ -108,6 +116,8 @@ import org.slf4j.LoggerFactory; * @author Florin Coras * */ +@Component(service = IMappingService.class, immediate = true, property = "type=default") +@Singleton public class MappingService implements OdlMappingserviceService, IMappingService, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(MappingService.class); @@ -115,27 +125,22 @@ public class MappingService implements OdlMappingserviceService, IMappingService private DataStoreBackEnd dsbe; private AuthenticationKeyDataListener keyListener; private MappingDataListener mappingListener; - private final ILispDAO dao; - private final DataBroker dataBroker; - private final NotificationPublishService notificationPublishService; + @Reference + private volatile ILispDAO dao; + @Reference + private volatile DataBroker dataBroker; + @Reference + private volatile RpcProviderService rpcProviderService; + @Reference + private volatile NotificationPublishService notificationPublishService; + private Registration rpcRegistration; private boolean mappingMergePolicy = ConfigIni.getInstance().mappingMergeIsSet(); private final boolean notificationPolicy = ConfigIni.getInstance().smrIsSet(); private final boolean iterateMask = true; private boolean isMaster = false; - public MappingService(final DataBroker broker, - final NotificationPublishService notificationPublishService, - final ILispDAO lispDAO) { - this.dataBroker = broker; - this.notificationPublishService = notificationPublishService; - this.dao = lispDAO; - - LOG.debug("MappingService created!"); - } - - @Override public void setMappingMerge(boolean mergeMapping) { this.mappingMergePolicy = mergeMapping; @@ -150,10 +155,13 @@ public class MappingService implements OdlMappingserviceService, IMappingService ConfigIni.getInstance().setLookupPolicy(policy); } + @Activate public void initialize() { LOG.info("Mapping Service initializing..."); dsbe = new DataStoreBackEnd(dataBroker); + rpcRegistration = rpcProviderService.registerRpcImplementation(OdlMappingserviceService.class, this); + mappingSystem = new MappingSystem(dao, iterateMask, notificationPublishService, mappingMergePolicy); mappingSystem.setDataStoreBackEnd(dsbe); mappingSystem.initialize(); @@ -544,9 +552,12 @@ public class MappingService implements OdlMappingserviceService, IMappingService return mappingSystem.prettyPrintKeys(); } + @PreDestroy + @Deactivate @Override public void close() throws Exception { LOG.info("Mapping Service is being destroyed!"); + rpcRegistration.close(); keyListener.closeDataChangeListener(); mappingListener.closeDataChangeListener(); mappingSystem.destroy(); diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingServiceShell.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingServiceShell.java index 15d680b71..6ac6e21df 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingServiceShell.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/MappingServiceShell.java @@ -7,6 +7,8 @@ */ package org.opendaylight.lispflowmapping.implementation; +import javax.inject.Inject; +import javax.inject.Singleton; import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService; import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServiceShell; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; @@ -16,6 +18,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ei import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder; import org.opendaylight.yangtools.yang.common.Uint16; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,12 +30,16 @@ import org.slf4j.LoggerFactory; * @author Lorand Jakab * */ +@Singleton +@Component(service = IMappingServiceShell.class, immediate = true, property = "type=default") public class MappingServiceShell implements IMappingServiceShell { protected static final Logger LOG = LoggerFactory.getLogger(MappingServiceShell.class); private final IMappingService mappingService; - public MappingServiceShell(final IMappingService mappingService) { + @Inject + @Activate + public MappingServiceShell(final @Reference IMappingService mappingService) { this.mappingService = mappingService; } diff --git a/mappingservice/implementation/src/main/resources/OSGI-INF/blueprint/mappingservice.xml b/mappingservice/implementation/src/main/resources/OSGI-INF/blueprint/mappingservice.xml deleted file mode 100644 index 22726bcfe..000000000 --- a/mappingservice/implementation/src/main/resources/OSGI-INF/blueprint/mappingservice.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java index c7c5cd7c3..021da330b 100644 --- a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java +++ b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java @@ -30,6 +30,7 @@ import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingServic import org.opendaylight.lispflowmapping.lisp.type.LispMessage; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; import org.opendaylight.mdsal.binding.api.NotificationService; +import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary; @@ -65,6 +66,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendM import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder; +import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.util.BindingMap; import org.opendaylight.yangtools.yang.common.Uint16; @@ -76,8 +78,11 @@ public class LispMappingServiceTest { @Mock(name = "tlsMapReply") private static ThreadLocal tlsMapReplyMock; @Mock(name = "tlsMapNotify") private static ThreadLocal>> tlsMapNotifyMock; @Mock(name = "tlsMapRequest") private static ThreadLocal> tlsMapRequestMock; + @Mock private static Registration rpcRegistration; + @Mock(name = "listenerRegistration") private static Registration listenerRegistration; private final NotificationService notificationService = Mockito.mock(NotificationService.class); + private final RpcProviderService rpcProviderService = Mockito.mock(RpcProviderService.class); private final IMappingService mappingService = Mockito.mock(IMappingService.class); private final OdlLispSbService odlLispSbService = Mockito.mock(OdlLispSbService.class); private final ClusterSingletonServiceProvider clusterSingletonService = Mockito.mock( @@ -85,7 +90,7 @@ public class LispMappingServiceTest { @InjectMocks private final LispMappingService lispMappingService = new LispMappingService( - notificationService, mappingService, odlLispSbService, clusterSingletonService); + mappingService, odlLispSbService, clusterSingletonService, rpcProviderService, notificationService); private static final byte[] IPV4_BYTES_1 = new byte[] {1, 2, 3, 0}; private static final byte[] IPV4_BYTES_2 = new byte[] {1, 2, 4, 0}; @@ -339,8 +344,12 @@ public class LispMappingServiceTest { */ @Test public void closeTest() throws Exception { + setMock("rpcRegistration", rpcRegistration); + setMock("listenerRegistration", listenerRegistration); lispMappingService.close(); - + Mockito.verify(rpcRegistration).close(); + Mockito.verify(listenerRegistration).close(); + Mockito.verify(clusterSingletonService).close(); assertNull(getField("mapResolver")); assertNull(getField("mapServer")); } @@ -362,6 +371,13 @@ public class LispMappingServiceTest { lispMappingService.onXtrReplyMapping(Mockito.mock(XtrReplyMapping.class)); } + private void setMock(final String fieldName, final Object value) + throws IllegalAccessException, NoSuchFieldException { + final Field field = LispMappingService.class.getDeclaredField(fieldName); + field.setAccessible(true); + field.set(lispMappingService, value); + } + @SuppressWarnings("unchecked") private T getField(String fieldName) throws NoSuchFieldException, IllegalAccessException { final Field field = LispMappingService.class.getDeclaredField(fieldName); diff --git a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java index af644bd2a..61e08edb1 100644 --- a/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java +++ b/mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MappingServiceTest.java @@ -32,6 +32,7 @@ import org.opendaylight.lispflowmapping.lisp.type.MappingData; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId; @@ -83,6 +84,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput; +import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.common.ErrorTag; import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.RpcError; @@ -97,15 +99,16 @@ public class MappingServiceTest { @Mock(name = "dsbe") private static DataStoreBackEnd dsbe; @Mock(name = "keyListener") private static AuthenticationKeyDataListener keyListener; @Mock(name = "mappingListener") private static MappingDataListener mappingListener; + @Mock(name = "rpcRegistration") private static Registration rpcRegistration; private final DataBroker dataBroker = Mockito.mock(DataBroker.class); + private final RpcProviderService rpcProviderService = Mockito.mock(RpcProviderService.class); private final NotificationPublishService notificationPublishService = Mockito .mock(NotificationPublishService.class); private final ILispDAO lispDAO = Mockito.mock(ILispDAO.class); @InjectMocks - MappingService mappingService = new MappingService(dataBroker, - notificationPublishService, lispDAO); + MappingService mappingService = new MappingService(); private static final String IPV4_STRING = "1.2.3.0"; private static final Eid IPV4_EID = LispAddressUtil.asIpv4Eid(IPV4_STRING); @@ -520,6 +523,7 @@ public class MappingServiceTest { @Test public void close() throws Exception { mappingService.close(); + Mockito.verify(rpcRegistration).close(); Mockito.verify(keyListener).closeDataChangeListener(); Mockito.verify(mappingListener).closeDataChangeListener(); } -- 2.36.6