kongdeqiang
2025-12-02 7a2e82325091a39c32cf9940d0016178a3655dc9
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
package com.wgcloud.service;
 
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.OspfErrorLog;
import com.wgcloud.entity.OspfInfo;
import com.wgcloud.entity.SnmpInfo;
import com.wgcloud.entity.vo.OspfNeighbor;
import com.wgcloud.mapper.OspfErrorLogMapper;
import com.wgcloud.mapper.OspfInfoMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.OspfNeighborUtil;
import com.wgcloud.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:OspfInfoService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: OspfInfoService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class OspfInfoService {
 
    @Autowired
    private RouterConnectionService routerConnectionService;
    @Autowired
    private OspfNeighborUtil ospfNeighborUtil;
    @Autowired
    private OspfErrorLogMapper errorLogMapper;
 
    @Value("${osfService.host}")
    private String remoteHost;
    @Value("${osfService.port}")
    private String remotePort;
 
    private static final Logger logger = LoggerFactory.getLogger(OspfInfoService.class);
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<OspfInfo> list = OspfInfoMapper.selectByParams(params);
        PageInfo<OspfInfo> pageInfo = new PageInfo<OspfInfo>(list);
        return pageInfo;
    }
 
    public void saveRecord(List<OspfInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        for (OspfInfo as : recordList) {
            as.setId(UUIDUtil.getUUID());
            as.setCreateTime(DateUtil.getNowTime());
        }
        OspfInfoMapper.insertList(recordList);
    }
 
    public void save(OspfInfo ospfInfo) {
        ospfInfo.setId(UUIDUtil.getUUID());
        ospfInfo.setCreateTime(DateUtil.getNowTime());
        try {
            OspfInfoMapper.save(ospfInfo);
        } catch (Exception e) {
            logger.error("保存日志信息异常:", e);
        }
    }
 
    public void updateById(OspfInfo ospfInfo)
            throws Exception {
        if (StringUtils.isEmpty(ospfInfo.getHostname())) {
            ospfInfo.setHostname(ospfInfo.getHostname().trim());
        }
        OspfInfoMapper.updateById(ospfInfo);
    }
 
    /**
     * 获取详细的OSPF邻居信息
     */
    public void getDetailedOspfNeighborInfo() {
        List<OspfInfo> ospfInfos = OspfInfoMapper.selectAllByParams(null);
        for (OspfInfo ospfInfo : ospfInfos) {
            String command = "display ospf peer "+ospfInfo.getHostname();
            String result =  routerConnectionService.executeCommand(command,ospfInfo.getMonitorUsername(),ospfInfo.getMonitorPassword(),ospfInfo.getMonitorIp());
            System.out.println("获取到的邻居信息为:"+result);
            if(StringUtils.isNotBlank(result)){
                List<OspfNeighbor> neighbors = ospfNeighborUtil.analysisStr(result);
                if(neighbors != null || neighbors.size()>0){
                    OspfNeighbor ospfNeighbor = neighbors.get(0);
                    String state = ospfNeighbor.getState();
                    if (state.contains("Down") || state.contains("Init")) {
                        ospfInfo.setState("1");
                        OspfInfoMapper.updateById(ospfInfo);
 
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("state","1");
                        hashMap.put("hostname",ospfInfo.getHostname());
                        List<OspfErrorLog> ospfErrorLogs = errorLogMapper.selectAllByParams(hashMap);
                        if(ospfErrorLogs==null||ospfErrorLogs.size()==0){
                            OspfErrorLog ospfErrorLog = new OspfErrorLog();
                            ospfErrorLog.setInfoContent(ospfInfo.getInfoContent());
                            ospfErrorLog.setHostname(ospfInfo.getHostname());
                            ospfErrorLog.setState("1");
                            try {
                                errorLogMapper.save(ospfErrorLog);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }else {
                        ospfInfo.setState("0");
                        OspfInfoMapper.updateById(ospfInfo);
                    }
                }
            }else {
                ospfInfo.setState("1");
                OspfInfoMapper.updateById(ospfInfo);
            }
 
        }
    }
 
    public void getRouterStatus() {
        List<OspfInfo> ospfInfos = OspfInfoMapper.selectAllByParams(null);
        for (OspfInfo ospfInfo : ospfInfos) {
            String command = "display ospf peer "+ospfInfo.getHostname();
            Map<String,String> map = new HashMap<>();
            map.put("command",command);
            map.put("username",ospfInfo.getMonitorUsername());
            map.put("password",ospfInfo.getMonitorPassword());
            map.put("monitorIp",ospfInfo.getMonitorIp());
            String str = JSON.toJSONString(map);
            System.out.println(str);
            try {
                String url = "http://"+remoteHost+":"+remotePort+"/api/connect/getResult";
                String responseBody = HttpRequest.post(url)
                        .timeout(5000)
                        .body(str)
                        .execute()
                        .body();
                System.out.println(responseBody);
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 获取OSPF接口状态
     */
//    public String getOspfInterfaceStatus() {
//        String command = "display ospf interface";
//        return routerConnectionService.executeCommand(command);
//    }
 
 
 
 
    public int countByParams(Map<String, Object> params) throws Exception {
        return OspfInfoMapper.countByParams(params);
    }
 
    public int deleteById(String[] id) throws Exception {
        return OspfInfoMapper.deleteById(id);
    }
 
    public OspfInfo selectById(String id) throws Exception {
        return OspfInfoMapper.selectById(id);
    }
 
    public List<OspfInfo> selectAllByParams(Map<String, Object> params) throws Exception {
        return OspfInfoMapper.selectAllByParams(params);
    }
 
    public int deleteByDate(Map<String, Object> map) throws Exception{
        return OspfInfoMapper.deleteByDate(map);
    }
 
    @Autowired
    private OspfInfoMapper OspfInfoMapper;
 
 
}