· 8 years ago · Jul 23, 2018, 10:00 AM
1require 'active_record'
2require 'yaml'
3
4class GraphLabel < ActiveRecord::Base
5 has_many :network_elements
6end
7
8class NetworkElement < ActiveRecord::Base
9 belongs_to :graph_label
10 validates_presence_of :graph_label_id
11end
12
13YAML.add_domain_type("yaml.org,2002", "") do |type, val|
14 klass = type.split(":").last.constantize
15
16 #Without removing this element, we get a "`log_protected_attribute_removal': undefined method `debug' for nil:NilClass (NoMethodError)"
17 # why ????
18 val["attributes"].delete("id")
19
20 warn "Creating object class: #{klass} values: #{val["attributes"].inspect}"
21
22 if klass == NetworkElement then
23 NetworkElement.new(val["attributes"])
24 end
25
26 if klass == GraphLabel then
27 GraphLabel.new(val["attributes"])
28 end
29 #YAML.object_maker(klass,val)
30end
31
32class ActiveRecord::Base
33 def to_yaml_type
34 "!yaml.org,2002:#{self.class}"
35 end
36end
37
38# database.yml [3] =
39#- adapter: mysql
40# host: localhost
41# database: VNet_db
42
43$config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
44ActiveRecord::Base.establish_connection $config[3]
45
46# Just make sure database is empty...
47 NetworkElement.delete_all
48 GraphLabel.delete_all
49
50# Create the master object and 2 slave objects
51 gl1 = GraphLabel.create(:role_identifier => "SNode 111", :graph_type => "ML", :graph_nr => 0)
52 gl1.network_elements.create(:identifier => "testnode1", :alias => "testnode1alias", :ne_type => "/node/host/generic")
53 gl1.network_elements.create(:identifier => "testnode2", :ne_type => "/node/host/generic")
54
55# use of warn(stderr) instead of print(stdin) for unbuffered-readable output in Windows
56
57# Create yaml file with contents of the database ...
58 graph = GraphLabel.first(:include => :network_elements)
59 gf = File.open("graph.yml","w")
60 gf.write graph.to_yaml
61 gf.close
62
63# Empty database AGAIN.
64 NetworkElement.delete_all
65 GraphLabel.delete_all
66
67# Try to recreate from the yaml file the objects and it's associations...
68File.open( 'graph.yml' ) do |yf|
69 YAML.each_document( yf ) do |gl|
70 # if we comment the YAML.add_domain_type and AR::Base blocks, we get a full dump, but still no network_elements array filled...
71 warn gl.to_yaml
72
73 if gl.network_elements.count > 0 then
74 gl.network_elements.first.alias = "alias field changed"
75 warn gl.network_elements.first.to_yaml
76# gl.instance_variable_set("@new_record", true)
77# gl.id = nil
78# gl.save! # Updates or creates a new one?
79 warn gl.network_elements.first.to_yaml
80 else
81 warn " ==> Error: Net Elem array is empty!!!"
82 end
83# warn gl.to_yaml
84 end
85end
86
87warn "\nScript has finished."
88
89#Database creation
90
91#CREATE TABLE IF NOT EXISTS `VNet_db`.`graph_labels` (
92# `id` INT NOT NULL AUTO_INCREMENT ,
93# `role_identifier` VARCHAR(45) NOT NULL COMMENT 'VNP A' ,
94# `graph_type` VARCHAR(2) NOT NULL COMMENT 'OL ML UL' ,
95# `graph_nr` INT NOT NULL COMMENT 'OL/UL#n(from top/bottom)' ,
96# `graph_tag` VARCHAR(45) NULL ,
97# `v_net_identifier` VARCHAR(120) NULL ,
98# PRIMARY KEY (`id`) )
99#ENGINE = InnoDB;
100#
101#CREATE TABLE IF NOT EXISTS `VNet_db`.`network_elements` (
102# `id` INT NOT NULL AUTO_INCREMENT COMMENT 'elementID (db purposes)' ,
103# `identifier` VARCHAR(120) NOT NULL DEFAULT 'unused' COMMENT 'ID as in topology description' ,
104# `alias` VARCHAR(120) NULL DEFAULT NULL COMMENT 'alias name (as given by the role managing this db)' ,
105# `ne_type` VARCHAR(20) NOT NULL DEFAULT '/node/host/generic' COMMENT 'host/switch/router' ,
106# `console_interface_id` INT NULL DEFAULT NULL ,
107# `customer_console_interface_id` INT NULL ,
108# `graph_label_id` INT NULL ,
109# `provisioning_interface_id` INT NULL ,
110# PRIMARY KEY (`id`) ,
111# INDEX `fk_network_elements_graph_labels1` (`graph_label_id` ASC) ,
112# CONSTRAINT `fk_network_elements_graph_labels1`
113# FOREIGN KEY (`graph_label_id` )
114# REFERENCES `VNet_db`.`graph_labels` (`id` )
115# ON DELETE CASCADE
116# ON UPDATE NO ACTION,
117#ENGINE = InnoDB;
118
119# Database contents in YAML format
120#
121#--- !yaml.org,2002:GraphLabel
122#attributes:
123# graph_type: ML
124# id: "340"
125# role_identifier: SNode 111
126# v_net_identifier:
127# graph_tag:
128# graph_nr: "0"
129#attributes_cache: {}
130#
131#network_elements:
132#- !yaml.org,2002:NetworkElement
133# attributes:
134# graph_label_id: "340"
135# ne_type: /node/host/generic
136# id: "866"
137# provisioning_interface_id:
138# console_interface_id:
139# alias: testnode1alias
140# identifier: testnode1
141# customer_console_interface_id:
142# attributes_cache: {}
143#
144#- !yaml.org,2002:NetworkElement
145# attributes:
146# graph_label_id: "340"
147# ne_type: /node/host/generic
148# id: "867"
149# provisioning_interface_id:
150# console_interface_id:
151# alias:
152# identifier: testnode2
153# customer_console_interface_id:
154# attributes_cache: {}
155#