Skip to content

Commit

Permalink
registered topic
Browse files Browse the repository at this point in the history
Signed-off-by: Ritik Jain (IN74108) <[email protected]>
  • Loading branch information
Ritik Jain (IN74108) committed May 28, 2024
1 parent 66d8089 commit ea888b2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
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;

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;
Expand Down Expand Up @@ -80,13 +77,8 @@ static interface ConsumerWithBusinessException<T,R> {
@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)
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put(ID_HASH, idHash);
removeIdStatusEvent.setData(dataMap);
removeIdStatusEvent.setTimestamp(DateUtils.formatToISOString(DateUtils.getUTCCurrentDateTime()));
return removeIdStatusEvent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ea888b2

Please sign in to comment.