Class: Rubyang::Database::SchemaTree::SchemaNode

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyang/database/schema_tree.rb

Overview

TODO: should add config_mode_context to validate and raise error if there is config true stmt in config false stmt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yangs, arg, yang, parent, _module) ⇒ SchemaNode

Returns a new instance of SchemaNode.



396
397
398
399
400
401
402
403
# File 'lib/rubyang/database/schema_tree.rb', line 396

def initialize yangs, arg, yang, parent, _module
  @logger = Logger.new(self.class.name)
  @yangs = yangs
  @arg = arg
  @yang = yang
  @parent = parent
  @module = _module
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



395
396
397
# File 'lib/rubyang/database/schema_tree.rb', line 395

def arg
  @arg
end

#moduleObject

Returns the value of attribute module.



395
396
397
# File 'lib/rubyang/database/schema_tree.rb', line 395

def module
  @module
end

#parentObject

Returns the value of attribute parent.



395
396
397
# File 'lib/rubyang/database/schema_tree.rb', line 395

def parent
  @parent
end

#yangObject

Returns the value of attribute yang.



395
396
397
# File 'lib/rubyang/database/schema_tree.rb', line 395

def yang
  @yang
end

#yangsObject

Returns the value of attribute yangs.



395
396
397
# File 'lib/rubyang/database/schema_tree.rb', line 395

def yangs
  @yangs
end

Instance Method Details

#evaluate_xpath(xpath, current = self) ⇒ Object



437
438
439
440
441
442
443
444
# File 'lib/rubyang/database/schema_tree.rb', line 437

def evaluate_xpath xpath, current=self
  @logger.debug { 'in evaluate_xpath:' }
  @logger.debug { 'xpath:' }
  @logger.debug { xpath.to_yaml }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  evaluate_xpath_expr xpath, current
end

#evaluate_xpath_axis(location_step, current) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/rubyang/database/schema_tree.rb', line 476

def evaluate_xpath_axis location_step, current
  @logger.debug { 'in evaluate_xpath_axis:' }
  @logger.debug { 'location_step:' }
  @logger.debug { location_step.to_yaml }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  case location_step.axis.name
  when Rubyang::Xpath::Axis::SELF
    [self]
  when Rubyang::Xpath::Axis::PARENT
    [@parent]
  when Rubyang::Xpath::Axis::CHILD
    @children
  else
    raise "location_step.axis.name: #{location_step.axis.name} NOT implemented"
  end
end

#evaluate_xpath_expr(expr, current) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/rubyang/database/schema_tree.rb', line 532

