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.mask.rule.changed;
19  
20  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
21  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
22  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent;
23  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
24  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
25  import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
26  import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
27  import org.apache.shardingsphere.mask.metadata.nodepath.MaskRuleNodePathProvider;
28  import org.apache.shardingsphere.mask.rule.MaskRule;
29  import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
30  import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
31  import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
32  
33  import java.util.LinkedHashMap;
34  import java.util.LinkedList;
35  
36  /**
37   * Mask table changed processor.
38   */
39  public final class MaskTableChangedProcessor implements RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, MaskTableRuleConfiguration> {
40      
41      @Override
42      public MaskTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
43          return new YamlMaskTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlMaskTableRuleConfiguration.class));
44      }
45      
46      @Override
47      public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
48          return database.getRuleMetaData().findSingleRule(MaskRule.class)
49                  .map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
50      }
51      
52      private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) {
53          return null == config.getTables() ? new MaskRuleConfiguration(new LinkedList<>(), config.getMaskAlgorithms()) : config;
54      }
55      
56      @Override
57      public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig, final MaskTableRuleConfiguration toBeChangedItemConfig) {
58          // TODO refactor DistSQL to only persist config
59          currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
60          currentRuleConfig.getTables().add(toBeChangedItemConfig);
61      }
62      
63      @Override
64      public void dropRuleItemConfiguration(final DropRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig) {
65          currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName()));
66      }
67      
68      @Override
69      public String getType() {
70          return MaskRuleNodePathProvider.RULE_TYPE + "." + MaskRuleNodePathProvider.TABLES;
71      }
72  }