Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Jira
serverONAP Jira
columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
serverId425b2b0a-557c-3c0c-b515-579789cceedb
keyCPS-506


Background

Acceptance Criteria

Add a method to cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java to retrieve all modules and version known by CPS

Steps

...

List all know modules and revision know by CPS. i.e list the module and revisions of yang resources in the yang_resources db table. Currently within this yang_resources table, we are storing fileName, content and checksum, we do not store module metadata. To retrieve module name and revision, we need to store these information when a yang resource is being added to the db table.

Proposal

For the following open question on the Jira.

...

Code Block
languagejava
themeMidnight
titleChanges needed to be made to CpsModulePersistenceServiceImpl
// need to add the following pattern in line 79
private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
// in the method synchronizeYangResources the following changes need to be added
// after line 123
final Map<String,String> metaDataMap = getModuleNameAndRevision(entry.getKey(), entry.getValue());
// after line 127 
yangResourceEntity.setModuleName(metaDataMap.get("moduleName"));
yangResourceEntity.setRevision(metaDataMap.get("revision"));
// at the end of the file add the following methods
    private static HashMap<String, String> getModuleNameAndRevision(final String sourceName, final String source) {
        final HashMap<String, String> metaDataMap = new HashMap<>();
        final var revisionSourceIdentifier =
                createIdentifierFromSourceName(checkNotNull(sourceName));

        YangTextSchemaSource 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", String.valueOf(dependencyInfo.getRevision()));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (YangSyntaxErrorException e) {
            e.printStackTrace();
        }
        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);
    }

Following on from this, we need to add a liquibase step to update the yang_resources table to include the two new columns. To do this, create a new yaml file → 10-add-column-to-yang-resources-table.yaml

Code Block
languageyml
themeMidnight
titleLiquibase Update
databaseChangeLog:
  - changeset:
      id: 10
      label: addModuleNameAndRevisionColumn
      author: cps
      changes:
        - addColumn:
            tableName: yang_resource
            columns:
              - column:
                  name: module_name
                  type: TEXT
              - column:
                  name: revision
                  type: TEXT

The cps-ri/src/main/resources/changelog/changelog-master.yaml will also include the following at the end of the file

  • - include:
    file: changelog/db/changes/10-add-column-to-yang-resources-table.yaml

With these changes, when a yang resource is being inserted into the database the module name and revision will also be included.

Finally, we need  to add a method to cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java to retrieve all modules and version known by CPS.

Code Block
languagejava
themeMidnight
titleMethod added to CpsModuleService.java
    /**
     * Retrieve all modules and revisions known by CPS
     * @return a list of ModuleReferences
     */
    List<ModuleReferences> getAllYangResources();

For this we will use an existing model called ModuleReference in the cps-service/src/main/java/org/onap/cps/spi/model/ModuleReference.java

Another method getAllYangResources will also need to be added in the cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java

Code Block
languagejava
themeMidnight
titleMethod added to CpsModulePersistenceService.java
    /**
     * Returns all YANG resources
     *
     * @return List of ModuleReferences
     */
    List<ModuleReferences> getAllYangResources();