View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.traffic.distsql.parser.core;
19  
20  import org.antlr.v4.runtime.tree.ParseTree;
21  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementBaseVisitor;
22  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.AlgorithmDefinitionContext;
23  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.AlterTrafficRuleContext;
24  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.LabelDefinitionContext;
25  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.LoadBalancerDefinitionContext;
26  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.PropertiesDefinitionContext;
27  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.PropertyContext;
28  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.ShowTrafficRulesContext;
29  import org.apache.shardingsphere.distsql.parser.autogen.TrafficDistSQLStatementParser.TrafficRuleDefinitionContext;
30  import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
31  import org.apache.shardingsphere.sql.parser.api.ASTNode;
32  import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
33  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
34  import org.apache.shardingsphere.traffic.distsql.segment.TrafficRuleSegment;
35  import org.apache.shardingsphere.traffic.distsql.statement.queryable.ShowTrafficRulesStatement;
36  import org.apache.shardingsphere.traffic.distsql.statement.updatable.AlterTrafficRuleStatement;
37  
38  import java.util.Collection;
39  import java.util.Collections;
40  import java.util.Properties;
41  import java.util.stream.Collectors;
42  
43  /**
44   * SQL statement visitor for traffic DistSQL.
45   */
46  public final class TrafficDistSQLStatementVisitor extends TrafficDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
47      
48      @Override
49      public ASTNode visitAlterTrafficRule(final AlterTrafficRuleContext ctx) {
50          return new AlterTrafficRuleStatement(ctx.trafficRuleDefinition().stream().map(each -> (TrafficRuleSegment) visit(each)).collect(Collectors.toList()));
51      }
52      
53      @Override
54      public ASTNode visitTrafficRuleDefinition(final TrafficRuleDefinitionContext ctx) {
55          AlgorithmSegment loadBalancerSegment = null == ctx.loadBalancerDefinition() ? null : (AlgorithmSegment) visit(ctx.loadBalancerDefinition().algorithmDefinition());
56          return new TrafficRuleSegment(
57                  getIdentifierValue(ctx.ruleName()), buildLabels(ctx.labelDefinition()), (AlgorithmSegment) visit(ctx.trafficAlgorithmDefinition().algorithmDefinition()), loadBalancerSegment);
58      }
59      
60      private Collection<String> buildLabels(final LabelDefinitionContext labelDefinition) {
61          return null == labelDefinition ? Collections.emptyList() : labelDefinition.label().stream().map(this::getIdentifierValue).collect(Collectors.toList());
62      }
63      
64      @Override
65      public ASTNode visitLoadBalancerDefinition(final LoadBalancerDefinitionContext ctx) {
66          return null == ctx ? null : visit(ctx.algorithmDefinition());
67      }
68      
69      @Override
70      public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
71          return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), buildProperties(ctx.propertiesDefinition()));
72      }
73      
74      private Properties buildProperties(final PropertiesDefinitionContext ctx) {
75          Properties result = new Properties();
76          if (null == ctx) {
77              return result;
78          }
79          for (PropertyContext each : ctx.properties().property()) {
80              result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
81          }
82          return result;
83      }
84      
85      @Override
86      public ASTNode visitShowTrafficRules(final ShowTrafficRulesContext ctx) {
87          return new ShowTrafficRulesStatement(null == ctx.ruleName() ? null : getIdentifierValue(ctx.ruleName()));
88      }
89      
90      private String getIdentifierValue(final ParseTree context) {
91          return null == context ? null : new IdentifierValue(context.getText()).getValue();
92      }
93  }