+ private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) {
+ final Map<String, String> metaDataMap = new HashMap<>();
+ final var revisionSourceIdentifier =
+ createIdentifierFromSourceName(checkNotNull(sourceName));
+
+ final var tempYangTextSchemaSource = new YangTextSchemaSource(revisionSourceIdentifier) {
+ @Override
+ protected MoreObjects.ToStringHelper addToStringAttributes(
+ final MoreObjects.ToStringHelper toStringHelper) {
+ return toStringHelper;
+ }
+
+ @Override
+ public InputStream openStream() {
+ return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
+ }
+ };
+ try {
+ final var dependencyInfo = YangModelDependencyInfo.forYangText(tempYangTextSchemaSource);
+ metaDataMap.put("moduleName", dependencyInfo.getName());
+ metaDataMap.put("revision", dependencyInfo.getFormattedRevision());
+ } catch (final YangSyntaxErrorException | IOException e) {
+ throw new ModelValidationException("Yang resource is invalid.",
+ String.format("Yang syntax validation failed for resource %s:%n%s", sourceName, e.getMessage()), e);
+ }
+ return metaDataMap;
+ }
+
+ private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
+ final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
+ if (matcher.matches()) {
+ return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
+ }
+ return RevisionSourceIdentifier.create(sourceName);
+ }
+
+ /**
+ * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
+ * if the cause of the error is a yang checksum database constraint violation.
+ *
+ * @param originalException the original db exception.
+ * @param yangResourceEntities the collection of Yang resources involved in the db failure.
+ * @return an optional converted CPS duplicated Yang resource exception. The optional is empty if the original
+ * cause of the error is not a yang checksum database constraint violation.
+ */
+ private Optional<DuplicatedYangResourceException> convertToDuplicatedYangResourceException(
+ final DataIntegrityViolationException originalException,
+ final Collection<YangResourceEntity> yangResourceEntities) {
+
+ // The exception result
+ DuplicatedYangResourceException duplicatedYangResourceException = null;
+
+ final Throwable cause = originalException.getCause();
+ if (cause instanceof ConstraintViolationException) {
+ final ConstraintViolationException constraintException = (ConstraintViolationException) cause;
+ if (YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME.equals(constraintException.getConstraintName())) {
+ // Db constraint related to yang resource checksum uniqueness is not respected
+ final String checksumInError = getDuplicatedChecksumFromException(constraintException);
+ final String nameInError = getNameForChecksum(checksumInError, yangResourceEntities);
+ duplicatedYangResourceException =
+ new DuplicatedYangResourceException(nameInError, checksumInError, constraintException);
+ }
+ }
+
+ return Optional.ofNullable(duplicatedYangResourceException);
+
+ }
+
+ /**
+ * Get the name of the yang resource having the specified checksum.
+ *
+ * @param checksum the checksum. Null is supported.
+ * @param yangResourceEntities the list of yang resources to search among.
+ * @return the name found or null if none.
+ */
+ private String getNameForChecksum(
+ final String checksum, final Collection<YangResourceEntity> yangResourceEntities) {
+ return
+ yangResourceEntities.stream()
+ .filter(entity -> StringUtils.equals(checksum, (entity.getChecksum())))
+ .findFirst()
+ .map(YangResourceEntity::getName)
+ .orElse(null);
+ }
+
+ /**
+ * Get the checksum that caused the constraint violation exception.
+ *
+ * @param exception the exception having the checksum in error.
+ * @return the checksum in error or null if not found.
+ */
+ private String getDuplicatedChecksumFromException(final ConstraintViolationException exception) {
+ String checksum = null;
+ final var matcher = CHECKSUM_EXCEPTION_PATTERN.matcher(exception.getSQLException().getMessage());
+ if (matcher.find() && matcher.groupCount() == 1) {
+ checksum = matcher.group(1);
+ }
+ return checksum;
+ }
+
+ private static ModuleReference toModuleReference(
+ final YangResourceModuleReference yangResourceModuleReference) {
+ return ModuleReference.builder()
+ .moduleName(yangResourceModuleReference.getModuleName())
+ .revision(yangResourceModuleReference.getRevision())
+ .build();
+ }