Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'active_record'
- require 'yaml'
- class GraphLabel < ActiveRecord::Base
- has_many :network_elements
- end
- class NetworkElement < ActiveRecord::Base
- belongs_to :graph_label
- validates_presence_of :graph_label_id
- end
- YAML.add_domain_type("yaml.org,2002", "") do |type, val|
- klass = type.split(":").last.constantize
- #Without removing this element, we get a "`log_protected_attribute_removal': undefined method `debug' for nil:NilClass (NoMethodError)"
- # why ????
- val["attributes"].delete("id")
- warn "Creating object class: #{klass} values: #{val["attributes"].inspect}"
- if klass == NetworkElement then
- NetworkElement.new(val["attributes"])
- end
- if klass == GraphLabel then
- GraphLabel.new(val["attributes"])
- end
- #YAML.object_maker(klass,val)
- end
- class ActiveRecord::Base
- def to_yaml_type
- "!yaml.org,2002:#{self.class}"
- end
- end
- # database.yml [3] =
- #- adapter: mysql
- # host: localhost
- # database: VNet_db
- $config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
- ActiveRecord::Base.establish_connection $config[3]
- # Just make sure database is empty...
- NetworkElement.delete_all
- GraphLabel.delete_all
- # Create the master object and 2 slave objects
- gl1 = GraphLabel.create(:role_identifier => "SNode 111", :graph_type => "ML", :graph_nr => 0)
- gl1.network_elements.create(:identifier => "testnode1", :alias => "testnode1alias", :ne_type => "/node/host/generic")
- gl1.network_elements.create(:identifier => "testnode2", :ne_type => "/node/host/generic")
- # use of warn(stderr) instead of print(stdin) for unbuffered-readable output in Windows
- # Create yaml file with contents of the database ...
- graph = GraphLabel.first(:include => :network_elements)
- gf = File.open("graph.yml","w")
- gf.write graph.to_yaml
- gf.close
- # Empty database AGAIN.
- NetworkElement.delete_all
- GraphLabel.delete_all
- # Try to recreate from the yaml file the objects and it's associations...
- File.open( 'graph.yml' ) do |yf|
- YAML.each_document( yf ) do |gl|
- # if we comment the YAML.add_domain_type and AR::Base blocks, we get a full dump, but still no network_elements array filled...
- warn gl.to_yaml
- if gl.network_elements.count > 0 then
- gl.network_elements.first.alias = "alias field changed"
- warn gl.network_elements.first.to_yaml
- # gl.instance_variable_set("@new_record", true)
- # gl.id = nil
- # gl.save! # Updates or creates a new one?
- warn gl.network_elements.first.to_yaml
- else
- warn " ==> Error: Net Elem array is empty!!!"
- end
- # warn gl.to_yaml
- end
- end
- warn "\nScript has finished."
- #Database creation
- #CREATE TABLE IF NOT EXISTS `VNet_db`.`graph_labels` (
- # `id` INT NOT NULL AUTO_INCREMENT ,
- # `role_identifier` VARCHAR(45) NOT NULL COMMENT 'VNP A' ,
- # `graph_type` VARCHAR(2) NOT NULL COMMENT 'OL ML UL' ,
- # `graph_nr` INT NOT NULL COMMENT 'OL/UL#n(from top/bottom)' ,
- # `graph_tag` VARCHAR(45) NULL ,
- # `v_net_identifier` VARCHAR(120) NULL ,
- # PRIMARY KEY (`id`) )
- #ENGINE = InnoDB;
- #
- #CREATE TABLE IF NOT EXISTS `VNet_db`.`network_elements` (
- # `id` INT NOT NULL AUTO_INCREMENT COMMENT 'elementID (db purposes)' ,
- # `identifier` VARCHAR(120) NOT NULL DEFAULT 'unused' COMMENT 'ID as in topology description' ,
- # `alias` VARCHAR(120) NULL DEFAULT NULL COMMENT 'alias name (as given by the role managing this db)' ,
- # `ne_type` VARCHAR(20) NOT NULL DEFAULT '/node/host/generic' COMMENT 'host/switch/router' ,
- # `console_interface_id` INT NULL DEFAULT NULL ,
- # `customer_console_interface_id` INT NULL ,
- # `graph_label_id` INT NULL ,
- # `provisioning_interface_id` INT NULL ,
- # PRIMARY KEY (`id`) ,
- # INDEX `fk_network_elements_graph_labels1` (`graph_label_id` ASC) ,
- # CONSTRAINT `fk_network_elements_graph_labels1`
- # FOREIGN KEY (`graph_label_id` )
- # REFERENCES `VNet_db`.`graph_labels` (`id` )
- # ON DELETE CASCADE
- # ON UPDATE NO ACTION,
- #ENGINE = InnoDB;
- # Database contents in YAML format
- #
- #--- !yaml.org,2002:GraphLabel
- #attributes:
- # graph_type: ML
- # id: "340"
- # role_identifier: SNode 111
- # v_net_identifier:
- # graph_tag:
- # graph_nr: "0"
- #attributes_cache: {}
- #
- #network_elements:
- #- !yaml.org,2002:NetworkElement
- # attributes:
- # graph_label_id: "340"
- # ne_type: /node/host/generic
- # id: "866"
- # provisioning_interface_id:
- # console_interface_id:
- # alias: testnode1alias
- # identifier: testnode1
- # customer_console_interface_id:
- # attributes_cache: {}
- #
- #- !yaml.org,2002:NetworkElement
- # attributes:
- # graph_label_id: "340"
- # ne_type: /node/host/generic
- # id: "867"
- # provisioning_interface_id:
- # console_interface_id:
- # alias:
- # identifier: testnode2
- # customer_console_interface_id:
- # attributes_cache: {}
- #
Add Comment
Please, Sign In to add comment