Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • tfs/controller
1 result
Show changes
Commits on Source (2)
Showing
with 3621 additions and 21 deletions
......@@ -212,24 +212,24 @@ for COMPONENT in $TFS_COMPONENTS; do
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}.log"
if [ "$COMPONENT" == "ztp" ] || [ "$COMPONENT" == "policy" ]; then
$DOCKER_BUILD -t "$COMPONENT:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/Dockerfile ./src/"$COMPONENT"/ > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/Dockerfile ./src/"$COMPONENT"/ > "$BUILD_LOG"
elif [ "$COMPONENT" == "pathcomp" ]; then
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}-frontend.log"
$DOCKER_BUILD -t "$COMPONENT-frontend:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/frontend/Dockerfile . > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT-frontend:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/frontend/Dockerfile . > "$BUILD_LOG"
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}-backend.log"
$DOCKER_BUILD -t "$COMPONENT-backend:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/backend/Dockerfile . > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT-backend:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/backend/Dockerfile . > "$BUILD_LOG"
# next command is redundant, but helpful to keep cache updated between rebuilds
IMAGE_NAME="$COMPONENT-backend:$TFS_IMAGE_TAG-builder"
$DOCKER_BUILD -t "$IMAGE_NAME" --target builder -f ./src/"$COMPONENT"/backend/Dockerfile . >> "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$IMAGE_NAME" --target builder -f ./src/"$COMPONENT"/backend/Dockerfile . >> "$BUILD_LOG"
elif [ "$COMPONENT" == "dlt" ]; then
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}-connector.log"
$DOCKER_BUILD -t "$COMPONENT-connector:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/connector/Dockerfile . > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT-connector:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/connector/Dockerfile . > "$BUILD_LOG"
BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}-gateway.log"
$DOCKER_BUILD -t "$COMPONENT-gateway:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/gateway/Dockerfile . > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT-gateway:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/gateway/Dockerfile . > "$BUILD_LOG"
else
$DOCKER_BUILD -t "$COMPONENT:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/Dockerfile . > "$BUILD_LOG"
$DOCKER_BUILD --network=host -t "$COMPONENT:$TFS_IMAGE_TAG" -f ./src/"$COMPONENT"/Dockerfile . > "$BUILD_LOG"
fi
echo " Pushing Docker image to '$TFS_REGISTRY_IMAGES'..."
......
......@@ -20,7 +20,9 @@
export TFS_REGISTRY_IMAGES="http://localhost:32000/tfs/"
# Set the list of components, separated by spaces, you want to build images for, and deploy.
export TFS_COMPONENTS="context device pathcomp service slice nbi webui load_generator"
export TFS_COMPONENTS="context device pathcomp service slice nbi webui app"
# export load_generator
# Uncomment to activate Monitoring
#export TFS_COMPONENTS="${TFS_COMPONENTS} monitoring"
......@@ -109,7 +111,7 @@ export CRDB_DEPLOY_MODE="single"
export CRDB_DROP_DATABASE_IF_EXISTS=""
# Disable flag for re-deploying CockroachDB from scratch.
export CRDB_REDEPLOY=""
export CRDB_REDEPLOY="YES"
# ----- NATS -------------------------------------------------------------------
......
......@@ -47,6 +47,7 @@ class DeviceTypeEnum(Enum):
PACKET_ROUTER = 'packet-router'
PACKET_SWITCH = 'packet-switch'
XR_CONSTELLATION = 'xr-constellation'
QKD_NODE = 'qkd-node'
# ETSI TeraFlowSDN controller
TERAFLOWSDN_CONTROLLER = 'teraflowsdn'
../../proto/src/python
\ No newline at end of file
import sys
import os
import grpc
import json
from flask import Flask, jsonify, request, abort
from common.proto.context_pb2 import Empty, ContextId, TopologyId, Uuid
from common.proto.context_pb2_grpc import ContextServiceStub
from device.service.driver_api.DriverInstanceCache import DriverInstanceCache, get_driver
from device.service.driver_api.DriverFactory import DriverFactory
from device.service.driver_api.FilterFields import FilterFieldEnum
from device.service.driver_api._Driver import _Driver
from device.service.drivers.qkd.QKDDriver2 import QKDDriver
app = Flask(__name__)
# Add the directory containing the driver_api module to the system path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..')))
# Initialize the DriverFactory and DriverInstanceCache
drivers_list = [
(QKDDriver, [{'filter_field1': 'value1', 'filter_field2': 'value2'}])
]
driver_factory = DriverFactory(drivers_list)
driver_instance_cache = DriverInstanceCache(driver_factory)
def get_context_topology_info():
try:
# Establish a gRPC channel
channel = grpc.insecure_channel('10.152.183.77:1010') # Update with the correct IP and port
stub = ContextServiceStub(channel)
# Retrieve the context information
context_list = stub.ListContexts(Empty())
contexts_info = []
for context in context_list.contexts:
context_info = {
'context_id': context.context_id.context_uuid.uuid,
'context_name': context.name,
'topologies': []
}
# Retrieve topology information for each context
topology_list = stub.ListTopologies(context.context_id)
for topology in topology_list.topologies:
topology_info = {
'topology_id': topology.topology_id.topology_uuid.uuid,
'topology_name': topology.name,
'devices': []
}
# Retrieve detailed topology information
topology_details = stub.GetTopologyDetails(topology.topology_id)
for device in topology_details.devices:
device_info = {
'device_id': device.device_id.device_uuid.uuid,
'device_name': device.name,
'device_type': device.device_type,
'status': device.device_operational_status,
'drivers': [driver for driver in device.device_drivers],
'endpoints': [{
'uuid': endpoint.endpoint_id.endpoint_uuid.uuid,
'name': endpoint.name,
'type': endpoint.endpoint_type,
'location': endpoint.endpoint_location
} for endpoint in device.device_endpoints],
'configurations': [{
'key': config.custom.resource_key,
'value': config.custom.resource_value
} for config in device.device_config.config_rules],
'interfaces': [{
'id': interface.qkdi_id,
'enabled': interface.enabled,
'name': interface.name,
'att_point': interface.qkdi_att_point,
'capabilities': interface.qkdi_capabilities
} for interface in device.qkd_interfaces.qkd_interface],
'applications': [{
'app_id': app.app_id,
'app_qos': app.app_qos,
'app_statistics': app.app_statistics,
'backing_qkdl_id': app.backing_qkdl_id,
'client_app_id': app.client_app_id
} for app in device.qkd_applications.qkd_app]
}
topology_info['devices'].append(device_info)
context_info['topologies'].append(topology_info)
contexts_info.append(context_info)
return contexts_info
except grpc.RpcError as e:
app.logger.error(f"gRPC error: {e}")
abort(502, description=f"gRPC error: {e.details()}")
except Exception as e:
app.logger.error(f"Error retrieving context topology info: {e}")
abort(500, description="Internal Server Error")
def get_detailed_device_info():
try:
context_info = get_context_topology_info()
detailed_info = []
for context in context_info:
if context['context_name'] == 'admin':
for topology in context['topologies']:
if topology['topology_name'] == 'admin':
for device in topology['devices']:
driver = get_driver_instance(device)
if driver:
detailed_info.append({
'device_info': device,
'driver_info': get_device_driver_info(driver)
})
return detailed_info
except Exception as e:
app.logger.error(f"Error retrieving detailed device info: {e}")
abort(500, description="Internal Server Error")
def get_driver_instance(device):
device_uuid = device['device_id']
driver_filter_fields = {
FilterFieldEnum.DEVICE_TYPE: device['device_type'],
FilterFieldEnum.DRIVER: device['drivers'],
}
connect_rules = {config['key']: config['value'] for config in device['configurations']}
address = connect_rules.get('_connect/address', '127.0.0.1')
port = int(connect_rules.get('_connect/port', '0'))
settings = json.loads(connect_rules.get('_connect/settings', '{}'))
try:
driver = driver_instance_cache.get(
device_uuid, filter_fields=driver_filter_fields, address=address, port=port, settings=settings)
if os.getenv('QKD_API_URL'): # Assume real QKD system if QKD_API_URL is set
if not driver.Connect():
raise Exception("Failed to connect to real QKD system")
else:
driver.Connect()
return driver
except Exception as e:
app.logger.error(f"Failed to get driver instance for device {device_uuid}: {e}")
return None
def get_device_driver_info(driver: _Driver):
try:
return {
'initial_config': driver.GetInitialConfig(),
'running_config': driver.GetConfig(),
'subscriptions': list(driver.GetState(blocking=False))
}
except Exception as e:
app.logger.error(f"Failed to retrieve driver information: {e}")
return {'error': str(e)}
@app.route('/qkd/devices', methods=['GET'])
def retrieve_qkd_devices():
try:
context_info = get_context_topology_info()
return jsonify(context_info), 200
except Exception as e:
app.logger.error(f"Error retrieving QKD devices: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
@app.route('/qkd/capabilities', methods=['GET'])
def get_qkd_capabilities():
try:
context_info = get_context_topology_info()
for context in context_info:
if context['context_name'] == 'admin':
for topology in context['topologies']:
if topology['topology_name'] == 'admin':
for device in topology['devices']:
driver = get_driver_instance(device)
if driver:
capabilities = driver.get_qkd_capabilities()
return jsonify(capabilities), 200
abort(404, description="No capabilities found")
except Exception as e:
app.logger.error(f"Error retrieving QKD capabilities: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
@app.route('/qkd/interfaces', methods=['GET'])
def get_qkd_interfaces():
try:
context_info = get_context_topology_info()
for context in context_info:
if context['context_name'] == 'admin':
for topology in context['topologies']:
if topology['topology_name'] == 'admin':
for device in topology['devices']:
driver = get_driver_instance(device)
if driver:
interfaces = driver.get_qkd_interfaces()
return jsonify(interfaces), 200
abort(404, description="No interfaces found")
except Exception as e:
app.logger.error(f"Error retrieving QKD interfaces: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
@app.route('/qkd/links', methods=['GET'])
def get_qkd_links():
try:
context_info = get_context_topology_info()
for context in context_info:
if context['context_name'] == 'admin':
for topology in context['topologies']:
if topology['topology_name'] == 'admin':
for device in topology['devices']:
driver = get_driver_instance(device)
if driver:
links = driver.get_qkd_links()
return jsonify(links), 200
abort(404, description="No links found")
except Exception as e:
app.logger.error(f"Error retrieving QKD links: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
@app.route('/qkd/applications', methods=['GET'])
def get_qkd_applications():
try:
context_info = get_context_topology_info()
for context in context_info:
if context['context_name'] == 'admin':
for topology in context['topologies']:
if topology['topology_name'] == 'admin':
for device in topology['devices']:
driver = get_driver_instance(device)
if driver:
applications = driver.get_qkd_applications()
return jsonify(applications), 200
abort(404, description="No applications found")
except Exception as e:
app.logger.error(f"Error retrieving QKD applications: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
......@@ -180,7 +180,7 @@ if LOAD_ALL_DEVICE_DRIVERS:
]))
if LOAD_ALL_DEVICE_DRIVERS:
from .qkd.QKDDriver import QKDDriver # pylint: disable=wrong-import-position
from .qkd.QKDDriver2 import QKDDriver # pylint: disable=wrong-import-position
DRIVERS.append(
(QKDDriver, [
{
......
import grpc
import json
import requests
from common.proto.context_pb2 import DeviceId, DeviceOperationalStatusEnum, ConfigRule, ConfigRule_Custom, Empty, ContextId, TopologyId, Uuid, ConfigActionEnum
from common.proto.context_pb2_grpc import ContextServiceStub
from common.DeviceTypes import DeviceTypeEnum
from common.tools.grpc.ConfigRules import update_config_rule_custom
def login_qkd(address, port, username, password):
url = f"http://{address}:{port}/login"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'username': username, 'password': password}
response = requests.post(url, headers=headers, data=data)
response.raise_for_status()
token = response.json().get('access_token')
return token
def fetch_qkd_info(address, port, token, endpoint):
url = f"http://{address}:{port}{endpoint}"
headers = {'Authorization': f'Bearer {token}', 'accept': 'application/json'}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def create_config_file(qkd_devices, filename):
json_structure = {
"contexts": [
{"context_id": {"context_uuid": {"uuid": "admin"}}}
],
"topologies": [
{"topology_id": {"topology_uuid": {"uuid": "admin"}, "context_id": {"context_uuid": {"uuid": "admin"}}}}
],
"devices": [],
"links": []
}
for device in qkd_devices:
device_entry = {
"device_id": {"device_uuid": {"uuid": device["uuid"]}},
"device_type": "qkd-node",
"device_operational_status": DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED,
"device_drivers": [12], # Assuming 12 is the correct driver ID for QKD
"device_endpoints": [],
"device_config": {"config_rules": [
{"action": ConfigActionEnum.CONFIGACTION_SET, "custom": {"resource_key": "_connect/address", "resource_value": device["address"]}},
{"action": ConfigActionEnum.CONFIGACTION_SET, "custom": {"resource_key": "_connect/port", "resource_value": str(device["port"])}},
{"action": ConfigActionEnum.CONFIGACTION_SET, "custom": {"resource_key": "_connect/settings", "resource_value": json.dumps({"scheme": "http", "token": device["token"]})}}
]}
}
json_structure["devices"].append(device_entry)
for interface in device["interfaces"]["qkd_interface"]:
endpoint_id = f"{device['address']}:{interface['qkdi_id']}"
device_entry["device_endpoints"].append({
"device_id": {"device_uuid": {"uuid": device["uuid"]}},
"endpoint_uuid": {"uuid": endpoint_id}
})
for link in device["links"]["qkd_links"]:
link_entry = {
"link_id": {"link_uuid": {"uuid": link["qkdl_id"]}},
"link_endpoint_ids": [
{"device_id": {"device_uuid": {"uuid": device["uuid"]}}, "endpoint_uuid": {"uuid": f"{device['address']}:{link['qkdl_id']}"}}
# You need to fetch and add the other endpoint details similarly
]
}
json_structure["links"].append(link_entry)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(json_structure, f, ensure_ascii=False, indent=4)
def main():
qkd_devices = [
{
"uuid": "real_qkd1",
"address": "10.13.13.2",
"port": 5100,
"username": "admin",
"password": "password"
}
# Add more QKD devices if needed
]
# Step 1: Authenticate and get the JWT tokens for each QKD device
for qkd_device in qkd_devices:
qkd_device['token'] = login_qkd(
qkd_device['address'], qkd_device['port'], qkd_device['username'], qkd_device['password'])
# Step 2: Fetch QKD device information
for qkd_device in qkd_devices:
qkd_device['capabilities'] = fetch_qkd_info(qkd_device['address'], qkd_device['port'], qkd_device['token'], '/restconf/data/etsi-qkd-sdn-node:qkd_node/qkdn_capabilities')
qkd_device['interfaces'] = fetch_qkd_info(qkd_device['address'], qkd_device['port'], qkd_device['token'], '/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_interfaces')
qkd_device['links'] = fetch_qkd_info(qkd_device['address'], qkd_device['port'], qkd_device['token'], '/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_links')
qkd_device['applications'] = fetch_qkd_info(qkd_device['address'], qkd_device['port'], qkd_device['token'], '/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_applications')
# Step 3: Create config files for each QKD device
config_filename = "qkd_devices_config.json"
create_config_file(qkd_devices, config_filename)
print(f"Config file created: {config_filename}")
if __name__ == "__main__":
main()
......@@ -26,6 +26,8 @@ class QKDDriver(_Driver):
self.__qkd_root = '{:s}://{:s}:{:d}'.format(scheme, self.address, int(self.port))
self.__timeout = int(self.settings.get('timeout', 120))
self.__node_ids = set(self.settings.get('node_ids', []))
token = self.settings.get('token')
self.__headers = {'Authorization': 'Bearer ' + token}
self.__initial_data = None
def Connect(self) -> bool:
......@@ -34,7 +36,11 @@ class QKDDriver(_Driver):
if self.__started.is_set(): return True
r = None
try:
r = requests.get(url, timeout=self.__timeout, verify=False, auth=self.__auth)
LOGGER.info(f'requests.get("{url}", timeout={self.__timeout}, verify=False, auth={self.__auth}, headers={self.__headers})')
r = requests.get(url, timeout=self.__timeout, verify=False, auth=self.__auth, headers=self.__headers)
LOGGER.info(f'R: {r}')
LOGGER.info(f'Text: {r.text}')
LOGGER.info(f'Json: {r.json()}')
except requests.exceptions.Timeout:
LOGGER.exception('Timeout connecting {:s}'.format(str(self.__qkd_root)))
return False
......@@ -67,7 +73,7 @@ class QKDDriver(_Driver):
chk_string(str_resource_name, resource_key, allow_empty=False)
results.extend(config_getter(
self.__qkd_root, resource_key, timeout=self.__timeout, auth=self.__auth,
node_ids=self.__node_ids))
node_ids=self.__node_ids, headers=self.__headers))
return results
......@@ -97,7 +103,7 @@ class QKDDriver(_Driver):
data = create_connectivity_link(
self.__qkd_root, link_uuid, node_id_src, interface_id_src, node_id_dst, interface_id_dst,
virt_prev_hop, virt_next_hops, virt_bandwidth,
timeout=self.__timeout, auth=self.__auth
timeout=self.__timeout, auth=self.__auth, headers=self.__headers
)
#data = create_connectivity_link(
......
import os
import json
import logging
import requests
import threading
from requests.auth import HTTPBasicAuth
from typing import Any, List, Optional, Tuple, Union
from common.method_wrappers.Decorator import MetricsPool, metered_subclass_method
from common.type_checkers.Checkers import chk_string, chk_type
from device.service.driver_api._Driver import _Driver
from .Tools2 import config_getter, create_connectivity_link
LOGGER = logging.getLogger(__name__)
DRIVER_NAME = 'qkd'
METRICS_POOL = MetricsPool('Device', 'Driver', labels={'driver': DRIVER_NAME})
class QKDDriver(_Driver):
def __init__(self, address: str, port: int, **settings) -> None:
LOGGER.info(f"Initializing QKDDriver with address={address}, port={port}, settings={settings}")
super().__init__(DRIVER_NAME, address, port, **settings)
self.__lock = threading.Lock()
self.__started = threading.Event()
self.__terminate = threading.Event()
self.__auth = None
self.__headers = {}
self.__qkd_root = os.getenv('QKD_API_URL', '{:s}://{:s}:{:d}'.format(settings.get('scheme', 'http'), self.address, int(self.port)))
self.__timeout = int(self.settings.get('timeout', 120))
self.__node_ids = set(self.settings.get('node_ids', []))
self.__initial_data = None
# Authentication settings
self.__username = settings.get('username')
self.__password = settings.get('password')
self.__use_jwt = settings.get('use_jwt', True) # Default to True if JWT is required
self.__token = settings.get('token')
if self.__token:
self.__headers = {'Authorization': 'Bearer ' + self.__token}
elif self.__username and self.__password:
self.__auth = HTTPBasicAuth(self.__username, self.__password)
LOGGER.info(f"QKDDriver initialized with QKD root URL: {self.__qkd_root}")
def authenticate(self) -> bool:
if self.__use_jwt and not self.__token:
return self.__authenticate_with_jwt()
return True
def __authenticate_with_jwt(self) -> bool:
login_url = f'{self.__qkd_root}/login'
payload = {'username': self.__username, 'password': self.__password}
try:
LOGGER.info(f'Attempting to authenticate with JWT at {login_url}')
response = requests.post(login_url, data=payload, timeout=self.__timeout)
response.raise_for_status()
token = response.json().get('access_token')
if not token:
LOGGER.error('Failed to retrieve access token')
return False
self.__token = token # Store the token
self.__headers = {'Authorization': f'Bearer {token}'}
LOGGER.info('JWT authentication successful')
return True
except requests.exceptions.RequestException as e:
LOGGER.exception(f'JWT authentication failed: {e}')
return False
def Connect(self) -> bool:
url = self.__qkd_root + '/restconf/data/etsi-qkd-sdn-node:qkd_node'
with self.__lock:
LOGGER.info(f"Starting connection to {url}")
if self.__started.is_set():
LOGGER.info("Already connected, skipping re-connection.")
return True
try:
if not self.__headers and not self.__auth:
LOGGER.info("No headers or auth found, calling authenticate.")
if not self.authenticate():
return False
LOGGER.info(f'Attempting to connect to {url} with headers {self.__headers} and timeout {self.__timeout}')
response = requests.get(url, timeout=self.__timeout, verify=False, headers=self.__headers, auth=self.__auth)
LOGGER.info(f'Received response: {response.status_code}, content: {response.text}')
response.raise_for_status()
self.__initial_data = response.json()
self.__started.set()
LOGGER.info('Connection successful')
return True
except requests.exceptions.RequestException as e:
LOGGER.error(f'Connection failed: {e}')
return False
def Disconnect(self) -> bool:
LOGGER.info("Disconnecting QKDDriver")
with self.__lock:
self.__terminate.set()
LOGGER.info("QKDDriver disconnected successfully")
return True
@metered_subclass_method(METRICS_POOL)
def GetInitialConfig(self) -> List[Tuple[str, Any]]:
LOGGER.info("Getting initial configuration")
with self.__lock:
if isinstance(self.__initial_data, dict):
initial_config = [('qkd_node', self.__initial_data.get('qkd_node', {}))]
LOGGER.info(f"Initial configuration: {initial_config}")
return initial_config
LOGGER.warning("Initial data is not a dictionary")
return []
@metered_subclass_method(METRICS_POOL)
def GetConfig(self, resource_keys: List[str] = []) -> List[Tuple[str, Union[Any, None, Exception]]]:
chk_type('resources', resource_keys, list)
LOGGER.info(f"Getting configuration for resource_keys: {resource_keys}")
results = []
with self.__lock:
if not resource_keys:
resource_keys = ['capabilities', 'interfaces', 'links', 'endpoints', 'apps']
for i, resource_key in enumerate(resource_keys):
chk_string(f'resource_key[{i}]', resource_key, allow_empty=False)
LOGGER.info(f"Retrieving resource key: {resource_key}")
resource_results = config_getter(
self.__qkd_root, resource_key, timeout=self.__timeout, headers=self.__headers, auth=self.__auth,
node_ids=self.__node_ids)
results.extend(resource_results)
LOGGER.info(f"Resource results for {resource_key}: {resource_results}")
LOGGER.info(f"Final configuration results: {results}")
return results
@metered_subclass_method(METRICS_POOL)
def SetConfig(self, resources: List[Tuple[str, Any]]) -> List[Union[bool, Exception]]:
LOGGER.info(f"Setting configuration for resources: {resources}")
results = []
if not resources:
LOGGER.warning("No resources provided for SetConfig")
return results
with self.__lock:
for resource_key, resource_value in resources:
LOGGER.info(f'Processing resource_key: {resource_key}, resource_value: {resource_value}')
if resource_key.startswith('/link'):
try:
if not isinstance(resource_value, dict):
raise TypeError(f"Expected dictionary but got {type(resource_value).__name__}")
link_uuid = resource_value.get('uuid')
node_id_src = resource_value.get('src_qkdn_id')
interface_id_src = resource_value.get('src_interface_id')
node_id_dst = resource_value.get('dst_qkdn_id')
interface_id_dst = resource_value.get('dst_interface_id')
virt_prev_hop = resource_value.get('virt_prev_hop')
virt_next_hops = resource_value.get('virt_next_hops')
virt_bandwidth = resource_value.get('virt_bandwidth')
LOGGER.info(f"Creating connectivity link with UUID: {link_uuid}")
create_connectivity_link(
self.__qkd_root, link_uuid, node_id_src, interface_id_src, node_id_dst, interface_id_dst,
virt_prev_hop, virt_next_hops, virt_bandwidth,
headers=self.__headers, timeout=self.__timeout, auth=self.__auth
)
results.append(True)
LOGGER.info(f"Connectivity link {link_uuid} created successfully")
except Exception as e:
LOGGER.exception(f'Unhandled error processing resource_key({resource_key})')
results.append(e)
else:
LOGGER.error(f'Invalid resource key detected: {resource_key}')
results.append(ValueError(f'Invalid resource key: {resource_key}'))
LOGGER.info(f"SetConfig results: {results}")
return results
@metered_subclass_method(METRICS_POOL)
def DeleteConfig(self, resources: List[Tuple[str, Any]]) -> List[Union[bool, Exception]]:
LOGGER.info(f"Deleting configuration for resources: {resources}")
results = []
if not resources:
LOGGER.warning("No resources provided for DeleteConfig")
return results
with self.__lock:
for resource in resources:
LOGGER.info(f'Resource to delete: {resource}')
uuid = resource[1].get('uuid')
if uuid:
LOGGER.info(f'Resource with UUID {uuid} deleted successfully')
results.append(True)
else:
LOGGER.warning(f"UUID not found in resource: {resource}")
results.append(False)
LOGGER.info(f"DeleteConfig results: {results}")
return results
@metered_subclass_method(METRICS_POOL)
def SubscribeState(self, subscriptions: List[Tuple[str, float, float]]) -> List[Union[bool, Exception]]:
LOGGER.info(f"Subscribing to state updates: {subscriptions}")
results = [True for _ in subscriptions]
LOGGER.info(f"Subscription results: {results}")
return results
@metered_subclass_method(METRICS_POOL)
def UnsubscribeState(self, subscriptions: List[Tuple[str, float, float]]) -> List[Union[bool, Exception]]:
LOGGER.info(f"Unsubscribing from state updates: {subscriptions}")
results = [True for _ in subscriptions]
LOGGER.info(f"Unsubscription results: {results}")
return results
@metered_subclass_method(METRICS_POOL)
def GetState(self, blocking=False, terminate: Optional[threading.Event] = None) -> Union[dict, list]:
LOGGER.info(f"GetState called with blocking={blocking}, terminate={terminate}")
url = self.__qkd_root + '/restconf/data/etsi-qkd-sdn-node:qkd_node'
try:
LOGGER.info(f"Making GET request to {url} to retrieve state")
response = requests.get(url, timeout=self.__timeout, verify=False, headers=self.__headers, auth=self.__auth)
LOGGER.info(f"Received state response: {response.status_code}, content: {response.text}")
response.raise_for_status()
state_data = response.json()
LOGGER.info(f"State data retrieved: {state_data}")
return state_data
except requests.exceptions.Timeout:
LOGGER.error(f'Timeout getting state from {self.__qkd_root}')
return []
except Exception as e:
LOGGER.error(f'Exception getting state from {self.__qkd_root}: {e}')
return []
......@@ -21,7 +21,7 @@ def find_key(resource, key):
def config_getter(
root_url : str, resource_key : str, auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None,
node_ids : Set[str] = set()
node_ids : Set[str] = set(), headers={}
):
# getting endpoints
......@@ -33,7 +33,7 @@ def config_getter(
try:
if resource_key in [RESOURCE_ENDPOINTS, RESOURCE_INTERFACES]:
url += 'qkd_interfaces/'
r = requests.get(url, timeout=timeout, verify=False, auth=auth)
r = requests.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
interfaces = r.json()['qkd_interfaces']['qkd_interface']
# If it's a physical endpoint
......@@ -73,7 +73,7 @@ def config_getter(
elif resource_key in [RESOURCE_LINKS, RESOURCE_NETWORK_INSTANCES]:
url += 'qkd_links/'
r = requests.get(url, timeout=timeout, verify=False, auth=auth)
r = requests.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
links = r.json()['qkd_links']['qkd_link']
if resource_key == RESOURCE_LINKS:
......@@ -93,7 +93,7 @@ def config_getter(
elif resource_key == RESOURCE_APPS:
url += 'qkd_applications/'
r = requests.get(url, timeout=timeout, verify=False, auth=auth)
r = requests.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
apps = r.json()['qkd_applications']['qkd_app']
for app in apps:
......@@ -103,13 +103,13 @@ def config_getter(
elif resource_key == RESOURCE_CAPABILITES:
url += 'qkdn_capabilities/'
r = requests.get(url, timeout=timeout, verify=False, auth=auth)
r = requests.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
capabilities = r.json()['qkdn_capabilities']
result.append((resource_key, capabilities))
elif resource_key == RESOURCE_NODE:
r = requests.get(url, timeout=timeout, verify=False, auth=auth)
r = requests.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
node = r.json()['qkd_node']
result.append((resource_key, node))
......@@ -128,7 +128,7 @@ def config_getter(
def create_connectivity_link(
root_url, link_uuid, node_id_src, interface_id_src, node_id_dst, interface_id_dst,
virt_prev_hop = None, virt_next_hops = None, virt_bandwidth = None,
auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None
auth : Optional[HTTPBasicAuth] = None, timeout : Optional[int] = None, headers={}
):
url = root_url + '/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_links/'
......@@ -155,5 +155,5 @@ def create_connectivity_link(
data = {'qkd_links': {'qkd_link': [qkd_link]}}
requests.post(url, json=data)
requests.post(url, json=data, headers=headers)
import json
import logging
import requests
from requests.auth import HTTPBasicAuth
from typing import Dict, Optional, Set, List, Tuple, Union, Any
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
LOGGER = logging.getLogger(__name__)
HTTP_OK_CODES = {
200, # OK
201, # Created
202, # Accepted
204, # No Content
}
def get_request_session(retries=5, backoff_factor=1.0, status_forcelist=(500, 502, 504)):
"""
Creates a requests session with retries and backoff strategy.
"""
LOGGER.info(f"Creating request session with retries={retries}, backoff_factor={backoff_factor}, status_forcelist={status_forcelist}")
session = requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
LOGGER.info("Request session created successfully")
return session
def find_key(resource, key):
"""
Extracts a specific key from a JSON resource.
"""
return json.loads(resource[1])[key]
def verify_endpoint_existence(session, endpoint_uuid, root_url, headers):
"""
Verifies if the given endpoint exists.
"""
url = f"{root_url}/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_interfaces/qkd_interface={endpoint_uuid}"
r = session.get(url, headers=headers)
if r.status_code == 200 and r.json():
return True
else:
LOGGER.error(f"Endpoint {endpoint_uuid} does not exist or is not accessible")
return False
def config_getter(
root_url: str, resource_key: str, auth: Optional[HTTPBasicAuth] = None, timeout: Optional[int] = None,
node_ids: Set[str] = set(), headers: Dict[str, str] = {}
) -> List[Tuple[str, Union[Dict[str, Any], Exception]]]:
"""
Fetches configuration data from a QKD node for a specified resource key.
Returns a list of tuples containing the resource key and the corresponding data or exception.
"""
url = f"{root_url}/restconf/data/etsi-qkd-sdn-node:qkd_node/"
result = []
session = get_request_session()
LOGGER.info(f"Starting config_getter with root_url={root_url}, resource_key={resource_key}, headers={headers}")
try:
if resource_key in ['endpoints', '__endpoints__', 'interfaces']:
url += 'qkd_interfaces/'
LOGGER.info(f"Making GET request to {url} with headers: {headers}")
r = session.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response: {r.status_code}, content: {r.text}")
r.raise_for_status()
interfaces = r.json().get('qkd_interfaces', {}).get('qkd_interface', [])
if not interfaces:
raise KeyError('qkd_interfaces')
for interface in interfaces:
if resource_key in ['endpoints', '__endpoints__']:
endpoint_uuid = f"{interface['qkdi_att_point'].get('device', 'N/A')}:{interface['qkdi_att_point'].get('port', 'N/A')}"
resource_key_with_uuid = f"/endpoints/endpoint[{endpoint_uuid}]"
interface['uuid'] = endpoint_uuid
result.append((resource_key_with_uuid, interface))
else:
interface_uuid = f"{interface['qkdi_att_point'].get('device', 'N/A')}:{interface['qkdi_att_point'].get('port', 'N/A')}"
interface['uuid'] = interface_uuid
interface['name'] = interface_uuid
interface['enabled'] = True
resource_key_with_uuid = f"/interface[{interface['qkdi_id']}]"
result.append((resource_key_with_uuid, interface))
elif resource_key in ['links', '__links__', '__network_instances__', 'network_instances']:
url += 'qkd_links/'
LOGGER.info(f"Making GET request to {url} with headers: {headers}")
r = session.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response: {r.status_code}, content: {r.text}")
r.raise_for_status()
links = r.json().get('qkd_links', {}).get('qkd_link', [])
if not links:
LOGGER.warning(f"No links found in the response for 'qkd_links'")
for link in links:
link_type = link.get('qkdl_type', 'Direct')
if resource_key == 'links':
if link_type == 'Direct':
resource_key_with_uuid = f"/link[{link['qkdl_id']}]"
result.append((resource_key_with_uuid, link))
else:
if link_type == 'Virtual':
resource_key_with_uuid = f"/service[{link['qkdl_id']}]"
result.append((resource_key_with_uuid, link))
elif resource_key in ['apps', '__apps__']:
url += 'qkd_applications/'
LOGGER.info(f"Making GET request to {url} with headers: {headers}")
r = session.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response: {r.status_code}, content: {r.text}")
r.raise_for_status()
apps = r.json().get('qkd_applications', {}).get('qkd_app', [])
if not apps:
raise KeyError('qkd_applications')
for app in apps:
app_resource_key = f"/app[{app['app_id']}]"
result.append((app_resource_key, app))
elif resource_key in ['capabilities', '__capabilities__']:
url += 'qkdn_capabilities/'
LOGGER.info(f"Making GET request to {url} with headers: {headers}")
r = session.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response: {r.status_code}, content: {r.text}")
r.raise_for_status()
capabilities = r.json()
result.append((resource_key, capabilities))
elif resource_key in ['node', '__node__']:
LOGGER.info(f"Making GET request to {url} with headers: {headers}")
r = session.get(url, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response: {r.status_code}, content: {r.text}")
r.raise_for_status()
node = r.json().get('qkd_node', {})
result.append((resource_key, node))
else:
LOGGER.warning(f"Unknown resource key: {resource_key}")
result.append((resource_key, ValueError(f"Unknown resource key: {resource_key}")))
except requests.exceptions.RequestException as e:
LOGGER.error(f'Exception retrieving/parsing {resource_key} from {url}: {e}')
result.append((resource_key, e))
LOGGER.info(f"config_getter results for {resource_key}: {result}")
return result
def create_connectivity_link(
root_url: str, link_uuid: str, node_id_src: str, interface_id_src: str, node_id_dst: str, interface_id_dst: str,
virt_prev_hop: Optional[str] = None, virt_next_hops: Optional[List[str]] = None, virt_bandwidth: Optional[int] = None,
auth: Optional[HTTPBasicAuth] = None, timeout: Optional[int] = None, headers: Dict[str, str] = {}
) -> Union[bool, Exception]:
"""
Creates a connectivity link between QKD nodes using the provided parameters.
"""
url = f"{root_url}/restconf/data/etsi-qkd-sdn-node:qkd_node/qkd_links/"
session = get_request_session()
# Verify that endpoints exist before creating the link
if not (verify_endpoint_existence(session, interface_id_src, root_url, headers) and
verify_endpoint_existence(session, interface_id_dst, root_url, headers)):
LOGGER.error(f"Cannot create link {link_uuid} because one or both endpoints do not exist.")
return Exception(f"Endpoint verification failed for link {link_uuid}")
is_virtual = bool(virt_prev_hop or virt_next_hops)
qkd_link = {
'qkdl_id': link_uuid,
'qkdl_type': 'etsi-qkd-node-types:' + ('VIRT' if is_virtual else 'PHYS'),
'qkdl_local': {
'qkdn_id': node_id_src,
'qkdi_id': interface_id_src
},
'qkdl_remote': {
'qkdn_id': node_id_dst,
'qkdi_id': interface_id_dst
}
}
if is_virtual:
qkd_link['virt_prev_hop'] = virt_prev_hop
qkd_link['virt_next_hop'] = virt_next_hops or []
qkd_link['virt_bandwidth'] = virt_bandwidth
data = {'qkd_links': {'qkd_link': [qkd_link]}}
LOGGER.info(f"Creating connectivity link with payload: {json.dumps(data)}")
try:
r = session.post(url, json=data, timeout=timeout, verify=False, auth=auth, headers=headers)
LOGGER.info(f"Received response for link creation: {r.status_code}, content: {r.text}")
r.raise_for_status()
if r.status_code in HTTP_OK_CODES:
LOGGER.info(f"Link {link_uuid} created successfully.")
return True
else:
LOGGER.error(f"Failed to create link {link_uuid}, status code: {r.status_code}")
return False
except requests.exceptions.RequestException as e:
LOGGER.error(f"Exception creating link {link_uuid} with payload {json.dumps(data)}: {e}")
return e
import pytest
import json
import logging
import os
from dotenv import load_dotenv
from src.device.service.drivers.qkd.QKDDriver import QKDDriver
# Load environment variables from .env file
load_dotenv()
# Set up logging
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
class SafeJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Exception):
return {'error': str(obj), 'type': type(obj).__name__}
return super().default(obj)
# Dictionary to store retrieved information
retrieved_info = {
"config_qkd1": None,
"config_qkd2": None,
"capabilities_qkd1": None,
"capabilities_qkd2": None,
"interfaces_qkd1": None,
"interfaces_qkd2": None,
"links_qkd1": None,
"links_qkd2": None,
"state_qkd1": None,
"state_qkd2": None,
}
# Environment variables for sensitive information
QKD1_ADDRESS = os.getenv("QKD1_ADDRESS")
QKD2_ADDRESS = os.getenv("QKD2_ADDRESS")
PORT = os.getenv("QKD_PORT")
USERNAME = os.getenv("QKD_USERNAME")
PASSWORD = os.getenv("QKD_PASSWORD")
@pytest.fixture
def driver_qkd1():
return QKDDriver(address=QKD1_ADDRESS, port=PORT, username=USERNAME, password=PASSWORD, use_jwt=True)
@pytest.fixture
def driver_qkd2():
return QKDDriver(address=QKD2_ADDRESS, port=PORT, username=USERNAME, password=PASSWORD, use_jwt=True)
def log_data(label, data):
"""Logs data in JSON format with a label."""
LOGGER.info(f"{label}: {json.dumps(data, indent=2, cls=SafeJSONEncoder)}")
def get_jwt_token(driver):
"""Retrieve JWT token from the driver."""
try:
return driver._QKDDriver__headers.get('Authorization').split(' ')[1]
except (AttributeError, KeyError, TypeError):
LOGGER.error("Failed to retrieve JWT token")
return None
def save_json_file(filename, data):
"""Save data to a JSON file."""
try:
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
LOGGER.info(f"Successfully saved {filename}")
except Exception as e:
LOGGER.error(f"Failed to save {filename}: {e}")
def retrieve_data(driver, label, method, *args):
"""Retrieve data from the driver and log it."""
try:
data = method(*args)
log_data(label, data)
return data
except Exception as e:
LOGGER.error(f"Failed to retrieve {label}: {e}")
return None
def test_retrieve_and_create_descriptor(driver_qkd1, driver_qkd2):
# Connect to both QKD nodes
assert driver_qkd1.Connect(), "Failed to connect to QKD1"
assert driver_qkd2.Connect(), "Failed to connect to QKD2"
# Use the same JWT token for all requests
jwt_token = get_jwt_token(driver_qkd1)
assert jwt_token, "Failed to retrieve JWT token from QKD1"
driver_qkd2._QKDDriver__headers['Authorization'] = f'Bearer {jwt_token}'
# Retrieve configurations
retrieved_info['config_qkd1'] = retrieve_data(driver_qkd1, "QKD1 Initial Config", driver_qkd1.GetInitialConfig)
retrieved_info['config_qkd2'] = retrieve_data(driver_qkd2, "QKD2 Initial Config", driver_qkd2.GetInitialConfig)
# Retrieve capabilities
retrieved_info['capabilities_qkd1'] = retrieve_data(driver_qkd1, "QKD1 Capabilities", driver_qkd1.GetConfig, ['capabilities'])
retrieved_info['capabilities_qkd2'] = retrieve_data(driver_qkd2, "QKD2 Capabilities", driver_qkd2.GetConfig, ['capabilities'])
# Retrieve interfaces
retrieved_info['interfaces_qkd1'] = retrieve_data(driver_qkd1, "QKD1 Interfaces", driver_qkd1.GetConfig, ['interfaces'])
retrieved_info['interfaces_qkd2'] = retrieve_data(driver_qkd2, "QKD2 Interfaces", driver_qkd2.GetConfig, ['interfaces'])
# Retrieve links
retrieved_info['links_qkd1'] = retrieve_data(driver_qkd1, "QKD1 Links", driver_qkd1.GetConfig, ['links'])
retrieved_info['links_qkd2'] = retrieve_data(driver_qkd2, "QKD2 Links", driver_qkd2.GetConfig, ['links'])
# Retrieve states
retrieved_info['state_qkd1'] = retrieve_data(driver_qkd1, "QKD1 Current State", driver_qkd1.GetState)
retrieved_info['state_qkd2'] = retrieve_data(driver_qkd2, "QKD2 Current State", driver_qkd2.GetState)
# Save retrieved information
save_json_file('retrieved_info.json', retrieved_info)
# Create descriptor dynamically
descriptor = {
"contexts": [{"context_id": {"context_uuid": {"uuid": "admin"}}}],
"topologies": [{"topology_id": {"topology_uuid": {"uuid": "admin"}, "context_id": {"context_uuid": {"uuid": "admin"}}}}],
"devices": [],
"links": []
}
# Add device information to descriptor
for config, token, interfaces, device_name, address in [
(retrieved_info['config_qkd1'], jwt_token, retrieved_info['interfaces_qkd1'], "QKD1", QKD1_ADDRESS),
(retrieved_info['config_qkd2'], jwt_token, retrieved_info['interfaces_qkd2'], "QKD2", QKD2_ADDRESS)
]:
device_info = {
"device_id": {"device_uuid": {"uuid": device_name}},
"device_type": "qkd-node",
"device_operational_status": 0,
"device_drivers": [12],
"device_endpoints": [],
"device_config": {
"config_rules": [
{"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": address}},
{"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": PORT}},
{"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"scheme": "http", "token": token}}}
]
}
}
descriptor['devices'].append(device_info)
# Create links based on retrieved link data
if retrieved_info['links_qkd1'] and retrieved_info['links_qkd2']:
for link_data in retrieved_info['links_qkd1']:
link_entry = {
"link_id": {"link_uuid": {"uuid": f"QKD1/{QKD1_ADDRESS}:{PORT}==QKD2/{retrieved_info['links_qkd2'][0][1]['qkdi_status']}/{PORT}"}},
"link_endpoint_ids": [
{"device_id": {"device_uuid": {"uuid": "QKD1"}}, "endpoint_uuid": {"uuid": f"{QKD1_ADDRESS}:{PORT}"}},
{"device_id": {"device_uuid": {"uuid": "QKD2"}}, "endpoint_uuid": {"uuid": f"{QKD2_ADDRESS}:{PORT}"}}
]
}
descriptor['links'].append(link_entry)
# Save the dynamically created descriptor
save_json_file('descriptor.json', descriptor)
log_data("Created Descriptor", descriptor)
import pytest
from src.device.service.drivers.qkd.QKDDriver2 import QKDDriver
def test_end_to_end_workflow():
driver = QKDDriver(address='10.211.36.220', port=11111, username='user', password='pass')
assert driver.Connect() is True
# Retrieve initial configuration
config = driver.GetInitialConfig()
assert isinstance(config, dict)
assert 'qkd_node' in config
# Define the new configuration
new_config = {'uuid': 'test', 'device': 'device1', 'port': 'port1'}
# Use a valid resource key based on driver implementation
valid_resource_key = '/link/valid_resource_key' # Adjust this key as necessary
try:
result = driver.SetConfig([(valid_resource_key, new_config)])
# Check for ValueErrors in results
if any(isinstance(res, ValueError) for res in result):
pytest.fail(f"SetConfig failed with error: {next(res for res in result if isinstance(res, ValueError))}")
# Ensure result is a list of booleans
assert isinstance(result, list)
assert all(isinstance(res, bool) for res in result)
assert all(result) # Ensure all operations succeeded
except Exception as e:
pytest.fail(f"SetConfig failed: {e}")
# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import libyang, os
from typing import Dict, Optional
YANG_DIR = os.path.join(os.path.dirname(__file__), 'yang')
class YangValidator:
def __init__(self, main_module : str, dependency_modules : [str]) -> None:
self._yang_context = libyang.Context(YANG_DIR)
self._yang_module = self._yang_context.load_module(main_module)
mods = [self._yang_context.load_module(mod) for mod in dependency_modules] + [self._yang_module]
for mod in mods:
mod.feature_enable_all()
def parse_to_dict(self, message : Dict) -> Dict:
dnode : Optional[libyang.DNode] = self._yang_module.parse_data_dict(
message, validate_present=True, validate=True, strict=True
)
if dnode is None: raise Exception('Unable to parse Message({:s})'.format(str(message)))
message = dnode.print_dict()
dnode.free()
return message
def destroy(self) -> None:
self._yang_context.destroy()
import os
from flask import Flask, request
from YangValidator import YangValidator
app = Flask(__name__)
yang_validator = YangValidator('etsi-qkd-sdn-node', ['etsi-qkd-node-types'])
nodes = {
'10.211.36.220:11111': {'node': {
'qkdn_id': '00000001-0000-0000-0000-000000000000',
},
'qkdn_capabilities': {
},
'qkd_applications': {
'qkd_app': [
{
'app_id': '00000001-0001-0000-0000-000000000000',
'client_app_id': [],
'app_statistics': {
'statistics': []
},
'app_qos': {
},
'backing_qkdl_id': []
}
]
},
'qkd_interfaces': {
'qkd_interface': [
{
'qkdi_id': '100',
'qkdi_att_point': {
},
'qkdi_capabilities': {
}
},
{
'qkdi_id': '101',
'qkdi_att_point': {
'device':'10.211.36.220',
'port':'1001'
},
'qkdi_capabilities': {
}
}
]
},
'qkd_links': {
'qkd_link': [
]
}
},
'10.211.36.220:22222': {'node': {
'qkdn_id': '00000002-0000-0000-0000-000000000000',
},
'qkdn_capabilities': {
},
'qkd_applications': {
'qkd_app': [
{
'app_id': '00000002-0001-0000-0000-000000000000',
'client_app_id': [],
'app_statistics': {
'statistics': []
},
'app_qos': {
},
'backing_qkdl_id': []
}
]
},
'qkd_interfaces': {
'qkd_interface': [
{
'qkdi_id': '200',
'qkdi_att_point': {
},
'qkdi_capabilities': {
}
},
{
'qkdi_id': '201',
'qkdi_att_point': {
'device':'10.211.36.220',
'port':'2001'
},
'qkdi_capabilities': {
}
},
{
'qkdi_id': '202',
'qkdi_att_point': {
'device':'10.211.36.220',
'port':'2002'
},
'qkdi_capabilities': {
}
}
]
},
'qkd_links': {
'qkd_link': [
]
}
},
'10.211.36.220:33333': {'node': {
'qkdn_id': '00000003-0000-0000-0000-000000000000',
},
'qkdn_capabilities': {
},
'qkd_applications': {
'qkd_app': [
{
'app_id': '00000003-0001-0000-0000-000000000000',
'client_app_id': [],
'app_statistics': {
'statistics': []
},
'app_qos': {
},
'backing_qkdl_id': []
}
]
},
'qkd_interfaces': {
'qkd_interface': [
{
'qkdi_id': '300',
'qkdi_att_point': {
},
'qkdi_capabilities': {
}
},
{
'qkdi_id': '301',
'qkdi_att_point': {
'device':'10.211.36.220',
'port':'3001'
},
'qkdi_capabilities': {
}
}
]
},
'qkd_links': {
'qkd_link': [
]
}
}
}
def get_side_effect(url):
steps = url.lstrip('https://').lstrip('http://').rstrip('/')
ip_port, _, _, header, *steps = steps.split('/')
header_splitted = header.split(':')
module = header_splitted[0]
assert(module == 'etsi-qkd-sdn-node')
tree = {'qkd_node': nodes[ip_port]['node'].copy()}
if len(header_splitted) == 1 or not header_splitted[1]:
value = nodes[ip_port].copy()
value.pop('node')
tree['qkd_node'].update(value)
return tree, tree
root = header_splitted[1]
assert(root == 'qkd_node')
if not steps:
return tree, tree
endpoint, *steps = steps
value = nodes[ip_port][endpoint]
if not steps:
return_value = {endpoint:value}
tree['qkd_node'].update(return_value)
return return_value, tree
'''
element, *steps = steps
container, key = element.split('=')
# value = value[container][key]
if not steps:
return_value['qkd_node'][endpoint] = [value]
return return_value
'''
raise Exception('Url too long')
def edit(from_dict, to_dict, create):
for key, value in from_dict.items():
if isinstance(value, dict):
if key not in to_dict and create:
to_dict[key] = {}
edit(from_dict[key], to_dict[key], create)
elif isinstance(value, list):
to_dict[key].extend(value)
else:
to_dict[key] = value
def edit_side_effect(url, json, create):
steps = url.lstrip('https://').lstrip('http://').rstrip('/')
ip_port, _, _, header, *steps = steps.split('/')
module, root = header.split(':')
assert(module == 'etsi-qkd-sdn-node')
assert(root == 'qkd_node')
if not steps:
edit(json, nodes[ip_port]['node'])
return
endpoint, *steps = steps
if not steps:
edit(json[endpoint], nodes[ip_port][endpoint], create)
return
'''
element, *steps = steps
container, key = element.split('=')
if not steps:
if key not in nodes[ip_port][endpoint][container] and create:
nodes[ip_port][endpoint][container][key] = {}
edit(json, nodes[ip_port][endpoint][container][key], create)
return 0
'''
raise Exception('Url too long')
@app.get('/', defaults={'path': ''})
@app.get("/<string:path>")
@app.get('/<path:path>')
def get(path):
msg, msg_validate = get_side_effect(request.base_url)
print(msg_validate)
yang_validator.parse_to_dict(msg_validate)
return msg
@app.post('/', defaults={'path': ''})
@app.post("/<string:path>")
@app.post('/<path:path>')
def post(path):
success = True
reason = ''
try:
edit_side_effect(request.base_url, request.json, True)
except Exception as e:
reason = str(e)
success = False
return {'success': success, 'reason': reason}
@app.route('/', defaults={'path': ''}, methods=['PUT', 'PATCH'])
@app.route("/<string:path>", methods=['PUT', 'PATCH'])
@app.route('/<path:path>', methods=['PUT', 'PATCH'])
def patch(path):
success = True
reason = ''
try:
edit_side_effect(request.base_url, request.json, False)
except Exception as e:
reason = str(e)
success = False
return {'success': success, 'reason': reason}
# import json
# from mock import requests
# import pyangbind.lib.pybindJSON as enc
# from pyangbind.lib.serialise import pybindJSONDecoder as dec
# from yang.sbi.qkd.templates.etsi_qkd_sdn_node import etsi_qkd_sdn_node
# module = etsi_qkd_sdn_node()
# url = 'https://1.1.1.1/restconf/data/etsi-qkd-sdn-node:'
# # Get node all info
# z = requests.get(url).json()
# var = dec.load_json(z, None, None, obj=module)
# print(enc.dumps(var))
# Reset module variable because it is already filled
# module = etsi_qkd_sdn_node()
# # Get node basic info
# node = module.qkd_node
# z = requests.get(url + 'qkd_node').json()
# var = dec.load_json(z, None, None, obj=node)
# print(enc.dumps(var))
# # Get all apps
# apps = node.qkd_applications
# z = requests.get(url + 'qkd_node/qkd_applications').json()
# var = dec.load_json(z, None, None, obj=apps)
# print(enc.dumps(var))
# # Edit app 0
# app = apps.qkd_app['00000000-0001-0000-0000-000000000000']
# app.client_app_id = 'id_0'
# requests.put(url + 'qkd_node/qkd_applications/qkd_app=00000000-0001-0000-0000-000000000000', json=json.loads(enc.dumps(app)))
# # Create app 1
# app = apps.qkd_app.add('00000000-0001-0000-0000-000000000001')
# requests.post(url + 'qkd_node/qkd_applications/qkd_app=00000000-0001-0000-0000-000000000001', json=json.loads(enc.dumps(app)))
# # Get all apps
# apps = node.qkd_applications
# z = requests.get(url + 'qkd_node/qkd_applications').json()
# var = dec.load_json(z, None, None, obj=apps)
# print(enc.dumps(var))
#!/bin/bash
cd "$(dirname "$0")"
#!/bin/bash
killbg() {
for p in "${pids[@]}" ; do
kill "$p";
done
}
trap killbg EXIT
pids=()
flask --app mock run --host 0.0.0.0 --port 11111 &
pids+=($!)
flask --app mock run --host 0.0.0.0 --port 22222 &
pids+=($!)
flask --app mock run --host 0.0.0.0 --port 33333
/* Copyright 2022 ETSI
Licensed under the BSD-3 Clause (https://forge.etsi.org/legal-matters) */
module etsi-qkd-node-types {
yang-version "1";
namespace "urn:etsi:qkd:yang:etsi-qkd-node-types";
prefix "etsi-qkdn-types";
organization "ETSI ISG QKD";
contact
"https://www.etsi.org/committee/qkd
vicente@fi.upm.es";
description
"This module contains the base types created for
the software-defined QKD node information models
specified in ETSI GS QKD 015 V2.1.1
- QKD-TECHNOLOGY-TYPES
- QKDN-STATUS-TYPES
- QKD-LINK-TYPES
- QKD-ROLE-TYPES
- QKD-APP-TYPES
- Wavelength
";
revision "2022-01-30" {
description
"Refinement of the YANG model to make it compatible with the ETSI ISG QKD 018. Minor fixes.";
}
revision "2020-09-30" {
description
"First definition based on initial requirement analysis.";
}
identity QKD-TECHNOLOGY-TYPES {
description "Quantum Key Distribution System base technology types.";
}
identity CV-QKD {
base QKD-TECHNOLOGY-TYPES;
description "Continuous Variable base technology.";
}
identity DV-QKD {
base QKD-TECHNOLOGY-TYPES;
description "Discrete Variable base technology.";
}
identity DV-QKD-COW {
base QKD-TECHNOLOGY-TYPES;
description "COW base technology.";
}
identity DV-QKD-2Ws {
base QKD-TECHNOLOGY-TYPES;
description "2-Ways base technology.";
}
typedef qkd-technology-types {
type identityref {
base QKD-TECHNOLOGY-TYPES;
}
description "This type represents the base technology types of the SD-QKD system.";
}
identity QKDN-STATUS-TYPES {
description "Base identity used to identify the SD-QKD node status.";
}
identity NEW {
base QKDN-STATUS-TYPES;
description "The QKD node is installed.";
}
identity OPERATING {
base QKDN-STATUS-TYPES;
description "The QKD node is up.";
}
identity DOWN {
base QKDN-STATUS-TYPES;
description "The QKD node is not working as expected.";
}
identity FAILURE {
base QKDN-STATUS-TYPES;
description "The QKD node cannot be accessed by SDN controller with communication failure.";
}
identity OUT {
base QKDN-STATUS-TYPES;
description "The QKD node is switched off and uninstalled.";
}
typedef qkdn-status-types {
type identityref {
base QKDN-STATUS-TYPES;
}
description "This type represents the status of the SD-QKD node.";
}
identity QKD-LINK-TYPES {
description "QKD key association link types.";
}
identity VIRT {
base QKD-LINK-TYPES;
description "Virtual Link.";
}
identity PHYS {
base QKD-LINK-TYPES;
description "Physical Link.";
}
typedef qkd-link-types {
type identityref {
base QKD-LINK-TYPES;
}
description "This type represents the key association link type between two SD-QKD nodes.";
}
identity QKD-ROLE-TYPES {
description "QKD Role Type.";
}
identity TRANSMITTER {
base QKD-ROLE-TYPES;
description "QKD module working as transmitter.";
}
identity RECEIVER {
base QKD-ROLE-TYPES;
description "QKD module working as receiver.";
}
identity TRANSCEIVER {
base QKD-ROLE-TYPES;
description "QKD System that can work as a transmitter or receiver.";
}
typedef qkd-role-types {
type identityref {
base QKD-ROLE-TYPES;
}
description "This type represents the working mode of a SD-QKD module.";
}
identity QKD-APP-TYPES {
description "Application types.";
}
identity CLIENT {
base QKD-APP-TYPES;
description "Application working as client.";
}
identity INTERNAL {
base QKD-APP-TYPES;
description "Internal QKD node application.";
}
typedef qkd-app-types {
type identityref {
base QKD-APP-TYPES;
}
description "This type represents the application class consuming key from SD-QKD nodes.";
}
identity PHYS-PERF-TYPES {
description "Physical performance types.";
}
identity QBER {
base PHYS-PERF-TYPES;
description "Quantum Bit Error Rate.";
}
identity SNR {
base PHYS-PERF-TYPES;
description "Signal to Noise Ratio.";
}
typedef phys-perf-types {
type identityref {
base PHYS-PERF-TYPES;
}
description "This type represents physical performance types.";
}
identity LINK-STATUS-TYPES {
description "Status of the key association QKD link (physical and virtual).";
}
identity ACTIVE {
base LINK-STATUS-TYPES;
description "Link actively generating keys.";
}
identity PASSIVE {
base LINK-STATUS-TYPES;
description "No key generation on key association QKD link but a pool of keys
are still available.";
}
identity PENDING {
base LINK-STATUS-TYPES;
description "Waiting for activation and no keys are available.";
}
identity OFF {
base LINK-STATUS-TYPES;
description "No key generation and no keys are available.";
}
typedef link-status-types {
type identityref {
base LINK-STATUS-TYPES;
}
description "This type represents the status of a key association QKD link, both physical and virtual.";
}
///
identity IFACE-STATUS-TYPES {
description "Interface Status.";
}
identity ENABLED {
base IFACE-STATUS-TYPES;
description "The interfaces is up.";
}
identity DISABLED {
base IFACE-STATUS-TYPES;
description "The interfaces is down.";
}
identity FAILED {
base IFACE-STATUS-TYPES;
description "The interfaces has failed.";
}
typedef iface-status-types {
type identityref {
base IFACE-STATUS-TYPES;
}
description "This type represents the status of a interface between a SD-QKD node and a SD-QKD module.";
}
identity APP-STATUS-TYPES {
description "Application types.";
}
identity ON {
base APP-STATUS-TYPES;
description "The application is on.";
}
identity DISCONNECTED {
base APP-STATUS-TYPES;
description "The application is disconnected.";
}
identity OUT-OF-TIME {
base APP-STATUS-TYPES;
description "The application is out of time.";
}
identity ZOMBIE {
base APP-STATUS-TYPES;
description "The application is in a zombie state.";
}
typedef app-status-types {
type identityref {
base APP-STATUS-TYPES;
}
description "This type represents the status of an application consuming key from SD-QKD nodes.";
}
identity SEVERITY-TYPES {
description "Error/Failure severity levels.";
}
identity MAJOR {
base SEVERITY-TYPES;
description "Major error/failure.";
}
identity MINOR {
base SEVERITY-TYPES;
description "Minor error/failure.";
}
typedef severity-types {
type identityref {
base SEVERITY-TYPES;
}
description "This type represents the Error/Failure severity levels.";
}
typedef wavelength {
type string {
pattern "([1-9][0-9]{0,3})";
}
description
"A WDM channel number (starting at 1). For example: 20";
}
//Pattern from "A Yang Data Model for WSON Optical Networks".
typedef wavelength-range-type {
type string {
pattern "([1-9][0-9]{0,3}(-[1-9][0-9]{0,3})?" +
"(,[1-9][0-9]{0,3}(-[1-9][0-9]{0,3})?)*)";
}
description
"A list of WDM channel numbers (starting at 1)
in ascending order. For example: 1,12-20,40,50-80";
}
}
module ietf-inet-types {
namespace "urn:ietf:params:xml:ns:yang:ietf-inet-types";
prefix "inet";
organization
"IETF NETMOD (NETCONF Data Modeling Language) Working Group";
contact
"WG Web: <http://tools.ietf.org/wg/netmod/>
WG List: <mailto:netmod@ietf.org>
WG Chair: David Kessens
<mailto:david.kessens@nsn.com>
WG Chair: Juergen Schoenwaelder
<mailto:j.schoenwaelder@jacobs-university.de>
Editor: Juergen Schoenwaelder
<mailto:j.schoenwaelder@jacobs-university.de>";
description
"This module contains a collection of generally useful derived
YANG data types for Internet addresses and related things.
Copyright (c) 2013 IETF Trust and the persons identified as
authors of the code. All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, is permitted pursuant to, and subject
to the license terms contained in, the Simplified BSD License
set forth in Section 4.c of the IETF Trust's Legal Provisions
Relating to IETF Documents
(http://trustee.ietf.org/license-info).
This version of this YANG module is part of RFC 6991; see
the RFC itself for full legal notices.";
revision 2013-07-15 {
description
"This revision adds the following new data types:
- ip-address-no-zone
- ipv4-address-no-zone
- ipv6-address-no-zone";
reference
"RFC 6991: Common YANG Data Types";
}
revision 2010-09-24 {
description
"Initial revision.";
reference
"RFC 6021: Common YANG Data Types";
}
/*** collection of types related to protocol fields ***/
typedef ip-version {
type enumeration {
enum unknown {
value "0";
description
"An unknown or unspecified version of the Internet
protocol.";
}
enum ipv4 {
value "1";
description
"The IPv4 protocol as defined in RFC 791.";
}
enum ipv6 {
value "2";
description
"The IPv6 protocol as defined in RFC 2460.";
}
}
description
"This value represents the version of the IP protocol.
In the value set and its semantics, this type is equivalent
to the InetVersion textual convention of the SMIv2.";
reference
"RFC 791: Internet Protocol
RFC 2460: Internet Protocol, Version 6 (IPv6) Specification
RFC 4001: Textual Conventions for Internet Network Addresses";
}
typedef dscp {
type uint8 {
range "0..63";
}
description
"The dscp type represents a Differentiated Services Code Point
that may be used for marking packets in a traffic stream.
In the value set and its semantics, this type is equivalent
to the Dscp textual convention of the SMIv2.";
reference
"RFC 3289: Management Information Base for the Differentiated
Services Architecture
RFC 2474: Definition of the Differentiated Services Field
(DS Field) in the IPv4 and IPv6 Headers
RFC 2780: IANA Allocation Guidelines For Values In
the Internet Protocol and Related Headers";
}
typedef ipv6-flow-label {
type uint32 {
range "0..1048575";
}
description
"The ipv6-flow-label type represents the flow identifier or Flow
Label in an IPv6 packet header that may be used to
discriminate traffic flows.
In the value set and its semantics, this type is equivalent
to the IPv6FlowLabel textual convention of the SMIv2.";
reference
"RFC 3595: Textual Conventions for IPv6 Flow Label
RFC 2460: Internet Protocol, Version 6 (IPv6) Specification";
}
typedef port-number {
type uint16 {
range "0..65535";
}
description
"The port-number type represents a 16-bit port number of an
Internet transport-layer protocol such as UDP, TCP, DCCP, or
SCTP. Port numbers are assigned by IANA. A current list of
all assignments is available from <http://www.iana.org/>.
Note that the port number value zero is reserved by IANA. In
situations where the value zero does not make sense, it can
be excluded by subtyping the port-number type.
In the value set and its semantics, this type is equivalent
to the InetPortNumber textual convention of the SMIv2.";
reference
"RFC 768: User Datagram Protocol
RFC 793: Transmission Control Protocol
RFC 4960: Stream Control Transmission Protocol
RFC 4340: Datagram Congestion Control Protocol (DCCP)
RFC 4001: Textual Conventions for Internet Network Addresses";
}
/*** collection of types related to autonomous systems ***/
typedef as-number {
type uint32;
description
"The as-number type represents autonomous system numbers
which identify an Autonomous System (AS). An AS is a set
of routers under a single technical administration, using
an interior gateway protocol and common metrics to route
packets within the AS, and using an exterior gateway
protocol to route packets to other ASes. IANA maintains
the AS number space and has delegated large parts to the
regional registries.
Autonomous system numbers were originally limited to 16
bits. BGP extensions have enlarged the autonomous system
number space to 32 bits. This type therefore uses an uint32
base type without a range restriction in order to support
a larger autonomous system number space.
In the value set and its semantics, this type is equivalent
to the InetAutonomousSystemNumber textual convention of
the SMIv2.";
reference
"RFC 1930: Guidelines for creation, selection, and registration
of an Autonomous System (AS)
RFC 4271: A Border Gateway Protocol 4 (BGP-4)
RFC 4001: Textual Conventions for Internet Network Addresses
RFC 6793: BGP Support for Four-Octet Autonomous System (AS)
Number Space";
}
/*** collection of types related to IP addresses and hostnames ***/
typedef ip-address {
type union {
type inet:ipv4-address;
type inet:ipv6-address;
}
description
"The ip-address type represents an IP address and is IP
version neutral. The format of the textual representation
implies the IP version. This type supports scoped addresses
by allowing zone identifiers in the address format.";
reference
"RFC 4007: IPv6 Scoped Address Architecture";
}
typedef ipv4-address {
type string {
pattern
'(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+ '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+ '(%[\p{N}\p{L}]+)?';
}
description
"The ipv4-address type represents an IPv4 address in
dotted-quad notation. The IPv4 address may include a zone
index, separated by a % sign.
The zone index is used to disambiguate identical address
values. For link-local addresses, the zone index will
typically be the interface index number or the name of an
interface. If the zone index is not present, the default
zone of the device will be used.
The canonical format for the zone index is the numerical
format";
}
typedef ipv6-address {
type string {
pattern '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}'
+ '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|'
+ '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}'
+ '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))'
+ '(%[\p{N}\p{L}]+)?';
pattern '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|'
+ '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)'
+ '(%.+)?';
}
description
"The ipv6-address type represents an IPv6 address in full,
mixed, shortened, and shortened-mixed notation. The IPv6
address may include a zone index, separated by a % sign.
The zone index is used to disambiguate identical address
values. For link-local addresses, the zone index will
typically be the interface index number or the name of an
interface. If the zone index is not present, the default
zone of the device will be used.
The canonical format of IPv6 addresses uses the textual
representation defined in Section 4 of RFC 5952. The
canonical format for the zone index is the numerical
format as described in Section 11.2 of RFC 4007.";
reference
"RFC 4291: IP Version 6 Addressing Architecture
RFC 4007: IPv6 Scoped Address Architecture
RFC 5952: A Recommendation for IPv6 Address Text
Representation";
}
typedef ip-address-no-zone {
type union {
type inet:ipv4-address-no-zone;
type inet:ipv6-address-no-zone;
}
description
"The ip-address-no-zone type represents an IP address and is
IP version neutral. The format of the textual representation
implies the IP version. This type does not support scoped
addresses since it does not allow zone identifiers in the
address format.";
reference
"RFC 4007: IPv6 Scoped Address Architecture";
}
typedef ipv4-address-no-zone {
type inet:ipv4-address {
pattern '[0-9\.]*';
}
description
"An IPv4 address without a zone index. This type, derived from
ipv4-address, may be used in situations where the zone is
known from the context and hence no zone index is needed.";
}
typedef ipv6-address-no-zone {
type inet:ipv6-address {
pattern '[0-9a-fA-F:\.]*';
}
description
"An IPv6 address without a zone index. This type, derived from
ipv6-address, may be used in situations where the zone is
known from the context and hence no zone index is needed.";
reference
"RFC 4291: IP Version 6 Addressing Architecture
RFC 4007: IPv6 Scoped Address Architecture
RFC 5952: A Recommendation for IPv6 Address Text
Representation";
}
typedef ip-prefix {
type union {
type inet:ipv4-prefix;
type inet:ipv6-prefix;
}
description
"The ip-prefix type represents an IP prefix and is IP
version neutral. The format of the textual representations
implies the IP version.";
}
typedef ipv4-prefix {
type string {
pattern
'(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+ '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+ '/(([0-9])|([1-2][0-9])|(3[0-2]))';
}
description
"The ipv4-prefix type represents an IPv4 address prefix.
The prefix length is given by the number following the
slash character and must be less than or equal to 32.
A prefix length value of n corresponds to an IP address
mask that has n contiguous 1-bits from the most
significant bit (MSB) and all other bits set to 0.
The canonical format of an IPv4 prefix has all bits of
the IPv4 address set to zero that are not part of the
IPv4 prefix.";
}
typedef ipv6-prefix {
type string {
pattern '((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}'
+ '((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|'
+ '(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}'
+ '(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))'
+ '(/(([0-9])|([0-9]{2})|(1[0-1][0-9])|(12[0-8])))';
pattern '(([^:]+:){6}(([^:]+:[^:]+)|(.*\..*)))|'
+ '((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)'
+ '(/.+)';
}
description
"The ipv6-prefix type represents an IPv6 address prefix.
The prefix length is given by the number following the
slash character and must be less than or equal to 128.
A prefix length value of n corresponds to an IP address
mask that has n contiguous 1-bits from the most
significant bit (MSB) and all other bits set to 0.
The IPv6 address should have all bits that do not belong
to the prefix set to zero.
The canonical format of an IPv6 prefix has all bits of
the IPv6 address set to zero that are not part of the
IPv6 prefix. Furthermore, the IPv6 address is represented
as defined in Section 4 of RFC 5952.";
reference
"RFC 5952: A Recommendation for IPv6 Address Text
Representation";
}
/*** collection of domain name and URI types ***/
typedef domain-name {
type string {
pattern
'((([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.)*'
+ '([a-zA-Z0-9_]([a-zA-Z0-9\-_]){0,61})?[a-zA-Z0-9]\.?)'
+ '|\.';
length "1..253";
}
description
"The domain-name type represents a DNS domain name. The
name SHOULD be fully qualified whenever possible.
Internet domain names are only loosely specified. Section
3.5 of RFC 1034 recommends a syntax (modified in Section
2.1 of RFC 1123). The pattern above is intended to allow
for current practice in domain name use, and some possible
future expansion. It is designed to hold various types of
domain names, including names used for A or AAAA records
(host names) and other records, such as SRV records. Note
that Internet host names have a stricter syntax (described
in RFC 952) than the DNS recommendations in RFCs 1034 and
1123, and that systems that want to store host names in
schema nodes using the domain-name type are recommended to
adhere to this stricter standard to ensure interoperability.
The encoding of DNS names in the DNS protocol is limited
to 255 characters. Since the encoding consists of labels
prefixed by a length bytes and there is a trailing NULL
byte, only 253 characters can appear in the textual dotted
notation.
The description clause of schema nodes using the domain-name
type MUST describe when and how these names are resolved to
IP addresses. Note that the resolution of a domain-name value
may require to query multiple DNS records (e.g., A for IPv4
and AAAA for IPv6). The order of the resolution process and
which DNS record takes precedence can either be defined
explicitly or may depend on the configuration of the
resolver.
Domain-name values use the US-ASCII encoding. Their canonical
format uses lowercase US-ASCII characters. Internationalized
domain names MUST be A-labels as per RFC 5890.";
reference
"RFC 952: DoD Internet Host Table Specification
RFC 1034: Domain Names - Concepts and Facilities
RFC 1123: Requirements for Internet Hosts -- Application
and Support
RFC 2782: A DNS RR for specifying the location of services
(DNS SRV)
RFC 5890: Internationalized Domain Names in Applications
(IDNA): Definitions and Document Framework";
}
typedef host {
type union {
type inet:ip-address;
type inet:domain-name;
}
description
"The host type represents either an IP address or a DNS
domain name.";
}
typedef uri {
type string;
description
"The uri type represents a Uniform Resource Identifier
(URI) as defined by STD 66.
Objects using the uri type MUST be in US-ASCII encoding,
and MUST be normalized as described by RFC 3986 Sections
6.2.1, 6.2.2.1, and 6.2.2.2. All unnecessary
percent-encoding is removed, and all case-insensitive
characters are set to lowercase except for hexadecimal
digits, which are normalized to uppercase as described in
Section 6.2.2.1.
The purpose of this normalization is to help provide
unique URIs. Note that this normalization is not
sufficient to provide uniqueness. Two URIs that are
textually distinct after this normalization may still be
equivalent.
Objects using the uri type may restrict the schemes that
they permit. For example, 'data:' and 'urn:' schemes
might not be appropriate.
A zero-length URI is not a valid URI. This can be used to
express 'URI absent' where required.
In the value set and its semantics, this type is equivalent
to the Uri SMIv2 textual convention defined in RFC 5017.";
reference
"RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
RFC 3305: Report from the Joint W3C/IETF URI Planning Interest
Group: Uniform Resource Identifiers (URIs), URLs,
and Uniform Resource Names (URNs): Clarifications
and Recommendations
RFC 5017: MIB Textual Conventions for Uniform Resource
Identifiers (URIs)";
}
}
module ietf-yang-types {
namespace "urn:ietf:params:xml:ns:yang:ietf-yang-types";
prefix "yang";
organization
"IETF NETMOD (NETCONF Data Modeling Language) Working Group";
contact
"WG Web: <http://tools.ietf.org/wg/netmod/>
WG List: <mailto:netmod@ietf.org>
WG Chair: David Kessens
<mailto:david.kessens@nsn.com>
WG Chair: Juergen Schoenwaelder
<mailto:j.schoenwaelder@jacobs-university.de>
Editor: Juergen Schoenwaelder
<mailto:j.schoenwaelder@jacobs-university.de>";
description
"This module contains a collection of generally useful derived
YANG data types.
Copyright (c) 2013 IETF Trust and the persons identified as
authors of the code. All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, is permitted pursuant to, and subject
to the license terms contained in, the Simplified BSD License
set forth in Section 4.c of the IETF Trust's Legal Provisions
Relating to IETF Documents
(http://trustee.ietf.org/license-info).
This version of this YANG module is part of RFC 6991; see
the RFC itself for full legal notices.";
revision 2013-07-15 {
description
"This revision adds the following new data types:
- yang-identifier
- hex-string
- uuid
- dotted-quad";
reference
"RFC 6991: Common YANG Data Types";
}
revision 2010-09-24 {
description
"Initial revision.";
reference
"RFC 6021: Common YANG Data Types";
}
/*** collection of counter and gauge types ***/
typedef counter32 {
type uint32;
description
"The counter32 type represents a non-negative integer
that monotonically increases until it reaches a
maximum value of 2^32-1 (4294967295 decimal), when it
wraps around and starts increasing again from zero.
Counters have no defined 'initial' value, and thus, a
single value of a counter has (in general) no information
content. Discontinuities in the monotonically increasing
value normally occur at re-initialization of the
management system, and at other times as specified in the
description of a schema node using this type. If such
other times can occur, for example, the creation of
a schema node of type counter32 at times other than
re-initialization, then a corresponding schema node
should be defined, with an appropriate type, to indicate
the last discontinuity.
The counter32 type should not be used for configuration
schema nodes. A default statement SHOULD NOT be used in
combination with the type counter32.
In the value set and its semantics, this type is equivalent
to the Counter32 type of the SMIv2.";
reference
"RFC 2578: Structure of Management Information Version 2
(SMIv2)";
}
typedef zero-based-counter32 {
type yang:counter32;
default "0";
description
"The zero-based-counter32 type represents a counter32
that has the defined 'initial' value zero.
A schema node of this type will be set to zero (0) on creation
and will thereafter increase monotonically until it reaches
a maximum value of 2^32-1 (4294967295 decimal), when it
wraps around and starts increasing again from zero.
Provided that an application discovers a new schema node
of this type within the minimum time to wrap, it can use the
'initial' value as a delta. It is important for a management
station to be aware of this minimum time and the actual time
between polls, and to discard data if the actual time is too
long or there is no defined minimum time.
In the value set and its semantics, this type is equivalent
to the ZeroBasedCounter32 textual convention of the SMIv2.";
reference
"RFC 4502: Remote Network Monitoring Management Information
Base Version 2";
}
typedef counter64 {
type uint64;
description
"The counter64 type represents a non-negative integer
that monotonically increases until it reaches a
maximum value of 2^64-1 (18446744073709551615 decimal),
when it wraps around and starts increasing again from zero.
Counters have no defined 'initial' value, and thus, a
single value of a counter has (in general) no information
content. Discontinuities in the monotonically increasing
value normally occur at re-initialization of the
management system, and at other times as specified in the
description of a schema node using this type. If such
other times can occur, for example, the creation of
a schema node of type counter64 at times other than
re-initialization, then a corresponding schema node
should be defined, with an appropriate type, to indicate
the last discontinuity.
The counter64 type should not be used for configuration
schema nodes. A default statement SHOULD NOT be used in
combination with the type counter64.
In the value set and its semantics, this type is equivalent
to the Counter64 type of the SMIv2.";
reference
"RFC 2578: Structure of Management Information Version 2
(SMIv2)";
}
typedef zero-based-counter64 {
type yang:counter64;
default "0";
description
"The zero-based-counter64 type represents a counter64 that
has the defined 'initial' value zero.
A schema node of this type will be set to zero (0) on creation
and will thereafter increase monotonically until it reaches
a maximum value of 2^64-1 (18446744073709551615 decimal),
when it wraps around and starts increasing again from zero.
Provided that an application discovers a new schema node
of this type within the minimum time to wrap, it can use the
'initial' value as a delta. It is important for a management
station to be aware of this minimum time and the actual time
between polls, and to discard data if the actual time is too
long or there is no defined minimum time.
In the value set and its semantics, this type is equivalent
to the ZeroBasedCounter64 textual convention of the SMIv2.";
reference
"RFC 2856: Textual Conventions for Additional High Capacity
Data Types";
}
typedef gauge32 {
type uint32;
description
"The gauge32 type represents a non-negative integer, which
may increase or decrease, but shall never exceed a maximum
value, nor fall below a minimum value. The maximum value
cannot be greater than 2^32-1 (4294967295 decimal), and
the minimum value cannot be smaller than 0. The value of
a gauge32 has its maximum value whenever the information
being modeled is greater than or equal to its maximum
value, and has its minimum value whenever the information
being modeled is smaller than or equal to its minimum value.
If the information being modeled subsequently decreases
below (increases above) the maximum (minimum) value, the
gauge32 also decreases (increases).
In the value set and its semantics, this type is equivalent
to the Gauge32 type of the SMIv2.";
reference
"RFC 2578: Structure of Management Information Version 2
(SMIv2)";
}
typedef gauge64 {
type uint64;
description
"The gauge64 type represents a non-negative integer, which
may increase or decrease, but shall never exceed a maximum
value, nor fall below a minimum value. The maximum value
cannot be greater than 2^64-1 (18446744073709551615), and
the minimum value cannot be smaller than 0. The value of
a gauge64 has its maximum value whenever the information
being modeled is greater than or equal to its maximum
value, and has its minimum value whenever the information
being modeled is smaller than or equal to its minimum value.
If the information being modeled subsequently decreases
below (increases above) the maximum (minimum) value, the
gauge64 also decreases (increases).
In the value set and its semantics, this type is equivalent
to the CounterBasedGauge64 SMIv2 textual convention defined
in RFC 2856";
reference
"RFC 2856: Textual Conventions for Additional High Capacity
Data Types";
}
/*** collection of identifier-related types ***/
typedef object-identifier {
type string {
pattern '(([0-1](\.[1-3]?[0-9]))|(2\.(0|([1-9]\d*))))'
+ '(\.(0|([1-9]\d*)))*';
}
description
"The object-identifier type represents administratively
assigned names in a registration-hierarchical-name tree.
Values of this type are denoted as a sequence of numerical
non-negative sub-identifier values. Each sub-identifier
value MUST NOT exceed 2^32-1 (4294967295). Sub-identifiers
are separated by single dots and without any intermediate
whitespace.
The ASN.1 standard restricts the value space of the first
sub-identifier to 0, 1, or 2. Furthermore, the value space
of the second sub-identifier is restricted to the range
0 to 39 if the first sub-identifier is 0 or 1. Finally,
the ASN.1 standard requires that an object identifier
has always at least two sub-identifiers. The pattern
captures these restrictions.
Although the number of sub-identifiers is not limited,
module designers should realize that there may be
implementations that stick with the SMIv2 limit of 128
sub-identifiers.
This type is a superset of the SMIv2 OBJECT IDENTIFIER type
since it is not restricted to 128 sub-identifiers. Hence,
this type SHOULD NOT be used to represent the SMIv2 OBJECT
IDENTIFIER type; the object-identifier-128 type SHOULD be
used instead.";
reference
"ISO9834-1: Information technology -- Open Systems
Interconnection -- Procedures for the operation of OSI
Registration Authorities: General procedures and top
arcs of the ASN.1 Object Identifier tree";
}
typedef object-identifier-128 {
type object-identifier {
pattern '\d*(\.\d*){1,127}';
}
description
"This type represents object-identifiers restricted to 128
sub-identifiers.
In the value set and its semantics, this type is equivalent
to the OBJECT IDENTIFIER type of the SMIv2.";
reference
"RFC 2578: Structure of Management Information Version 2
(SMIv2)";
}
typedef yang-identifier {
type string {
length "1..max";
pattern '[a-zA-Z_][a-zA-Z0-9\-_.]*';
pattern '.|..|[^xX].*|.[^mM].*|..[^lL].*';
}
description
"A YANG identifier string as defined by the 'identifier'
rule in Section 12 of RFC 6020. An identifier must
start with an alphabetic character or an underscore
followed by an arbitrary sequence of alphabetic or
numeric characters, underscores, hyphens, or dots.
A YANG identifier MUST NOT start with any possible
combination of the lowercase or uppercase character
sequence 'xml'.";
reference
"RFC 6020: YANG - A Data Modeling Language for the Network
Configuration Protocol (NETCONF)";
}
/*** collection of types related to date and time***/
typedef date-and-time {
type string {
pattern '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?'
+ '(Z|[\+\-]\d{2}:\d{2})';
}
description
"The date-and-time type is a profile of the ISO 8601
standard for representation of dates and times using the
Gregorian calendar. The profile is defined by the
date-time production in Section 5.6 of RFC 3339.
The date-and-time type is compatible with the dateTime XML
schema type with the following notable exceptions:
(a) The date-and-time type does not allow negative years.
(b) The date-and-time time-offset -00:00 indicates an unknown
time zone (see RFC 3339) while -00:00 and +00:00 and Z
all represent the same time zone in dateTime.
(c) The canonical format (see below) of data-and-time values
differs from the canonical format used by the dateTime XML
schema type, which requires all times to be in UTC using
the time-offset 'Z'.
This type is not equivalent to the DateAndTime textual
convention of the SMIv2 since RFC 3339 uses a different
separator between full-date and full-time and provides
higher resolution of time-secfrac.
The canonical format for date-and-time values with a known time
zone uses a numeric time zone offset that is calculated using
the device's configured known offset to UTC time. A change of
the device's offset to UTC time will cause date-and-time values
to change accordingly. Such changes might happen periodically
in case a server follows automatically daylight saving time
(DST) time zone offset changes. The canonical format for
date-and-time values with an unknown time zone (usually
referring to the notion of local time) uses the time-offset
-00:00.";
reference
"RFC 3339: Date and Time on the Internet: Timestamps
RFC 2579: Textual Conventions for SMIv2
XSD-TYPES: XML Schema Part 2: Datatypes Second Edition";
}
typedef timeticks {
type uint32;
description
"The timeticks type represents a non-negative integer that
represents the time, modulo 2^32 (4294967296 decimal), in
hundredths of a second between two epochs. When a schema
node is defined that uses this type, the description of
the schema node identifies both of the reference epochs.
In the value set and its semantics, this type is equivalent
to the TimeTicks type of the SMIv2.";
reference
"RFC 2578: Structure of Management Information Version 2
(SMIv2)";
}
typedef timestamp {
type yang:timeticks;
description
"The timestamp type represents the value of an associated
timeticks schema node at which a specific occurrence
happened. The specific occurrence must be defined in the
description of any schema node defined using this type. When
the specific occurrence occurred prior to the last time the
associated timeticks attribute was zero, then the timestamp
value is zero. Note that this requires all timestamp values
to be reset to zero when the value of the associated timeticks
attribute reaches 497+ days and wraps around to zero.
The associated timeticks schema node must be specified
in the description of any schema node using this type.
In the value set and its semantics, this type is equivalent
to the TimeStamp textual convention of the SMIv2.";
reference
"RFC 2579: Textual Conventions for SMIv2";
}
/*** collection of generic address types ***/
typedef phys-address {
type string {
pattern '([0-9a-fA-F]{2}(:[0-9a-fA-F]{2})*)?';
}
description
"Represents media- or physical-level addresses represented
as a sequence octets, each octet represented by two hexadecimal
numbers. Octets are separated by colons. The canonical
representation uses lowercase characters.
In the value set and its semantics, this type is equivalent
to the PhysAddress textual convention of the SMIv2.";
reference
"RFC 2579: Textual Conventions for SMIv2";
}
typedef mac-address {
type string {
pattern '[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}';
}
description
"The mac-address type represents an IEEE 802 MAC address.
The canonical representation uses lowercase characters.
In the value set and its semantics, this type is equivalent
to the MacAddress textual convention of the SMIv2.";
reference
"IEEE 802: IEEE Standard for Local and Metropolitan Area
Networks: Overview and Architecture
RFC 2579: Textual Conventions for SMIv2";
}
/*** collection of XML-specific types ***/
typedef xpath1.0 {
type string;
description
"This type represents an XPATH 1.0 expression.
When a schema node is defined that uses this type, the
description of the schema node MUST specify the XPath
context in which the XPath expression is evaluated.";
reference
"XPATH: XML Path Language (XPath) Version 1.0";
}
/*** collection of string types ***/
typedef hex-string {
type string {
pattern '([0-9a-fA-F]{2}(:[0-9a-fA-F]{2})*)?';
}
description
"A hexadecimal string with octets represented as hex digits
separated by colons. The canonical representation uses
lowercase characters.";
}
typedef uuid {
type string {
pattern '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-'
+ '[0-9a-fA-F]{4}-[0-9a-fA-F]{12}';
}
description
"A Universally Unique IDentifier in the string representation
defined in RFC 4122. The canonical representation uses
lowercase characters.
The following is an example of a UUID in string representation:
f81d4fae-7dec-11d0-a765-00a0c91e6bf6
";
reference
"RFC 4122: A Universally Unique IDentifier (UUID) URN
Namespace";
}
typedef dotted-quad {
type string {
pattern
'(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+ '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
}
description
"An unsigned 32-bit number expressed in the dotted-quad
notation, i.e., four octets written as decimal numbers
and separated with the '.' (full stop) character.";
}
}