Updates docs for SR1 with final revision
[genesis.git] / fuel / prototypes / auto-deploy / deploy / tools / transplant_interfaces.py
1 #!/usr/bin/python
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 import yaml
13 import re
14 import sys
15 import os
16
17 if len(sys.argv) != 4:
18     sys.stderr.write("Usage: "+sys.argv[0]+" <infile> <deafile> <nodeid>\n")
19     sys.exit(1)
20
21 infile = sys.argv[1]
22 if not os.path.exists(infile):
23     sys.stderr.write("ERROR: The file "+infile+" could not be opened\n")
24     sys.exit(1)
25
26 deafile = sys.argv[2]
27 if not os.path.exists(deafile):
28     sys.stderr.write("ERROR: The file "+deafile+" could not be opened\n")
29     sys.exit(1)
30 deafile = sys.argv[2]
31 nodeid = int(sys.argv[3])
32
33 namespace = "interfaces"
34
35 f1 = open(infile, 'r')
36 doc1 = yaml.load(f1)
37 f1.close()
38
39 f2 = open(deafile, 'r')
40 doc2 = yaml.load(f2)
41 f2.close()
42
43
44 # Create lookup table network name -> id for current setup
45 nwlookup = {}
46 for interface in doc1:
47   iface = {}
48   networks = []
49   for network in interface["assigned_networks"]:
50     nwlookup[network["name"]] = network["id"]
51
52 # Find network information in DEA for this node
53 nodeInfo = {}
54 for node in doc2["nodes"]:
55     if node["id"] == nodeid:
56         nodeInfo=node
57         print "Found nodeinfo for node %d" % nodeid
58
59 out = {}
60 out["interfaces"] = {}
61
62 for interface in doc1:
63   assigned = []
64   nw = {}
65   interface["assigned_networks"] = []
66   try:
67     for nwname in nodeInfo["interfaces"][interface["name"]]:
68       iface = {}
69       iface["id"] = nwlookup[nwname]
70       iface["name"] = nwname
71       interface["assigned_networks"].append(iface)
72   except:
73     print "No match for interface " + interface["name"]
74
75 f3 = open(infile, 'w')
76 f3.write(yaml.dump(doc1, default_flow_style=False))
77 f3.close()