Commit 64c3542b authored by Nikhil Singh[MicroSoft]'s avatar Nikhil Singh[MicroSoft]
Browse files

Commit 12 Contents:

1- Exception Handling
2- Short Topic name usage
parent f7809a71
Pipeline #54592 failed with stages
in 1 minute and 12 seconds
......@@ -24,7 +24,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.di.CosmosContainerConfig;
import org.opengroup.osdu.register.provider.azure.util.CryptographyUtil;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelperServiceBus;
import org.opengroup.osdu.register.subscriber.model.*;
import org.opengroup.osdu.register.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -54,14 +54,14 @@ public class CosmosSubscription {
private CryptographyUtil cryptographyUtil;
@Autowired
private PartitionHelper partitionHelper;
private PartitionHelperServiceBus partitionHelper;
@Autowired
private TopicsRepositoryImpl topicsRepository;
public Subscription createSubscription(Subscription input) throws AppException {
SubscriptionDoc doc = new SubscriptionDoc(input, dpsHeaders.getPartitionId());
doc.setTopic(partitionHelper.getFullyQualifiedTopicName(input.getTopic()));
doc.setTopic(input.getTopic());
doc.setSecretValue(cryptographyUtil.encryptData(input.getSecret().toString()));
doc.setKeyName(cryptographyUtil.getKeyName());
doc.setKeyVersion(cryptographyUtil.getKeyVersion());
......@@ -73,7 +73,7 @@ public class CosmosSubscription {
public void deleteSubscription(String subscriptionId) {
cosmosStore.deleteItem(dpsHeaders.getPartitionId(), azureBootstrapConfig.getCosmosDBName(),
cosmosContainerConfig.getSubscriptionContainerName(), subscriptionId, dpsHeaders.getPartitionId());
logger.info("Record deleted for subscription with ID: " + subscriptionId);
logger.debug("Record deleted for subscription with ID: " + subscriptionId);
}
public Subscription getSubscription(String id) {
......@@ -82,6 +82,7 @@ public class CosmosSubscription {
logger.error(String.format("Subscriber with id %s does not exist.", id));
return new AppException(404, "Not found", String.format("Subscriber with id %s does not exist.", id));
});
logger.debug("Record fetched successfully for subscription with ID: " + id);
return convertToSubscriptionClass(doc);
}
......@@ -115,12 +116,12 @@ public class CosmosSubscription {
public void updateSubscription(Subscription input) {
SubscriptionDoc doc = new SubscriptionDoc(input, dpsHeaders.getPartitionId());
doc.setTopic(partitionHelper.getFullyQualifiedTopicName(input.getTopic()));
doc.setTopic(input.getTopic());
doc.setSecretValue(cryptographyUtil.encryptData(input.getSecret().toString()));
doc.setKeyName(cryptographyUtil.getKeyName());
doc.setKeyVersion(cryptographyUtil.getKeyVersion());
cosmosStore.upsertItem(dpsHeaders.getPartitionId(), azureBootstrapConfig.getCosmosDBName(), cosmosContainerConfig.getSubscriptionContainerName(), dpsHeaders.getPartitionId(), doc);
logger.info("Updating Record for subscription with ID: ", input.getId());
logger.debug("Updating Record for subscription with ID: ", input.getId());
}
private Subscription convertToSubscriptionClass(SubscriptionDoc doc) {
......@@ -137,7 +138,7 @@ public class CosmosSubscription {
hmacSecret.setValue(secretValue);
secret = hmacSecret;
}
return new Subscription(doc.getId(), doc.getName(), doc.getDescription(), topicsRepository.getTopicAliasFromFullyQualifiedTopicName(doc.getTopic()), doc.getPushEndpoint(),
return new Subscription(doc.getId(), doc.getName(), doc.getDescription(), doc.getTopic(), doc.getPushEndpoint(),
doc.getCreatedBy(), doc.getCreatedOnEpoch(), doc.getNotificationId(), secret);
}
......
......@@ -75,7 +75,7 @@ public class SubscriptionRepository implements ISubscriptionRepository {
// This will result in an 500 Exception so the user should be able to create the subscription with
// the same topic and PubSub Endpoint combination again
logger.info(String.format("Record already exists for Subscription with id %s", input.getId()));
Subscription outputSubscription = createPubSubSubscriptionIfDoesNotExist(subscription);
Subscription outputSubscription = createPubSubSubscriptionIfDoesNotExist(input);
input.setNotificationId(outputSubscription.getNotificationId());
return input;
} else {
......@@ -89,7 +89,7 @@ public class SubscriptionRepository implements ISubscriptionRepository {
return input;
} catch (AppException e) {
if (e.getError().getCode() == 409) {
throw new AppException(409, "Conflict", "Another request is trying to create the same subscription");
throw new AppException(409, "Conflict", e.getMessage());
}
logger.error("PubSub Subscription creation failed.Rollback Cosmos Subscription Creation");
try {
......@@ -119,13 +119,10 @@ public class SubscriptionRepository implements ISubscriptionRepository {
*/
@Override
public boolean delete(String id) {
Subscription subscription = cosmosSubscription.getSubscription(id);
if (subscription == null) {
logger.info("Record not found for subscription with ID: " + id);
return false;
}
logger.debug("Record found for subscription with ID: " + id);
try {
Subscription subscription = cosmosSubscription.getSubscription(id);
logger.debug("Record found for subscription with ID: " + id);
pubSubSubscription.deleteSubscription(subscription.getNotificationId(), subscription.getTopic());
logger.debug("PubSub Subscription deleted with ID:" + subscription.getNotificationId());
try {
......@@ -140,9 +137,13 @@ public class SubscriptionRepository implements ISubscriptionRepository {
}
}
} catch (AppException e) {
// TODO:This exception is not created in flow of delete
if (e.getError().getCode() == 409) {
logger.error("Another request is trying to delete the same subscription");
throw new AppException(409, "Conflict", "Another request is trying to delete the same subscription");
} else if (e.getMessage().equals(String.format("Subscriber with id %s does not exist.", id))) {
logger.info("Record not found for subscription with ID: " + id);
return false;
} else {
logger.error("Unexpected error deleting subscription");
throw new AppException(500, "Server Error", "Unexpected error deleting subscription");
......
......@@ -57,12 +57,19 @@ public class ManagementClientFactoryImpl implements IManagementClientFactory {
String serviceBusConnectionString = pi.getSbConnection();
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(serviceBusConnectionString);
ManagementClient managementClient = new ManagementClient(connectionStringBuilder);
if (managementClient == null)
throw new Exception("Null Pointer Exception");
logger.debug("Management client creation successful for partition Id : " + dpsHeaders.getPartitionId());
this.clientCache.put(cacheKey, managementClient);
return managementClient;
} catch (Exception e) {
logger.error("Management client creation failed for partition Id : " + dpsHeaders.getPartitionId());
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
if (e.getMessage().equals("Null Pointer Exception")) {
logger.error("Management client can not be Null for partition Id : " + dpsHeaders.getPartitionId());
throw new AppException(500, "Null Pointer Exception", MANAGEMENT_CLIENT_ERROR_MESSAGE);
} else {
logger.error("Management client creation failed for partition Id : " + dpsHeaders.getPartitionId());
throw new AppException(500, "Server Error", MANAGEMENT_CLIENT_ERROR_MESSAGE + " : " + e.getMessage().toString());
}
}
}
......
......@@ -14,24 +14,23 @@
package org.opengroup.osdu.register.provider.azure.subscriber.pubsub;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.servicebus.management.ManagementClient;
import com.microsoft.azure.servicebus.primitives.MessagingEntityAlreadyExistsException;
import com.microsoft.azure.servicebus.primitives.MessagingEntityNotFoundException;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.subscriber.messageBus.IManagementClientFactory;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(value = "azure.servicebus.enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnProperty(value = "azure.serviceBus.enabled", havingValue = "true", matchIfMissing = true)
public class PullSubscription implements IPubsubSubscription {
// TODO: UPDATE ERROR_MESSAGE FOR SERVICE BUS
private static final String RESOURCE_PROVISIONING_ERROR_MESSAGE = "Resource cannot be updated during provisioning";
private static final String MANAGEMENT_CLIENT_ERROR_MESSAGE = "Unable to create management client";
@Autowired
private IManagementClientFactory factory;
@Autowired
......@@ -40,8 +39,6 @@ public class PullSubscription implements IPubsubSubscription {
private JaxRsDpsLog logger;
@Autowired
private PartitionServiceClient partitionService;
@Autowired
private PartitionHelper partitionHelper;
public void createSubscription(String subscriptionId, String topicAliasName) {
ManagementClient managementClient = factory.getManager();
......@@ -52,7 +49,11 @@ public class PullSubscription implements IPubsubSubscription {
if (e.getMessage().equals(RESOURCE_PROVISIONING_ERROR_MESSAGE)) {
logger.error("Another request is trying to create the same Pull subscription");
throw new AppException(409, "Conflict", "Another request is trying to create the same Pull subscription");
} else {
} else if (e instanceof MessagingEntityAlreadyExistsException) {
logger.error(String.format("Pull Subscription with id %s already exist.", subscriptionId));
throw new AppException(409, "Conflict", "Pull Subscription already exists");
}
else {
logger.error("Creating Pull Subscription failed with error: " + e.toString());
throw new AppException(500, "Server Error", "Unexpected error creating Pull subscription");
}
......@@ -65,10 +66,9 @@ public class PullSubscription implements IPubsubSubscription {
managementClient.deleteSubscription(topicAliasName, subscriptionId);
logger.debug(String.format("Pull Subscription with id %s deleted successfully", subscriptionId));
} catch (Exception e) {
if (e instanceof CloudException) {
CloudException cloudException = (CloudException) e;
logger.error(cloudException.toString());
throw new AppException(cloudException.response().code(), cloudException.body().code(), cloudException.body().message());
if (e instanceof MessagingEntityNotFoundException) {
logger.error(String.format("Pull Subscription with id %s does not exist.", subscriptionId));
throw new AppException(404, "Not found", String.format("Pull Subscription with id %s does not exist.", subscriptionId));
} else {
logger.error("Deleting Pull Subscription failed with error: " + e.toString());
throw new AppException(500, "Server Error", "Unexpected error deleting Pull subscription");
......@@ -82,7 +82,7 @@ public class PullSubscription implements IPubsubSubscription {
managementClient.getSubscription(topicAliasName, subscriptionId);
logger.debug(String.format("Pull Subscription with id %s fetched successfully", subscriptionId));
} catch (Exception e) {
if (e instanceof NullPointerException) {
if (e instanceof MessagingEntityNotFoundException) {
logger.error(String.format("Pull Subscription with id %s does not exist.", subscriptionId));
throw new AppException(404, "Not found", String.format("Pull Subscription with id %s does not exist.", subscriptionId));
} else {
......
......@@ -97,8 +97,6 @@ public class ManagementClientFactoryTest {
});
assertNotNull(exception);
assertEquals(500, exception.getError().getCode());
assertEquals(MANAGEMENT_CLIENT_ERROR_MESSAGE, exception.getMessage());
assertEquals("Null Pointer Exception", exception.getError().getReason());
verify(partitionService, times(1)).getPartition(dataPartitionId);
verify(partitionInfoAzure, times(1)).getSbConnection();
verify(clientCache, times(0)).put(dpsHeaders.getPartitionId() + "-managementClient", managementClient);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment