summaryrefslogtreecommitdiffstats
path: root/vicn/resource/icn/face.py
blob: db72730d199e190552cc03a44f117722eac227be (plain)
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
#!/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 enum                       import Enum

from netmodel.model.type        import Integer, String, Bool
from vicn.core.attribute        import Attribute
from vicn.core.requirement      import Requirement
from vicn.core.resource         import Resource
from vicn.resource.node         import Node
from vicn.resource.interface    import Interface

DEFAULT_ETHER_PROTO = 0x0801
FMT_L4FACE = '{protocol.name}://{dst_ip}:{dst_port}/'
FMT_L2FACE = '{protocol.name}://[{dst_mac}]/{src_nic.device_name}'

class FaceProtocol(Enum):
    ether = 0
    ip4 = 1
    ip6 = 2
    tcp4 = 3
    tcp6 = 4
    udp4 = 5
    udp6 = 7
    app = 8

    @staticmethod
    def from_string(protocol):
        return getattr(FaceProtocol, protocol)

#------------------------------------------------------------------------------

class Face(Resource):
    """
    Resource: Face
    """

    node = Attribute(Node, mandatory = True,
            requirements = [
                Requirement('forwarder')
            ])
    protocol = Attribute(String, 
            description = 'Face underlying protocol', 
            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())

    def __repr__(self):
        flags = ''
        if self.permanent:
            flags += 'permanent '
        if self.wldr:
            flags += 'wldr '
        if self.x2:
            flags += 'x2 '
        sibling_face_name = self.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):
        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):

    src_nic = Attribute(Interface, 
            description = "Name of the network interface linked to the face",
            mandatory=True)
    dst_mac = Attribute(String, description = "destination MAC address", 
            mandatory=True)
    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):

    ip_version = Attribute(Integer, description = "IPv4 or IPv6", default = 4)
    src_ip = Attribute(String, description = "local IP address", 
            mandatory = True)
    src_port = Attribute(Integer, description = "local TCP/UDP port")
    dst_ip = Attribute(String, descrition = "remote IP address", 
            mandatory=True)
    dst_port = Attribute(Integer, description = "remote TCP/UDP port", 
            mandatory=True)

    def _lambda_nfd_uri(self):
        return self.format(FMT_L4FACE)