def evaluate_xpath_expr expr, current
  case expr
  when Rubyang::Xpath::Expr
    @logger.debug { "in Expr" }
    @logger.debug { "op: #{expr.op}" }
    op = expr.op
    op_result = self.evaluate_xpath_expr( op, current )
  when Rubyang::Xpath::OrExpr
    @logger.debug { "in OrExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Boolean
    end
  when Rubyang::Xpath::AndExpr
    @logger.debug { "in AndExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Boolean
    end
  when Rubyang::Xpath::EqualityExpr
    @logger.debug { "in EqualityExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Boolean
    end
  when Rubyang::Xpath::RelationalExpr
    @logger.debug { "in RelationalExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Boolean
    end
  when Rubyang::Xpath::AdditiveExpr
    @logger.debug { "in AdditiveExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Number
    end
  when Rubyang::Xpath::MultiplicativeExpr
    @logger.debug { "in MultiplicativeExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::Number
    end
  when Rubyang::Xpath::UnaryExpr
    @logger.debug { "in UnaryExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if operator == nil
      op1_result
    else
      Rubyang::Xpath::BasicType::Number
    end
  when Rubyang::Xpath::UnionExpr
    @logger.debug { "in UnionExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      Rubyang::Xpath::BasicType::NodeSet
    end
  when Rubyang::Xpath::PathExpr
    @logger.debug { "in PathExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = case op1
                 when Rubyang::Xpath::LocationPath
                   self.evaluate_xpath_path( op1, current )
                 when Rubyang::Xpath::FilterExpr
                   self.evaluate_xpath_expr( op1, current )
                 else
                   raise "PathExpr: #{op1} not supported"
                 end
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_path( op2, current )
      Rubyang::Xpath::BasicType::NodeSet
    end
  when Rubyang::Xpath::FilterExpr
    @logger.debug { "in FilterExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2.expr, current )
      Rubyang::Xpath::BasicType::NodeSet
    end
  when Rubyang::Xpath::PrimaryExpr
    @logger.debug { "in PrimaryExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    op1 = expr.op1
    case op1
    when Rubyang::Xpath::VariableReference
      Rubyang::Xpath::BasicType::String
    when Rubyang::Xpath::Expr
      op1_result = self.evaluate_xpath_expr( op1, current )
    when Rubyang::Xpath::Literal
      Rubyang::Xpath::BasicType::String
    when Rubyang::Xpath::Number
      Rubyang::Xpath::BasicType::Boolean
    when Rubyang::Xpath::FunctionCall
      op1_result = self.evaluate_xpath_expr( op1, current )
    else
      raise "Primary Expr: '#{op1}' not implemented"
    end
  when Rubyang::Xpath::FunctionCall
    @logger.debug { "in FunctionCall" }
    @logger.debug { "name: #{expr.name}" }
    @logger.debug { "args: #{expr.args}" }
    name = expr.name
    case name
    when Rubyang::Xpath::FunctionCall::CURRENT
      Rubyang::Xpath::BasicType::NodeSet
    else
      raise "FunctionCall: #{name} not implemented"
    end
  else
    raise "Unrecognized Expr: #{expr}"
  end
end

#evaluate_xpath_node_test(location_step, current) ⇒ Object



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/rubyang/database/schema_tree.rb', line 494

def evaluate_xpath_node_test location_step, current
  case location_step.node_test.node_test_type
  when Rubyang::Xpath::NodeTest::NodeTestType::NAME_TEST
    if self.model.arg == location_step.node_test.node_test
      [self]
    else
      []
    end
  when Rubyang::Xpath::NodeTest::NodeTestType::NODE_TYPE
    case location_step.node_test.node_test
    when Rubyang::Xpath::NodeTest::NodeType::COMMENT
      raise "node-type: comment is not implemented"
    when Rubyang::Xpath::NodeTest::NodeType::TEXT
      raise "node-type: text is not implemented"
    when Rubyang::Xpath::NodeTest::NodeType::NODE
      [self]
    else
      raise "node-type not match to comment or text or node"
    end
  when Rubyang::Xpath::NodeTest::NodeTestType::PROCESSING_INSTRUCTION
    raise "processing-instruction is not implemented"
  else
    raise ""
  end
end

#evaluate_xpath_path(location_path, current) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/rubyang/database/schema_tree.rb', line 446

def evaluate_xpath_path location_path, current
  @logger.debug { 'in evaluate_xpath_path:' }
  @logger.debug { 'location_path:' }
  @logger.debug { location_path.to_yaml }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  first_location_step = location_path.location_step_sequence.first
  @logger.debug { 'first_location_step:' }
  @logger.debug { first_location_step.to_yaml }
  candidates_by_axis = self.evaluate_xpath_axis( first_location_step, current )
  @logger.debug { 'candidates_by_axis:' }
  @logger.debug { candidates_by_axis.to_yaml }
  candidates_by_node_test = candidates_by_axis.inject([]){ |cs, c| cs + c.evaluate_xpath_node_test( first_location_step, current ) }
  @logger.debug { 'candidates_by_node_test:' }
  @logger.debug { candidates_by_node_test.to_yaml }
  candidates_by_predicates = candidates_by_node_test.inject([]){ |cs, c| cs + c.evaluate_xpath_predicates( first_location_step, current ) }
  @logger.debug { 'candidates_by_predicates:' }
  @logger.debug { candidates_by_predicates.to_yaml }
  if location_path.location_step_sequence[1..-1].size == 0
    candidates_by_predicates
  else
    candidates_by_predicates.inject([]){ |cs, c|
      following_location_path = Rubyang::Xpath::LocationPath.new *(location_path.location_step_sequence[1..-1])
      @logger.debug { 'following_location_path:' }
      @logger.debug { following_location_path.to_yaml }
      c.evaluate_xpath_path( following_location_path, current )
    }
  end
end

#evaluate_xpath_predicates(location_step, current) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
# File 'lib/rubyang/database/schema_tree.rb', line 520

def evaluate_xpath_predicates location_step, current
  case location_step.predicates.size
  when 0
    [self]
  else
    location_step.predicates.each{ |predicate|
      self.evaluate_xpath_expr predicate.expr, current
    }
    [self]
  end
end

#load_yang(yang, yangs = @yangs, parent_module = yang, current_module = yang, grouping_list = [], typedef_list = [], identity_list = []) ⇒ Object



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/rubyang/database/schema_tree.rb', line 721

def load_yang yang, yangs=@yangs, parent_module=yang, current_module=yang, grouping_list=[], typedef_list=[], identity_list=[]
  case yang
  when Rubyang::Model::Module
    yangs.push yang
    module_arg    = yang.arg
    namespace_arg = yang.substmt( 'namespace' )[0].arg
    prefix_arg    = yang.substmt( 'prefix' )[0].arg
    grouping_list += yang.substmt( 'grouping' )
    typedef_list  += yang.substmt( 'typedef' )
    identity_list += yang.substmt( 'identity' )
    yang.substmt( 'include' ).each{ |i|
      i.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
        self.load_yang s, yangs, parent_module, i, i.substmt( 'grouping' ), i.substmt( 'typedef' ), i.substmt( 'identity' )
      }
    }
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      self.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
  when Rubyang::Model::Submodule
    yangs.push yang
  when Rubyang::Model::Anyxml
    arg = yang.arg
    self.children.push Anyxml.new( yangs, arg, yang, self, parent_module )
  when Rubyang::Model::Container
    container_arg = yang.arg
    grouping_list += yang.substmt( 'grouping' )
    typedef_list  += yang.substmt( 'typedef' )
    identity_list  += yang.substmt( 'identity' )
    self.children.push Container.new( yangs, container_arg, yang, self, parent_module )
    # when start
    yang.substmt( "when" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
    # must start
    yang.substmt( "must" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
  when Rubyang::Model::Leaf
    leaf_arg = yang.arg
    self.children.push Leaf.new( yangs, leaf_arg, yang, self, parent_module, current_module, typedef_list, identity_list )
  when Rubyang::Model::LeafList
    leaf_arg = yang.arg
    self.children.push LeafList.new( yangs, leaf_arg, yang, self, parent_module, current_module, typedef_list, identity_list )
    # min-elements start
    yang.substmt( "min-elements" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
    # max-elements start
    yang.substmt( "max-elements" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
  when Rubyang::Model::List
    list_arg = yang.arg
    grouping_list += yang.substmt( 'grouping' )
    typedef_list  += yang.substmt( 'typedef' )
    identity_list  += yang.substmt( 'identity' )
    self.children.push List.new( yangs, list_arg, yang, self, parent_module )
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # min-elements start
    yang.substmt( "min-elements" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
    # max-elements start
    yang.substmt( "max-elements" ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
    # end
  when Rubyang::Model::Choice
    choice_arg = yang.arg
    grouping_list += yang.substmt( 'grouping' )
    typedef_list  += yang.substmt( 'typedef' )
    identity_list  += yang.substmt( 'identity' )
    self.children.push Choice.new( yangs, choice_arg, yang, self, parent_module )
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
  when Rubyang::Model::Case
    case_arg = yang.arg
    grouping_list += yang.substmt( 'grouping' )
    typedef_list  += yang.substmt( 'typedef' )
    self.children.push Case.new( yangs, case_arg, yang, self, parent_module )
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      self.children.last.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
  when Rubyang::Model::Augment
    target_node = self.resolve_node( yang.arg )
    yang.substmts( Rubyang::Model::DataDefStmtList ).each{ |s|
      target_node.load_yang s, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list
    }
  when Rubyang::Model::Uses
    self.resolve_uses( yang, yangs, parent_module, current_module, grouping_list, typedef_list, identity_list )
    # when start
  when Rubyang::Model::When
    @logger.debug { 'when statement' }
    @logger.debug { '' }
    @logger.debug { 'yang' }
    @logger.debug { yang.to_yaml }
    self.whens.push When.new( self, yang )
    # end
    # must start
  when Rubyang::Model::Must
    @logger.debug { yang }
    self.musts.push Must.new( self, yang )
    # end
    # min-elements start
  when Rubyang::Model::MinElements
    @logger.debug { yang }
    self.min_elements.push MinElements.new( yang )
    # end
    # max-elements start
  when Rubyang::Model::MaxElements
    @logger.debug { yang }
    self.max_elements.push MaxElements.new( yang )
    # end
  else
    raise "#{yang.class} is not impletented yet"
  end
end

#modelObject



415
416
417
# File 'lib/rubyang/database/schema_tree.rb', line 415

def model
  @yang
end

#namespaceObject



418
419
420
# File 'lib/rubyang/database/schema_tree.rb', line 418

def namespace
  @module.substmt( 'namespace' )[0].arg
end

#prefixObject



421
422
423
# File 'lib/rubyang/database/schema_tree.rb', line 421

def prefix
  @module.substmt( 'prefix' )[0].arg
end

#rootObject



424
425
426
427
428
429
430
# File 'lib/rubyang/database/schema_tree.rb', line 424

def root
  if @parent == nil
    self
  else
    @parent.root
  end
end

#to_jsonObject



431
432
433
434
435
# File 'lib/rubyang/database/schema_tree.rb', line 431

def to_json
  h = Hash.new
  self.to_json_recursive( h )
  h.to_json
end

#to_s(parent = true) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/rubyang/database/schema_tree.rb', line 404

def to_s parent=true
  head, vars, tail = "#<#{self.class.to_s}:0x#{(self.object_id << 1).to_s(16).rjust(14,'0')} ", Array.new, ">"
  if parent
    vars.push "@yangs=#{@yangs.to_s}"
    vars.push "@arg=#{@arg.to_s}"
    vars.push "@yang=#{@yang.to_s(false) rescue @yang.to_s}"
    vars.push "@parent=#{@parent.to_s(false) rescue @parent.to_s}"
    vars.push "@module=#{@module.to_s(false) rescue @module.to_s}"
  end
  head + vars.join(', ') + tail
end