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
148
149
150
151
152
|
#
# Copyright (c) 2019 PANTHEON.tech.
# Copyright (c) 2019 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.
#
import os
import subprocess
from vpp_controler import Vpp_controler
from netconf_client import NetConfClient
from socket import AF_INET
from pyroute2 import IPRoute
import psutil
import time
from ydk.providers import NetconfServiceProvider
from ydk.errors import YClientError
class Topology:
debug = False
def __init__(self):
self.process = []
def __del__(self):
self._kill_process()
def _kill_process(self):
if self.debug:
return
if not self.process:
return
for process in self.process:
if process is None:
continue
process.terminate()
for proc in psutil.process_iter(attrs=['pid', 'name']):
name = proc.info['name']
if 'vpp' in name or 'sysrepo' in name or 'netopeer' in name:
proc.kill()
self.process = []
def _prepare_linux_enviroment(self):
ip = IPRoute()
exist = ip.link_lookup(ifname='vpp1')
if exist:
return
ip.link('add', ifname='vpp1', peer='virtual1', kind='veth')
ip.link('add', ifname='vpp2', peer='virtual2', kind='veth')
vpp1 = ip.link_lookup(ifname='virtual1')[0]
vpp2 = ip.link_lookup(ifname='virtual2')[0]
ip.link('set', index=vpp1, state='up')
ip.link('set', index=vpp2, state='up')
ip.addr('add', index=vpp1, address='192.168.0.2', prefixlen=24)
ip.addr('add', index=vpp2, address='192.168.1.2', prefixlen=24)
def _start_sysrepo(self):
#TODO: Add to log
#print("Start sysrepo deamon.")
#TODO: Need property close.
err = open("/var/log/sysrepod", 'wb')
if self.debug:
params = "-l 4"
else:
params = "-l 3"
self.sysrepo = subprocess.Popen(["sysrepod", "-d", params],
stdout=subprocess.PIPE, stderr=err)
self.process.append(self.sysrepo)
def _start_sysrepo_plugins(self):
#TODO: Add to log
#print("Start sysrepo plugins.")
#TODO: Need property close.
err = open("/var/log/sysrepo-plugind", 'wb')
if self.debug:
params = "-l 4"
else:
params = "-l 3"
self.splugin = subprocess.Popen(["sysrepo-plugind", "-d", params],
stdout=subprocess.PIPE, stderr=err)
self.process.append(self.splugin)
def _start_netopeer_server(self):
#TODO: Add to log
#print("Start netopeer server.")
err = open("/var/log/netopeer2-server", 'wb')
self.netopeer_server = subprocess.Popen(["netopeer2-server", "-d"],
stdout=subprocess.PIPE, stderr=err)
self.process.append(self.netopeer_server)
def _start_netopeer_cli(self):
#TODO: Add to log
#print("Start netopeer client.")
self.netopeer_cli = Netopeer_controler()
self.process.append(self.netopeer_cli)
self.netopeer_cli.spawn()
def _start_vpp(self):
#print("Start VPP.")
self.vpp = Vpp_controler(self.debug)
self.vpp.spawn()
self.process.append(self.vpp)
def _start_netconfclient(self):
#print("Start NetconfClient")
try:
self.netconf_client = NetConfClient(address="127.0.0.1",
username="user", password="user")
self.process.append(self.netconf_client)
except RuntimeError as err:
print("NetConfClient failed, {}".format(err))
def get_vpp(self):
return self.vpp
def get_netopeer_cli(self):
return self.netconf_client
def create_topology(self, debug=False):
self.debug = debug
self._prepare_linux_enviroment()
self._start_vpp()
self._start_sysrepo()
time.sleep(1)
self._start_sysrepo_plugins()
self._start_netopeer_server()
#Wait for netopeer server
time.sleep(1)
self._start_netconfclient()
def close_topology(self):
self._kill_process()
|