Firewall-Policy-Designer

 view release on metacpan or  search on metacpan

lib/Firewall/Policy/Designer/Huawei.pm  view on Meta::CPAN

package Firewall::Policy::Designer::Huawei;

#------------------------------------------------------------------------------
# 引用基础模块
#------------------------------------------------------------------------------
use Moose;
use namespace::autoclean;

#------------------------------------------------------------------------------
# 引用项目模块
#------------------------------------------------------------------------------
use Firewall::Utils::Ip;
use Firewall::Utils::Set;
use Firewall::Policy::Searcher::Report::FwInfo;

#------------------------------------------------------------------------------
# 继承 Firewall::Policy::Designer::Role 通用属性
#------------------------------------------------------------------------------
# with 'Firewall::Policy::Designer::Role';

#------------------------------------------------------------------------------
# Firewall::Policy::Designer::Huawei 通用属性
#------------------------------------------------------------------------------
has dbi => ( is => 'ro', does => 'Firewall::DBI::Role', required => 1, );

has searcherReportFwInfo => ( is => 'ro', isa => 'Firewall::Policy::Searcher::Report::FwInfo', required => 1, );

has commandText => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, );

#------------------------------------------------------------------------------
# addToCommandText 向 commandText 属性推送命令行
#------------------------------------------------------------------------------
sub addToCommandText {
  my ( $self, @commands ) = @_;
  push @{$self->{commandText}}, @commands;
}

#------------------------------------------------------------------------------
# design 策略设计函数入口,入参为防火墙策略报告 -> searcherReportFwInfo
#------------------------------------------------------------------------------
sub design {
  my $self = shift;

  # 初始化防火墙策略报告下 type action 属性
  my $type   = $self->{searcherReportFwInfo}{type}   if exists $self->{searcherReportFwInfo}{type};
  my $action = $self->{searcherReportFwInfo}{action} if exists $self->{searcherReportFwInfo}{action};

  # 情况1:当类型为 new,则新建策略
  if ( $type eq "new" ) {
    $self->createRule();
  }

  # 情况2:当类型为 modify,则修改策略
  elsif ( $type eq "modify" ) {
    $self->createRule();
  }

  # 情况3:当类型为 ignore,还需要检查是否存在 NAT 策略
  elsif ( $type eq "ignore" ) {
    my $param = $action->{new} if defined $action;
    for my $natDirection ( keys %{$param} ) {
      if ( $natDirection eq "natSrc" || $natDirection eq "natDst" ) {
        $self->checkAndCreateNat( $param->{$natDirection}, $type );
      }
    }
  }

  # 其他情况:抛出异常,定位排除
  else {
    warn "ERROR: searcherReportFwInfo->type(" . $self->{"searcherReportFwInfo"}{"type"} . ") must be 'new' or 'modify'";
    confess $@;
  }

  # 拼接数组为字符串并返回
  return join( "\n", @{$self->{"commandText"}} );
} ## end sub design

#------------------------------------------------------------------------------
# design 策略设计函数入口,入参为防火墙策略报告 -> searcherReportFwInfo
#------------------------------------------------------------------------------
sub createRule {
  my $self = shift;

  # 初始化 commands
  my @commands;

  # 判断是否需要 nat
  my $action = $self->searcherReportFwInfo->action->{new};



( run in 0.651 second using v1.01-cache-2.11-cpan-39bf76dae61 )