diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/idevent/IdChangeEventHandlerServiceImpl.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/idevent/IdChangeEventHandlerServiceImpl.java index 1eaa5aad8af..c91749d4ccd 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/idevent/IdChangeEventHandlerServiceImpl.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/impl/idevent/IdChangeEventHandlerServiceImpl.java @@ -1,7 +1,5 @@ package io.mosip.authentication.common.service.impl.idevent; -import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.REMOVE_ID_STATUS_TOPIC; - import java.time.LocalDateTime; import java.util.Map; import java.util.Optional; @@ -9,14 +7,13 @@ import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import io.mosip.authentication.common.service.entity.IdentityEntity; import io.mosip.authentication.common.service.helper.AuditHelper; -import io.mosip.authentication.common.service.helper.WebSubHelper; import io.mosip.authentication.common.service.repository.IdentityCacheRepository; import io.mosip.authentication.common.service.spi.idevent.CredentialStoreService; +import io.mosip.authentication.common.service.websub.impl.RemoveIdStatusEventPublisher; import io.mosip.authentication.core.constant.AuditEvents; import io.mosip.authentication.core.constant.AuditModules; import io.mosip.authentication.core.constant.IdAuthCommonConstants; @@ -80,13 +77,8 @@ static interface ConsumerWithBusinessException { @Autowired private CredentialStoreService credStorService; - /** The web sub event publish helper. */ @Autowired - private WebSubHelper webSubHelper; - - /** The remove id status topic. */ - @Value("${" + REMOVE_ID_STATUS_TOPIC + "}") - private String removeIdStatusTopic; + private RemoveIdStatusEventPublisher removeIdStatusEventPublisher; /* (non-Javadoc) * @see io.mosip.authentication.core.spi.idevent.service.IdChangeEventHandlerService#handleIdEvent(java.util.List) @@ -197,17 +189,9 @@ private void handleRemoveId(EventModel eventModel) throws IdAuthenticationBusine String idHash = (String) additionalData.get(ID_HASH); if (idHash != null && !idHash.isEmpty()) { identityCacheRepo.deleteById(idHash); - publishRemoveIdStatusEvent(idHash); + removeIdStatusEventPublisher.publishRemoveIdStatusEvent(idHash); } } - - public void publishRemoveIdStatusEvent(String idHash) { - RemoveIdStatusEvent removeIdStatusEvent = new RemoveIdStatusEvent(); - removeIdStatusEvent.setData(Map.of(ID_HASH, idHash)); - removeIdStatusEvent.setTimestamp(DateUtils.formatToISOString(DateUtils.getUTCCurrentDateTime())); - webSubHelper.publishEvent(removeIdStatusTopic, - webSubHelper.createEventModel(removeIdStatusTopic, removeIdStatusEvent)); - } private void handleDeactivateId(EventModel eventModel) throws IdAuthenticationBusinessException { updateIdentityMetadata(eventModel); diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/websub/impl/RemoveIdStatusEventPublisher.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/websub/impl/RemoveIdStatusEventPublisher.java new file mode 100644 index 00000000000..48e7c2ab1ed --- /dev/null +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/websub/impl/RemoveIdStatusEventPublisher.java @@ -0,0 +1,92 @@ +package io.mosip.authentication.common.service.websub.impl; + +import static io.mosip.authentication.core.constant.IdAuthConfigKeyConstants.REMOVE_ID_STATUS_TOPIC; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import io.mosip.authentication.common.service.helper.WebSubHelper; +import io.mosip.authentication.common.service.impl.idevent.RemoveIdStatusEvent; +import io.mosip.authentication.core.constant.IdAuthCommonConstants; +import io.mosip.authentication.core.logger.IdaLogger; +import io.mosip.kernel.core.logger.spi.Logger; +import io.mosip.kernel.core.util.DateUtils; + +/** + * The Class RemoveIdStatusEventPublisher. + * + * @author Ritik Jain + */ +@Component +public class RemoveIdStatusEventPublisher extends BaseWebSubEventsInitializer { + + /** The Constant logger. */ + private static final Logger logger = IdaLogger.getLogger(RemoveIdStatusEventPublisher.class); + + /** The remove id status topic. */ + @Value("${" + REMOVE_ID_STATUS_TOPIC + "}") + private String removeIdStatusTopic; + + /** The web sub event publish helper. */ + @Autowired + private WebSubHelper webSubHelper; + + private static final String ID_HASH = "id_hash"; + + /** + * Do subscribe. + */ + @Override + protected void doSubscribe() { + // Nothing to do here since we are just publishing event for this topic. + } + + /** + * Try register topic remove id status event. + */ + private void tryRegisterTopic() { + try { + logger.debug(IdAuthCommonConstants.SESSION_ID, "tryRegisterTopic", "", + "Trying to register topic: " + removeIdStatusTopic); + webSubHelper.registerTopic(removeIdStatusTopic); + logger.info(IdAuthCommonConstants.SESSION_ID, "tryRegisterTopic", "", + "Registered topic: " + removeIdStatusTopic); + } catch (Exception e) { + logger.info(IdAuthCommonConstants.SESSION_ID, "tryRegisterTopic", e.getClass().toString(), + "Error registering topic: " + removeIdStatusTopic + "\n" + e.getMessage()); + } + } + + @Override + protected void doRegister() { + logger.info(IdAuthCommonConstants.SESSION_ID, "doRegister", this.getClass().getSimpleName(), + "Registering topic.."); + tryRegisterTopic(); + } + + public void publishRemoveIdStatusEvent(String idHash) { + RemoveIdStatusEvent removeIdStatusEvent = createRemoveIdStatusEvent(idHash); + webSubHelper.publishEvent(removeIdStatusTopic, + webSubHelper.createEventModel(removeIdStatusTopic, removeIdStatusEvent)); + } + + /** + * Creates the remove id status event. + * + * @param idHash the idHash + * @return the remove id status event + */ + private RemoveIdStatusEvent createRemoveIdStatusEvent(String idHash) { + RemoveIdStatusEvent removeIdStatusEvent = new RemoveIdStatusEvent(); + Map dataMap = new HashMap(); + dataMap.put(ID_HASH, idHash); + removeIdStatusEvent.setData(dataMap); + removeIdStatusEvent.setTimestamp(DateUtils.formatToISOString(DateUtils.getUTCCurrentDateTime())); + return removeIdStatusEvent; + } + +} diff --git a/authentication/authentication-internal-service/src/main/java/io/mosip/authentication/internal/service/listener/InternalAuthWebSubInitializer.java b/authentication/authentication-internal-service/src/main/java/io/mosip/authentication/internal/service/listener/InternalAuthWebSubInitializer.java index 2449ce885eb..3a1e8d7e848 100644 --- a/authentication/authentication-internal-service/src/main/java/io/mosip/authentication/internal/service/listener/InternalAuthWebSubInitializer.java +++ b/authentication/authentication-internal-service/src/main/java/io/mosip/authentication/internal/service/listener/InternalAuthWebSubInitializer.java @@ -16,6 +16,7 @@ import io.mosip.authentication.common.service.websub.impl.MasterDataUpdateEventInitializer; import io.mosip.authentication.common.service.websub.impl.PartnerCACertEventInitializer; import io.mosip.authentication.common.service.websub.impl.PartnerServiceEventsInitializer; +import io.mosip.authentication.common.service.websub.impl.RemoveIdStatusEventPublisher; /** * The Class InternalAuthWebSubInitializer. @@ -49,6 +50,10 @@ public class InternalAuthWebSubInitializer extends CacheUpdatingWebsubInitialize @Autowired private AuthTransactionStatusEventPublisher authTransactionStatusEventPublisher; + /** The remove id status event publisher. */ + @Autowired + private RemoveIdStatusEventPublisher removeIdStatusEventPublisher; + /** The partner service events subscriber. */ @Autowired private PartnerServiceEventsInitializer partnerServiceEventsInitializer; @@ -84,6 +89,7 @@ protected int doRegisterTopics() { webSubHelper.initRegistrar(credentialStoreStatusEventPublisher); webSubHelper.initRegistrar(authTypeStatusEventPublisher); webSubHelper.initRegistrar(authTransactionStatusEventPublisher); + webSubHelper.initRegistrar(removeIdStatusEventPublisher); if(Objects.nonNull(fraudEventPublisher)) webSubHelper.initRegistrar(fraudEventPublisher); return HttpStatus.SC_OK;