1   /*
2    * Copyright 2023 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.xds.internal;
18  
19  import java.util.concurrent.CompletionStage;
20  
21  import com.google.common.base.MoreObjects;
22  
23  import com.linecorp.armeria.common.annotation.Nullable;
24  import com.linecorp.armeria.common.util.UnmodifiableFuture;
25  import com.linecorp.centraldogma.server.plugin.AllReplicasPlugin;
26  import com.linecorp.centraldogma.server.plugin.PluginContext;
27  import com.linecorp.centraldogma.server.plugin.PluginInitContext;
28  import com.linecorp.centraldogma.server.plugin.PluginTarget;
29  import com.linecorp.centraldogma.server.storage.project.InternalProjectInitializer;
30  
31  public final class ControlPlanePlugin extends AllReplicasPlugin {
32  
33      public static final String XDS_CENTRAL_DOGMA_PROJECT = "@xds";
34  
35      @Nullable
36      private volatile ControlPlaneService controlPlaneService;
37  
38      @Override
39      public void init(PluginInitContext pluginInitContext) {
40          final InternalProjectInitializer projectInitializer = pluginInitContext.internalProjectInitializer();
41          projectInitializer.initialize(XDS_CENTRAL_DOGMA_PROJECT);
42          final ControlPlaneService controlPlaneService = new ControlPlaneService(
43                  pluginInitContext.projectManager().get(XDS_CENTRAL_DOGMA_PROJECT),
44                  pluginInitContext.meterRegistry());
45          this.controlPlaneService = controlPlaneService;
46          controlPlaneService.start(pluginInitContext);
47      }
48  
49      @Override
50      public CompletionStage<Void> start(PluginContext context) {
51          return UnmodifiableFuture.completedFuture(null);
52      }
53  
54      @Override
55      public CompletionStage<Void> stop(PluginContext context) {
56          final ControlPlaneService controlPlaneService = this.controlPlaneService;
57          if (controlPlaneService != null) {
58              controlPlaneService.stop();
59          }
60          return UnmodifiableFuture.completedFuture(null);
61      }
62  
63      @Override
64      public Class<?> configType() {
65          return ControlPlanePluginConfig.class;
66      }
67  
68      @Override
69      public String toString() {
70          return MoreObjects.toStringHelper(this)
71                            .add("configType", configType().getName())
72                            .add("target", PluginTarget.ALL_REPLICAS)
73                            .toString();
74      }
75  }