1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# 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.
#
from netmodel.model.type import Integer, String, Bool, InetAddress
from netmodel.model.object import ObjectMetaclass
from vicn.core.attribute import Attribute
from vicn.core.requirement import Requirement
from vicn.core.resource import Resource
from vicn.resource.interface import Interface
from vicn.resource.node import Node
DEFAULT_ETHER_PROTO = 0x0801
DEFAULT_PORT = 6363
FMT_L4FACE_IPV4 = '{protocol}://{dst.ip4_address}:{dst_port}/'
FMT_L4FACE_IPV6 = '{protocol}://{dst.ip6_address}:{dst_port}/'
FMT_L2FACE = '{protocol}://[{dst.mac_address}]/{src.device_name}'
#------------------------------------------------------------------------------
class FaceMetaclass(ObjectMetaclass):
def __new__(mcls, name, bases, attrs):
cls = super(FaceMetaclass, mcls).__new__(mcls, name, bases, attrs)
if name != 'Face':
cls.register()
return cls
class Face(Resource, metaclass=FaceMetaclass):
"""
Resource: Face
"""
node = Attribute(Node, mandatory = True,
requirements = [
Requirement('forwarder')
])
protocol = Attribute(String,
description = 'Face underlying protocol',
mandatory = True)
src = Attribute(Interface, mandatory = True)
dst = Attribute(Interface, mandatory = True)
id = Attribute(String, description = 'Local face ID',
ro = True)
# Cisco's extensions
wldr = Attribute(Bool, description = 'flag: WLDR enabled',
default = False)
x2 = Attribute(Bool, description = 'flag: X2 face',
default = False)
# NFD extensions
permanent = Attribute(Bool, description = 'flag: permanent face',
default = True)
nfd_uri = Attribute(String, description = 'Face uri',
func = lambda self : self._lambda_nfd_uri())
nfdc_flags = Attribute(String,
description = 'Flags for face creation with NFDC',
func = lambda self : self._lambda_nfdc_flags())
# map protocol -> face class
_map_protocol = dict()
@classmethod
def register(cls):
for protocol in cls.__protocols__:
Face._map_protocol[protocol] = cls
@classmethod
def from_protocol(cls, protocol):
return cls._map_protocol.get(protocol)
def __repr__(self):
flags = ''
if self.permanent:
flags += 'permanent '
if self.wldr:
flags += 'wldr '
if self.x2:
flags += 'x2 '
sibling_face_name = self._internal_data.get('sibling_face', None)
sibling_face = self._state.manager.by_name(sibling_face_name) \
if sibling_face_name else None
dst_node = sibling_face.node.name if sibling_face else None
return '<Face {} {} on node {} -- to node {}>'.format(
self.nfd_uri, flags, self.node.name, dst_node)
__str__ = __repr__
# NFD specifics
def _lambda_nfd_uri(self):
return 'N/A' # raise NotImplementedError
def _lambda_nfdc_flags(self):
flags = ''
if self.permanent:
flags += '-P '
if self.wldr:
flags += '-W '
if self.x2:
flags += '-X '
return flags
#------------------------------------------------------------------------------
class L2Face(Face):
__protocols__ = ['ether']
ether_proto = Attribute(String,
description = "Ethernet protocol number used by the face",
default = DEFAULT_ETHER_PROTO)
def _lambda_nfd_uri(self):
return self.format(FMT_L2FACE)
#------------------------------------------------------------------------------
class L4Face(Face):
__protocols__ = ['tcp4', 'tcp6', 'udp4', 'udp6']
src_port = Attribute(Integer, description = "local TCP/UDP port",
default = DEFAULT_PORT)
dst_port = Attribute(Integer, description = "remote TCP/UDP port",
default = DEFAULT_PORT)
def _lambda_nfd_uri(self):
fmt = FMT_L4FACE_IPV4 if self.protocol in ['tcp4', 'udp4'] else FMT_L4FACE_IPV6
return self.format(fmt)
|