Skip to content

Commit

Permalink
Merge pull request Azure#1 from jddarby/sunny/vnf-definition
Browse files Browse the repository at this point in the history
VNF NFD generator
  • Loading branch information
sunnycarter authored Apr 26, 2023
2 parents d4ee4e6 + cff0186 commit b761cac
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/aosm/azext_aosm/_configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from typing import Optional
from knack.util import CLIError
from ._constants import VNF, CNF, NSD

@dataclass
class ArtifactConfig:
Expand Down Expand Up @@ -29,11 +30,11 @@ def get_configuration(definition_type, config_as_dict=None):
if config_as_dict is None:
config_as_dict = {}

if definition_type == "vnf":
if definition_type == VNF:
config = VNFConfiguration(**config_as_dict)
elif definition_type == "cnf":
elif definition_type == CNF:
config = Configuration(**config_as_dict)
elif definition_type == "nsd":
elif definition_type == NSD:
config = Configuration(**config_as_dict)
else:
raise CLIError("Definition type not recognized, options are: vnf, cnf or nsd")
Expand Down
14 changes: 14 additions & 0 deletions src/aosm/azext_aosm/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
"""Constants used across aosm cli extension."""

# The types of definition that can be generated
VNF = "vnf"
CNF = "cnf"
NSD = "nsd"

# Artifact Types
VHD_ARTIFACT = "VhdImageFile"
ARM_TEMPLATE_ARTIFACT = "ArmTemplate"
3 changes: 2 additions & 1 deletion src/aosm/azext_aosm/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from argcomplete.completers import FilesCompleter
from azure.cli.core import AzCommandsLoader
from knack.arguments import CLIArgumentType
from ._constants import VNF, CNF, NSD


def load_arguments(self: AzCommandsLoader, _):

from azure.cli.core.commands.parameters import file_type, get_enum_type, get_three_state_flag

definition_type = get_enum_type(["vnf", "cnf", "nsd"])
definition_type = get_enum_type([VNF, CNF, NSD])

# Set the argument context so these options are only available when this specific command
# is called.
Expand Down
26 changes: 21 additions & 5 deletions src/aosm/azext_aosm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import json
from dataclasses import asdict
from typing import Optional, Tuple
from azext_aosm.generate_nfd.cnf_nfd_generator import CnfNfdGenerator
from azext_aosm.generate_nfd.nfd_generator_base import NFDGenerator
from azext_aosm.generate_nfd.vnf_nfd_generator import VnfNfdGenerator
from knack.log import get_logger
from azure.cli.core.azclierror import AzCLIError
from azure.mgmt.resource import ResourceManagementClient
from .vendored_sdks import HybridNetworkManagementClient
from .vendored_sdks.models import Publisher, NetworkFunctionDefinitionVersion
from ._client_factory import cf_resources
from ._configuration import Configuration, VNFConfiguration, get_configuration
from ._constants import VNF, CNF, NSD


logger = get_logger(__name__)
Expand All @@ -38,21 +42,21 @@ def _required_resources_exist(
config.acr_artifact_store_name,
):
return False
if definition_type == "vnf":
if definition_type == VNF:
if not resource_client.check_existence(
config.publisher_resource_group_name,
NFDG_RESOURCE_TYPE,
config.name,
):
return False
elif definition_type == "nsd":
elif definition_type == NSD:
if not resource_client.check_existence(
config.publisher_resource_group_name,
NSDG_RESOURCE_TYPE,
config.name,
):
return False
elif definition_type == "cnf":
elif definition_type == CNF:
if not resource_client.check_existence(
config.publisher_resource_group_name,
NFDG_RESOURCE_TYPE,
Expand Down Expand Up @@ -102,5 +106,17 @@ def generate_definition_config(cmd, definition_type, output_file="input.json"):
"Empty definition configuration has been written to %s",
output_file,
)



def _generate_nfd(definition_type, config):
"""_summary_
:param definition_type: _description_
:type definition_type: _type_
"""
nfd_generator: NFDGenerator
if definition_type == VNF:
nfd_generator = VnfNfdGenerator(config)
elif definition_type == CNF:
nfd_generator = CnfNfdGenerator(config)

nfd_generator.generate_nfd()
21 changes: 21 additions & 0 deletions src/aosm/azext_aosm/generate_nfd/cnf_nfd_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# --------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT
# License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------
"""Contains a class for generating VNF NFDs and associated resources."""
from typing import Dict, Any
from .nfd_generator_base import NFDGenerator

class CnfNfdGenerator(NFDGenerator):
"""_summary_
:param NFDGenerator: _description_
:type NFDGenerator: _type_
"""
def __init__(
self,
config: Dict[Any, Any]
):
super(NFDGenerator, self).__init__(
config=config,
)
30 changes: 30 additions & 0 deletions src/aosm/azext_aosm/generate_nfd/nfd_generator_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# --------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT
# License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------
"""Contains a base class for generating NFDs."""
from typing import Dict, Any
from knack.log import get_logger
logger = get_logger(__name__)

class NFDGenerator:
"""A class for generating an NFD from a config file."""

def __init__(
self,
config: Dict[Any, Any]
) -> None:
"""_summary_
:param definition_type: _description_
:type definition_type: str
:param config: _description_
:type config: Dict[Any, Any]
"""
self.config = config

def generate_nfd(self) -> None:
"""No-op on base class
"""
logger.error("Generate NFD called on base class. No-op")
return
47 changes: 47 additions & 0 deletions src/aosm/azext_aosm/generate_nfd/vnf_nfd_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# --------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT
# License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------
"""Contains a class for generating VNF NFDs and associated resources."""
from typing import Dict, Any
from .nfd_generator_base import NFDGenerator
from knack.log import get_logger
from azext_aosm.vendored_sdks import HybridNetworkManagementClient
from azext_aosm.vendored_sdks.models import Publisher, NetworkFunctionDefinitionVersion, NetworkFunctionDefinitionGroup, ArtifactManifest, ManifestArtifactFormat
from azext_aosm._constants import VHD_ARTIFACT, ARM_TEMPLATE_ARTIFACT


logger = get_logger(__name__)

class VnfNfdGenerator(NFDGenerator):
"""_summary_
:param NFDGenerator: _description_
:type NFDGenerator: _type_
"""
def __init__(
self,
config: Dict[Any, Any]
):
super(NFDGenerator, self).__init__(
config=config,
)

def generate_nfd(self) -> None:
"""Generate a VNF NFD which comprises an group, an Artifact Manifest and a NFDV.
"""
arty_manny_sa = ArtifactManifest(location="blah",
tags={"blah": "blah"},
artifacts=[ManifestArtifactFormat(artifact_name="blah",
artifact_type=VHD_ARTIFACT,
artifact_version="blah")])

arty_manny_acr = ArtifactManifest(location="blah",
tags={"blah": "blah"},
artifacts=[ManifestArtifactFormat(artifact_name="blah",
artifact_type=ARM_TEMPLATE_ARTIFACT,
artifact_version="blah")])




0 comments on commit b761cac

Please sign in to comment.