• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2025 Northeastern University
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from abc import ABC, abstractmethod
20from enum import Enum
21from typing import Dict, Any, Type, List
22
23from ohos.sbom.sbom.metadata.sbom_meta_data import SBOMMetaData
24
25
26class SBOMFormat(Enum):
27    """
28    Enumeration of supported SBOM formats.
29
30    Currently supported formats:
31        - SPDX: Software Package Data Exchange format
32    """
33    SPDX = "SPDX"
34
35
36class ISBOMConverter(ABC):
37    """
38    Abstract base class defining the interface for SBOM format converters.
39
40    All concrete SBOM converters must implement this interface.
41    """
42
43    @abstractmethod
44    def convert(self, sbom_meta: SBOMMetaData) -> Dict[str, Any]:
45        """
46        Convert SBOM metadata to the target format.
47
48        Args:
49            sbom_meta: Source SBOM metadata to convert
50
51        Returns:
52            Dictionary containing the converted SBOM data in target format
53        """
54        pass
55
56
57class SBOMConverterFactory:
58    """
59    Factory class for creating SBOM format converters.
60
61    Implements registry pattern to manage available converters.
62    """
63
64    _registry: Dict[SBOMFormat, Type[ISBOMConverter]] = {}
65
66    @classmethod
67    def register(cls, sbom_format: SBOMFormat, converter: Type[ISBOMConverter]) -> None:
68        """
69        Register a converter for a specific SBOM format.
70
71        Args:
72            sbom_format: Target format from SBOMFormat enum
73            converter: Converter class implementing ISBOMConverter
74        """
75        if not isinstance(converter, type):
76            raise TypeError("Converter must be a class")
77        if not issubclass(converter, ISBOMConverter):
78            raise ValueError(f"{converter.__name__} must implement ISBOMConverter interface")
79        cls._registry[sbom_format] = converter
80
81    @classmethod
82    def create(cls, sbom_format: SBOMFormat) -> ISBOMConverter:
83        """
84        Create an instance of the converter for the specified format.
85
86        Args:
87            sbom_format: Target format from SBOMFormat enum
88
89        Returns:
90            New converter instance implementing ISBOMConverter
91        """
92        if sbom_format not in cls._registry:
93            raise ValueError(f"Unsupported format: {sbom_format}")
94        return cls._registry[sbom_format]()
95
96    @classmethod
97    def supported_formats(cls) -> List[SBOMFormat]:
98        """
99        Get list of currently supported SBOM formats.
100
101        Returns:
102            List of registered SBOMFormat enum members
103        """
104        return list(cls._registry.keys())
105