liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.cloud.device.controller;
 
import com.cloud.common.exception.ApplicationException;
import com.cloud.device.constants.Constants;
import com.cloud.device.filter.AuthNoneIgnore;
import com.cloud.device.model.Cluster;
import com.cloud.device.model.Device;
import com.cloud.device.model.Node;
import com.cloud.device.service.ClusterService;
import com.cloud.device.service.DeviceService;
import com.cloud.device.service.NodeService;
import com.cloud.device.vo.ClusterNodeVo;
import com.cloud.device.vo.MenuTreeVo;
import com.cloud.device.vo.SharePersonParamVo;
import com.cloud.model.common.Page;
import com.cloud.model.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
 
import java.awt.*;
import java.util.*;
import java.util.List;
 
/**
 * 集群管理
 */
@Slf4j
@Api(value = "ClusterController", description = "集群管理")
@RequestMapping("/data/api-d/cluster")
@RestController
public class ClusterController {
 
    @Autowired
    private ClusterService clusterService;
 
    @Autowired
    private NodeService nodeService;
 
    @Autowired
    private DeviceService deviceService;
 
 
    @RequestMapping("/findClusterInfoByNodeIp")
    @ApiOperation(value = "通过集群内节点ip查找集群信息", notes = "通过集群内节点ip查找集群信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nodeIp", value = "节点ip地址", required = true, dataType = "String", paramType = "query")
    })
    public Result findClusterInfoByNodeIp(@RequestParam String nodeIp){
        try {
            if(StringUtils.isNotEmpty(nodeIp)){
                Map<String,Object> params = new HashMap<>();
                params.put("nodeIp", nodeIp);
                return Result.ok(Constants.RESULT_QUERY_OK, clusterService.findClusteInfoByNodeIp(params));
            }else
                return Result.error(new Exception("参数错误"));
 
        } catch (Exception e) {
            return Result.ok(Constants.RESULT_QUERY_FAIL, "未找到集群信息,请检查ip是否正确");
        }
    }
 
    @RequestMapping("/save")
    @ApiOperation(value = "保存集群和节点信息,包括节点下的设备信息", notes = "保存集群和节点信息,包括节点下的设备信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name="address",value="ip地址",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "cookie",value = "集群密码",required = true,dataType = "String",paramType = "query")
    })
    public Result save(String address, String cookie){
        try{
            if(StringUtils.isNotEmpty(address) && StringUtils.isNotEmpty(cookie)){
//                if(clusterService.verifyClusterPwdByCServer(address, cookie))
                return Result.ok(Constants.RESULT_SAVE_OK,clusterService.save(address, cookie));
            }
 
        }catch(Exception e){
            log.error(e.getLocalizedMessage());
        }
        return Result.custom("集群保存失败",500,false,"集群保存失败");
    }
 
    @GetMapping("/delete")
    @ApiOperation(value = "退出集群", notes = "退出集群", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cookie", value = "cookie", required = true, dataType = "String", paramType = "query")
    })
    public Result deleteCluster(String id, String cookie){
        try {
            Cluster cluster = clusterService.selectById(id);
            if(cluster != null && StringUtils.isNotEmpty(cluster.getAddress())){
//                if(clusterService.verifyClusterPwdByCServer(cluster.getAddress(), cookie)){
//                    if(clusterService.deleteById(id,cookie))
//                        return Result.ok(Constants.RESULT_DELETE_OK, "退出成功");
//
//                }else
//                    return Result.custom("退出失败,密码不匹配",500,false,"退出失败,密码不匹配");
 
                if(clusterService.deleteById(id,cookie)){
                    return Result.ok(Constants.RESULT_DELETE_OK, "退出成功");
                }else{
                    return Result.custom("退出失败,密码不匹配",401,false,"");
                }
            }
 
 
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
        return Result.custom("退出失败",500,false,"退出失败");
    }
 
    @RequestMapping("/getClusterDeviceTree")
    @ApiOperation(value = "获取集群设备树信息", notes = "获取集群设备树信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public List<MenuTreeVo> getClusterDeviceTree(){
        try {
            return clusterService.getClusterDeviceTree();
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return new ArrayList<>();
        }
    }
 
    @AuthNoneIgnore
    @RequestMapping("/findAllNode")
    @ApiOperation(value = "查询集群和节点信息", notes = "查询集群和节点信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "忽略,无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "集群名称,模糊查找", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isContainerDb", value = "是否返回数据库列表,不传表示不需要,需要请传:1", required = false, dataType = "String", paramType = "query"),
    })
    public Result findAllNode(@RequestParam Map<String, Object> params){
        try{
            return Result.ok(Constants.RESULT_QUERY_OK,clusterService.findAllNode(params));
        }catch(Exception e){
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
            return Result.error(e);
        }
 
    }
 
    @RequestMapping("/getAllCluListData")
    @ApiOperation(value = "获取所有集群设备级别信息,实时查询", notes = "获取所有集群设备级别信息,实时查询", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
 
    })
    public Result getAllCluListData(){
 
        try {
            return Result.ok(Constants.RESULT_QUERY_OK,clusterService.getAllCluListData());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/getAllCluDataBaseList")
    @ApiOperation(value = "获取集群节点及同步库信息",notes = "获取集群节点及同步库信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
 
    })
    public Result getAllCluDataBaseList(){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, clusterService.getAllCluDataBaseList());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/getClusterTreeData")
    @ApiOperation(value = "获取集群设备树(带全选)", notes = "获取集群设备树(带全选)", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result getClusterTreeData(){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, clusterService.getClusterTreeData());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @GetMapping("/findClusterList")
    @ApiOperation(value = "查询集群-节点-设备列表", notes = "查询集群-节点-设备列表", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "点击节点类型(1:集群,2:节点,3:设备)", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "点击节点id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "condition", value = "设备名称/设备地址", required = false, dataType = "String", paramType = "query")
    })
    public Result findClusterList(String type, String id, String condition){
        List<MenuTreeVo> result = new ArrayList<MenuTreeVo>();
 
        try {
            Cluster cluster = null;
            Node node =null;
            Device device = null;
            if(id.equals("0")){//表示查询所有集群信息
                //刷新整个集群的信息
 
                result = clusterService.getAllClusterMenuInfo(condition);
            }else{
                if(type.equals(Constants.TYPE_CLUSTER)){//点击集群
                    cluster = clusterService.selectById(id);
                }else if(type.equals(Constants.TYPE_NODE)){//点击节点
                    node = nodeService.selectById(id);
                    if(node !=null && StringUtils.isNotEmpty(node.getClusterId()))
                        cluster = clusterService.selectById(node.getClusterId());
                }else if(type.equals(Constants.TYPE_DEVICE)){//点击设备
                    device = deviceService.selectById(id);
                    if(device !=null && StringUtils.isNotEmpty(device.getNodeId())){
                        node = nodeService.selectById(device.getNodeId());
                        if(node !=null && StringUtils.isNotEmpty(node.getClusterId()))
                            cluster = clusterService.selectById(node.getClusterId());
                    }
                }
 
                if(cluster !=null && StringUtils.isNotEmpty(cluster.getAddress())){
                    String clusterIp = cluster.getAddress().split(":")[0];
                    //20190311,学校设备多了之后,从这里做同步速度很慢,而且取到的分析设备信息有误(后续同步功能由消息队列完成)
//                  clusterService.save(clusterIp, null);//刷新集群下所有节点和设备信息
 
                    result.add(clusterService.refreshCluster(clusterIp, type, id));
                }
            }
 
            return Result.ok(Constants.RESULT_QUERY_OK, result);
 
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/findNodeById")
    @ApiOperation(value = "根据节点Id,查询节点详细信息及归属的集群", notes = "根据节点Id,查询节点详细信息及归属的集群", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nodeIds", value = "节点Ids,多个用逗号分隔", required = false, dataType = "String", paramType = "query")
    })
    public Result findNodeById(String nodeIds){
        try{
            return Result.ok(Constants.RESULT_QUERY_OK,clusterService.findNodesByIds(nodeIds));
        }catch(Exception e){
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @PostMapping("/findClusterNodesByClusterIds")
    @ApiOperation(value = "通过集群ids查询集群及节点信息", notes = "通过集群ids查询集群及节点信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
 
    public Result findClusterNodesByClusterIds(@RequestBody Map<String,Object> params){
        try {
            String clusterIds = params.get("clusterIds").toString();
            return Result.ok(Constants.RESULT_QUERY_OK,clusterService.findClusterNodesByClusterIds(clusterIds));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
 
 
    public Result sharePersonToCluster(SharePersonParamVo sharePersonParamVo){
        try{
            return Result.ok("分享成功",clusterService.sharePersonToCluster(sharePersonParamVo));
        }catch(Exception e){
            e.printStackTrace();
            return Result.error(e);
        }
 
    }
}