Skip to content
Snippets Groups Projects
Commit fc7d0a25 authored by Konrad Krasnodebski's avatar Konrad Krasnodebski
Browse files

Extend cron to check about to expire legal tags

parent b67aac7f
No related branches found
No related tags found
2 merge requests!620Update version of default branch to 0.27.0-SNAPSHOT,!535Extend cron to catch about to expire legal tags
Showing
with 366 additions and 25 deletions
......@@ -3,7 +3,6 @@ package org.opengroup.osdu.legal.controller;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.legal.api.LegalTagStatusJobApi;
......@@ -12,6 +11,7 @@ import org.opengroup.osdu.legal.logging.AuditLogger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.opengroup.osdu.legal.jobs.models.LegalTagJobResult;
import javax.inject.Inject;
import java.util.Collection;
......@@ -58,7 +58,7 @@ public class LegalTagStatusJobController implements LegalTagStatusJobApi {
private boolean runJob(DpsHeaders convertedHeaders, TenantInfo tenantInfo, LegalTagStatusJob legalTagStatusJob) {
boolean success = true;
try {
StatusChangedTags result = legalTagStatusJob.run(tenantInfo.getProjectId(), convertedHeaders, tenantInfo.getName());
LegalTagJobResult result = legalTagStatusJob.run(tenantInfo.getProjectId(), convertedHeaders, tenantInfo.getName());
auditLogger.legalTagJobRanSuccess(singletonList(result.toString()));
} catch (Exception e) {
success = false;
......
......@@ -5,12 +5,21 @@ import org.opengroup.osdu.core.common.model.legal.StatusChangedTag;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagPublisher;
import org.opengroup.osdu.legal.provider.interfaces.IAboutToExpireLegalTagPublisher;
import org.opengroup.osdu.legal.tags.LegalTagConstraintValidator;
import org.opengroup.osdu.legal.tags.LegalTagService;
import org.opengroup.osdu.core.common.model.legal.LegalTag;
import org.opengroup.osdu.core.common.model.legal.Properties;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
import org.opengroup.osdu.legal.jobs.models.LegalTagJobResult;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import org.opengroup.osdu.core.common.model.http.AppException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Calendar;
import java.util.Date;
import javax.inject.Inject;
@Component
......@@ -22,33 +31,85 @@ public class LegalTagStatusJob {
@Inject
private ILegalTagPublisher legalTagPublisher;
@Inject
private IAboutToExpireLegalTagPublisher aboutToExpireLegalTagPublisher;
@Inject
private JaxRsDpsLog log;
public StatusChangedTags run(String projectId, DpsHeaders headers, String tenantName) throws Exception {
StatusChangedTags statusChangedTags = new StatusChangedTags();
@Value("${LEGALTAG_EXPIRATION}")
private String legalTagExpiration;
public LegalTagJobResult run(String projectId, DpsHeaders headers, String tenantName) throws Exception {
LegalTagJobResult legalTagJobResult = new LegalTagJobResult(new StatusChangedTags(), new AboutToExpireLegalTags());
validator.setHeaders(headers);
statusChangedTags = checkAndUpdateLegalTagStatus(true, tenantName, statusChangedTags);
statusChangedTags = checkAndUpdateLegalTagStatus(false, tenantName, statusChangedTags);
legalTagJobResult = checkAndUpdateLegalTagStatus(true, tenantName, legalTagJobResult);
legalTagJobResult = checkAndUpdateLegalTagStatus(false, tenantName, legalTagJobResult);
publishLegalTagStatusUpdateEvents(!statusChangedTags.getStatusChangedTags().isEmpty(), projectId, headers, statusChangedTags);
publishLegalTagStatusUpdateEvents(!legalTagJobResult.statusChangedTags.getStatusChangedTags().isEmpty(), projectId, headers, legalTagJobResult.statusChangedTags);
publisAboutToExpireLegalTagEvents(!legalTagJobResult.aboutToExpireLegalTags.getAboutToExpireLegalTags().isEmpty(), projectId, headers, legalTagJobResult.aboutToExpireLegalTags);
return statusChangedTags;
return legalTagJobResult;
}
private StatusChangedTags checkAndUpdateLegalTagStatus(Boolean isCurrentlyValid, String tenantName, StatusChangedTags statusChangedTags) {
private LegalTagJobResult checkAndUpdateLegalTagStatus(Boolean isCurrentlyValid, String tenantName, LegalTagJobResult legalTagJobResult) {
Collection<LegalTag> validLegalTags = legalTagService.listLegalTag(isCurrentlyValid, tenantName);
for (LegalTag tag : validLegalTags) {
String errors = validator.getErrors(tag);
Boolean hasErrors = errors != null;
if (isCurrentlyValid) {
checkAboutToExpireLegalTag(tag, tenantName, legalTagJobResult.aboutToExpireLegalTags, hasErrors);
}
if (isCurrentlyValid.equals(hasErrors)) {
log.info(String.format("Changing state of %s from %s to %s. Errors found in legaltag %s", tag.getName(),
isCurrentlyValid ? "Valid" : "Invalid",
hasErrors ? "Invalid" : "Valid", errors));
statusChangedTags.getStatusChangedTags().add(new StatusChangedTag(tag.getName(), hasErrors ? LegalTagCompliance.incompliant : LegalTagCompliance.compliant));
legalTagJobResult.statusChangedTags.getStatusChangedTags().add(new StatusChangedTag(tag.getName(), hasErrors ? LegalTagCompliance.incompliant : LegalTagCompliance.compliant));
legalTagService.updateStatus(tag.getName(), !isCurrentlyValid, tenantName);
}
}
return statusChangedTags;
return legalTagJobResult;
}
private void checkAboutToExpireLegalTag(LegalTag tag, String tenantName, AboutToExpireLegalTags aboutToExpireLegalTags, Boolean hasErrors) {
Properties properties = tag.getProperties();
Date expirationDate = properties.getExpirationDate();
Date today = new Date();
Date aboutToExpireDate = getAboutToExpireDate(expirationDate);
Boolean isAboutToExpire = aboutToExpireDate.before(today);
if (isAboutToExpire) {
log.info(String.format("Found legal tag about to expire: %s", tag.getName()));
aboutToExpireLegalTags.getAboutToExpireLegalTags().add(tag.getName());
}
}
private Date getAboutToExpireDate(Date expirationDate) throws AppException {
Calendar cal = Calendar.getInstance();
cal.setTime(expirationDate);
try {
if (legalTagExpiration.contains("d")) {
int numberOfDays = Integer.parseInt(legalTagExpiration.replace("d", ""));
cal.add(Calendar.DAY_OF_YEAR, - numberOfDays);
} else if (legalTagExpiration.contains("w")) {
int numberOfWeeks = Integer.parseInt(legalTagExpiration.replace("w", ""));
cal.add(Calendar.DAY_OF_YEAR, - 7 * numberOfWeeks);
} else if (legalTagExpiration.contains("m")) {
int numberOfMonths = Integer.parseInt(legalTagExpiration.replace("m", ""));
cal.add(Calendar.MONTH, - numberOfMonths);
} else if (legalTagExpiration.contains("y")) {
int numberOfYears = Integer.parseInt(legalTagExpiration.replace("y", ""));
cal.add(Calendar.YEAR, - numberOfYears);
} else {
throw new AppException(500, "Server error", String.format("Invalid legalTagExpiration value: %s", legalTagExpiration));
}
} catch (NumberFormatException e) {
log.error(String.format("Invalid legalTagExpiration value: %s", legalTagExpiration));
throw new AppException(500, "Server error", String.format("Invalid legalTagExpiration value: %s", legalTagExpiration));
}
return cal.getTime();
}
private void publishLegalTagStatusUpdateEvents(boolean hasStatusChanges, String projectId, DpsHeaders headers, StatusChangedTags statusChangedTags) throws Exception {
......@@ -56,4 +117,10 @@ public class LegalTagStatusJob {
legalTagPublisher.publish(projectId, headers, statusChangedTags);
}
}
private void publisAboutToExpireLegalTagEvents(boolean hasTags, String projectId, DpsHeaders headers, AboutToExpireLegalTags aboutToExpireLegalTags) throws Exception {
if (hasTags) {
aboutToExpireLegalTagPublisher.publish(projectId, headers, aboutToExpireLegalTags);
}
}
}
package org.opengroup.osdu.legal.jobs.models;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class AboutToExpireLegalTags {
private List<String> aboutToExpireLegalTags = new ArrayList<>();
}
package org.opengroup.osdu.legal.jobs.models;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
public class LegalTagJobResult {
public StatusChangedTags statusChangedTags;
public AboutToExpireLegalTags aboutToExpireLegalTags;
public LegalTagJobResult(StatusChangedTags statusChangedTags, AboutToExpireLegalTags aboutToExpireLegalTags) {
this.statusChangedTags = statusChangedTags;
this.aboutToExpireLegalTags = aboutToExpireLegalTags;
}
}
package org.opengroup.osdu.legal.provider.interfaces;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.legal.LegalTag;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import java.util.ArrayList;
public interface IAboutToExpireLegalTagPublisher {
void publish(String projectId, DpsHeaders headers, AboutToExpireLegalTags tags) throws Exception;
}
......@@ -18,6 +18,8 @@ import org.opengroup.osdu.legal.jobs.LegalTagStatusJob;
import org.opengroup.osdu.legal.logging.AuditLogger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.opengroup.osdu.legal.jobs.models.LegalTagJobResult;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import java.util.Collections;
import java.util.List;
......@@ -66,7 +68,8 @@ public class LegalTagStatusJobControllerTest {
StatusChangedTag statusChangedTag = new StatusChangedTag("testTag", LegalTagCompliance.incompliant);
StatusChangedTags statusChangedTags = new StatusChangedTags();
statusChangedTags.getStatusChangedTags().add(statusChangedTag);
when(legalTagStatusJob.run("projectId", dpsHeaders, "tenantName")).thenReturn(statusChangedTags);
LegalTagJobResult legalTagJobResult = new LegalTagJobResult(statusChangedTags, new AboutToExpireLegalTags());
when(legalTagStatusJob.run("projectId", dpsHeaders, "tenantName")).thenReturn(legalTagJobResult);
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
......@@ -98,11 +101,12 @@ public class LegalTagStatusJobControllerTest {
StatusChangedTag statusChangedTag = new StatusChangedTag("testTag", LegalTagCompliance.incompliant);
StatusChangedTags statusChangedTags = new StatusChangedTags();
statusChangedTags.getStatusChangedTags().add(statusChangedTag);
LegalTagJobResult legalTagJobResult = new LegalTagJobResult(statusChangedTags, new AboutToExpireLegalTags());
when(tenantStorageFactory.listTenantInfo()).thenReturn(List.of(tenantInfo1, tenantInfo2));
Exception exception = new Exception("error occurred");
when(legalTagStatusJob.run("projectId1", dpsHeaders, "tenantName1")).thenThrow(exception);
when(legalTagStatusJob.run("projectId2", dpsHeaders, "tenantName2")).thenReturn(statusChangedTags);
when(legalTagStatusJob.run("projectId2", dpsHeaders, "tenantName2")).thenReturn(legalTagJobResult);
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
......
......@@ -6,9 +6,13 @@ import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.legal.logging.AuditLogger;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.legal.provider.interfaces.ILegalTagPublisher;
import org.opengroup.osdu.legal.provider.interfaces.IAboutToExpireLegalTagPublisher;
import org.opengroup.osdu.legal.tags.LegalTagConstraintValidator;
import org.opengroup.osdu.legal.tags.LegalTagService;
import org.opengroup.osdu.core.common.model.legal.LegalTag;
import org.opengroup.osdu.core.common.model.legal.Properties;
import org.opengroup.osdu.legal.jobs.models.LegalTagJobResult;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import org.junit.Before;
import org.junit.Test;
......@@ -16,14 +20,17 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Calendar;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.*;
import static junit.framework.TestCase.fail;
@RunWith(MockitoJUnitRunner.class)
public class LegalTagStatusJobTests {
......@@ -36,6 +43,8 @@ public class LegalTagStatusJobTests {
@Mock
private ILegalTagPublisher messagePublisherMock;
@Mock
private IAboutToExpireLegalTagPublisher aboutToExpireLegalTagPublisherMock;
@Mock
private JaxRsDpsLog log;
@InjectMocks
......@@ -49,6 +58,7 @@ public class LegalTagStatusJobTests {
headers.put(DpsHeaders.DATA_PARTITION_ID, "SIS-INTERNAL-HQ");
headers.put(DpsHeaders.CORRELATION_ID, "12345-12345");
headers.put(DpsHeaders.USER_EMAIL, "nonexistent@nonexisent.domain");
ReflectionTestUtils.setField(sut, "legalTagExpiration", "1d");
}
@Test
......@@ -57,43 +67,43 @@ public class LegalTagStatusJobTests {
sut = createSut(validLegalTags);
//when(validator.getErrors(any())).thenReturn("");
StatusChangedTags result = sut.run("project1", headers, "tenant");
assertEquals(0, result.getStatusChangedTags().size());
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(0, result.statusChangedTags.getStatusChangedTags().size());
}
@Test
public void should_returnValidLegalTagName_when_isNotValidTagBecomesCompliant() throws Exception {
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", false);
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", false, 0);
validLegalTags.add(validLegalTagWithTrueStatus);
sut = createSut(validLegalTags);
StatusChangedTags result = sut.run("project1", headers, "tenant");
assertEquals(1, result.getStatusChangedTags().size());
assertEquals(LegalTagCompliance.compliant, result.getStatusChangedTags().get(0).getChangedTagStatus());
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(1, result.statusChangedTags.getStatusChangedTags().size());
assertEquals(LegalTagCompliance.compliant, result.statusChangedTags.getStatusChangedTags().get(0).getChangedTagStatus());
verify(validator, times(1)).setHeaders(any());
}
@Test
public void should_returnInvalidLegalTagName_when_isValidTagBecomesIncompliant() throws Exception {
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", true);
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", true, 0);
validLegalTags.add(validLegalTagWithTrueStatus);
sut = createSut(validLegalTags);
when(validator.getErrors(any())).thenReturn("Not Valid");
StatusChangedTags result = sut.run("project1", headers, "tenant");
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(1, result.getStatusChangedTags().size());
assertEquals(LegalTagCompliance.incompliant, result.getStatusChangedTags().get(0).getChangedTagStatus());
assertEquals(1, result.statusChangedTags.getStatusChangedTags().size());
assertEquals(LegalTagCompliance.incompliant, result.statusChangedTags.getStatusChangedTags().get(0).getChangedTagStatus());
}
@Test
public void should_sendLegalTagNameAndNewStatusWithCorrelationIdToPubSub_when_legalTagBecomesIncompliant() throws
Exception {
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", true);
LegalTag validLegalTagWithTrueStatus = createValidLegalTagWithIsValidStatus("legaltag", true, 0);
validLegalTags.add(validLegalTagWithTrueStatus);
sut = createSut(validLegalTags);
......@@ -125,10 +135,136 @@ public class LegalTagStatusJobTests {
verify(auditLogger, never()).publishedStatusChangeSuccess(any());
}
private static LegalTag createValidLegalTagWithIsValidStatus(String name, Boolean isValid) {
@Test
public void should_returnAboutToExpireLegalTagName_when_legalTagAboutToExpireIn3Days() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "3d");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag aboutToExpireLegalTag1 = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag1", true, 2);
validLegalTags.add(aboutToExpireLegalTag1);
LegalTag aboutToExpireLegalTag2 = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag2", true, 3);
validLegalTags.add(aboutToExpireLegalTag2);
LegalTag longTermLegalTag = createValidLegalTagWithIsValidStatus("longTermLegalTag", true, 10);
validLegalTags.add(longTermLegalTag);
sut = createSut(validLegalTags);
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(2, result.aboutToExpireLegalTags.getAboutToExpireLegalTags().size());
assertEquals("aboutToExpireLegalTag1", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(0));
assertEquals("aboutToExpireLegalTag2", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(1));
}
@Test
public void should_returnAboutToExpireLegalTagName_when_legalTagAboutToExpireIn2Weeks() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "2w");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag aboutToExpireLegalTag1 = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag1", true, 13);
validLegalTags.add(aboutToExpireLegalTag1);
LegalTag aboutToExpireLegalTag2 = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag2", true, 14);
validLegalTags.add(aboutToExpireLegalTag2);
LegalTag longTermLegalTag = createValidLegalTagWithIsValidStatus("longTermLegalTag", true, 20);
validLegalTags.add(longTermLegalTag);
sut = createSut(validLegalTags);
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(2, result.aboutToExpireLegalTags.getAboutToExpireLegalTags().size());
assertEquals("aboutToExpireLegalTag1", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(0));
assertEquals("aboutToExpireLegalTag2", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(1));
}
@Test
public void should_returnAboutToExpireLegalTagName_when_legalTagAboutToExpireIn3Months() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "3m");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag aboutToExpireLegalTag = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag", true, 80);
validLegalTags.add(aboutToExpireLegalTag);
LegalTag longTermLegalTag = createValidLegalTagWithIsValidStatus("longTermLegalTag", true, 100);
validLegalTags.add(longTermLegalTag);
sut = createSut(validLegalTags);
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(1, result.aboutToExpireLegalTags.getAboutToExpireLegalTags().size());
assertEquals("aboutToExpireLegalTag", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(0));
}
@Test
public void should_returnAboutToExpireLegalTagName_when_legalTagAboutToExpireIn1Year() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "1y");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag aboutToExpireLegalTag = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag", true, 354);
validLegalTags.add(aboutToExpireLegalTag);
LegalTag longTermLegalTag = createValidLegalTagWithIsValidStatus("longTermLegalTag", true, 367);
validLegalTags.add(longTermLegalTag);
sut = createSut(validLegalTags);
LegalTagJobResult result = sut.run("project1", headers, "tenant");
assertEquals(1, result.aboutToExpireLegalTags.getAboutToExpireLegalTags().size());
assertEquals("aboutToExpireLegalTag", result.aboutToExpireLegalTags.getAboutToExpireLegalTags().get(0));
}
@Test
public void should_returnAboutToExpireLegalTagName_when_invalidTimeToExpire() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "asd");
Collection<LegalTag> validLegalTags = new ArrayList<>();
sut = createSut(validLegalTags);
try {
sut.run("project1", headers, "tenant");
} catch (Exception e) {
fail(String.format("Should not throw this exception: '%s'", e.getMessage()));
}
}
@Test
public void should_sendLegalTagNameToPubSub_when_legalTagAboutToExpire() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "2w");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag aboutToExpireLegalTag = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag1", true, 13);
validLegalTags.add(aboutToExpireLegalTag);
sut = createSut(validLegalTags);
sut.run("project1", headers, "tenant");
verify(aboutToExpireLegalTagPublisherMock, times(1)).publish(any(), any(), any());
}
@Test
public void should_notSendLegalTagNameToPubSub_when_legalTagNotAboutToExpire() throws Exception {
ReflectionTestUtils.setField(sut, "legalTagExpiration", "2w");
Collection<LegalTag> validLegalTags = new ArrayList<>();
LegalTag longTermLegalTag = createValidLegalTagWithIsValidStatus("aboutToExpireLegalTag1", true, 20);
validLegalTags.add(longTermLegalTag);
sut = createSut(validLegalTags);
sut.run("project1", headers, "tenant");
verify(aboutToExpireLegalTagPublisherMock, times(0)).publish(any(), any(), any());
}
private static LegalTag createValidLegalTagWithIsValidStatus(String name, Boolean isValid, int daysToExpire) {
LegalTag legalTag = new LegalTag();
legalTag.setName(name);
legalTag.setIsValid(isValid);
if (daysToExpire > 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date(System.currentTimeMillis()));
cal.add(Calendar.DAY_OF_YEAR, + daysToExpire);
Properties properties = new Properties();
properties.setExpirationDate(new java.sql.Date(cal.getTimeInMillis()));
legalTag.setProperties(properties);
}
return legalTag;
}
......
package org.opengroup.osdu.legal.aws.jobs;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.AmazonSNS;
import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider;
import org.opengroup.osdu.core.aws.ssm.K8sParameterNotFoundException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.aws.sns.AmazonSNSConfig;
import org.opengroup.osdu.core.aws.sns.PublishRequestBuilder;
import org.opengroup.osdu.legal.provider.interfaces.IAboutToExpireLegalTagPublisher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.opengroup.osdu.legal.jobs.models.AboutToExpireLegalTags;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AboutToExpireLegalTagPublisherImpl implements IAboutToExpireLegalTagPublisher {
private String amazonSNSTopic;
@Value("${aws.sns.region}")
private String amazonSNSRegion;
private AmazonSNS snsClient;
private K8sLocalParameterProvider k8sLocalParameterProvider;
@Value("${OSDU_ABOUT_TO_EXPIRE_LEGALTAG_TOPIC}")
private String osduAboutToExpireLegalTagTopic;
@Inject
private JaxRsDpsLog log;
public void setK8sLocalParameterProvider(K8sLocalParameterProvider k8sLocalParameterProvider) {
this.k8sLocalParameterProvider = k8sLocalParameterProvider;
}
@PostConstruct
public void init() throws K8sParameterNotFoundException {
if (this.k8sLocalParameterProvider == null) {
this.k8sLocalParameterProvider = new K8sLocalParameterProvider();
}
AmazonSNSConfig snsConfig = new AmazonSNSConfig(amazonSNSRegion);
snsClient = snsConfig.AmazonSNS();
amazonSNSTopic = k8sLocalParameterProvider.getParameterAsString("legal-sns-topic-arn");
}
@Override
public void publish(String projectId, DpsHeaders headers, AboutToExpireLegalTags aboutToExpireLegalTags) {
final int BATCH_SIZE = 50;
// attributes
PublishRequestBuilder<AwsAboutToExpireLegalTags> publishRequestBuilder = new PublishRequestBuilder<>();
publishRequestBuilder.setGeneralParametersFromHeaders(headers);
log.debug("Publishing to topic " + osduAboutToExpireLegalTagTopic);
for (int i = 0; i < aboutToExpireLegalTags.getAboutToExpireLegalTags().size(); i += BATCH_SIZE){
List<String> batch = aboutToExpireLegalTags.getAboutToExpireLegalTags().subList(i, Math.min(aboutToExpireLegalTags.getAboutToExpireLegalTags().size(), i + BATCH_SIZE));
List<AwsAboutToExpireTag> awsBatch = batch.stream()
.map(name -> new AwsAboutToExpireTag(name, headers.getPartitionId()))
.collect(Collectors.toList());
AwsAboutToExpireLegalTags awsBatchTags = new AwsAboutToExpireLegalTags(awsBatch);
PublishRequest publishRequest = publishRequestBuilder.generatePublishRequest(osduAboutToExpireLegalTagTopic, amazonSNSTopic, awsBatchTags);
snsClient.publish(publishRequest);
}
}
}
record AwsAboutToExpireLegalTags(List<AwsAboutToExpireTag> aboutToExpireLegalTags) { }
package org.opengroup.osdu.legal.aws.jobs;
import lombok.Data;
@Data
public class AwsAboutToExpireTag {
private String tagName;
private String dataPartitionId;
AwsAboutToExpireTag(){
}
public AwsAboutToExpireTag(String tagName, String dataPartitionId) {
this.tagName = tagName;
this.dataPartitionId = dataPartitionId;
}
}
......@@ -31,7 +31,10 @@ LEGAL_HOSTNAME=notused
CRON_JOB_IP=10.0.0.1
ACCEPT_HTTP=true
LEGALTAG_EXPIRATION=2w
OSDU_TOPIC=${OSDU_LEGAL_TOPIC:legal-tag-status-changed}
OSDU_ABOUT_TO_EXPIRE_LEGALTAG_TOPIC=${OSDU_LEGAL_TOPIC:about-to-expire-legal-tag}
aws.s3.legal.config.file-name=Legal_COO.json
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment