AgentSyncDao.java 15.3 KB
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
package com.sitech.ibnms.config.sync.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.config.sync.SyncType;
import com.sitech.ibnms.config.sync.db.domain.AgentCollConfig;
import com.sitech.ibnms.config.sync.db.domain.AgentDetail;
import com.sitech.ibnms.config.sync.db.domain.MQDetail;
import com.sitech.ibnms.config.sync.db.domain.TbCfgEvent;
import com.sitech.ibnms.util.BaseDao;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.ibnms.util.SameGUID;
import com.sitech.ismp.messageObject.AgentSyncObject;
import com.sitech.ismp.messageObject.AlarmEventConfig;
import com.sitech.ismp.messageObject.AlarmEventConfigSyncObject;

public class AgentSyncDao extends BaseDao {

	public AgentDetail selectAgentDetail(String agentId) {
		try {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("AGENT_ID", agentId);
			return (AgentDetail) sqlmapClient.queryForObject(
					"selectAgentDetail", map);
		} catch (Exception e) {
			error.error("Exception while selectAgentDetail", e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<MQDetail> selectMqDetailList(String mqType) {
		try {
			return sqlmapClient.queryForList("selectMqDetailList", mqType);
		} catch (Exception e) {
			error.error("Exception while selectMqDetailList", e);
			return null;
		}
	}

	public MQDetail selectMqDetailByAgentId(String agentId) {
		try {
			return (MQDetail) sqlmapClient.queryForObject(
					"selectMqDetailByAgentId", agentId);
		} catch (Exception e) {
			error.error("Exception while selectMqDetailByAgentId:" + agentId, e);
			return null;
		}
	}

    /**
     * 查询链路采集配置
     */
    public List<AgentSyncObject> selectLinkCollList(String agentId) {
        List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();
        try{
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("AGENT_ID",agentId);
            List<HashMap<String,String>> list = sqlmapClient.queryForList("selectLinkCollList",params);
            Map<String,List> links = new HashMap<String, List>();
            List<String> linkIdList = new ArrayList<String>();
            for(int i=0;i<list.size();i++){
                HashMap<String,String> map = list.get(i);
                String linkId = map.get("LINK_ID").trim();
                if(null==links.get(linkId)){
                    List<HashMap<String,String>> linkList = new ArrayList<HashMap<String, String>>();
                    linkList.add(map);
                    linkIdList.add(linkId);
                    links.put(linkId,linkList);
                }else{
                    links.get(linkId).add(map);
                }
            }
            for(String linkId:linkIdList){
                List<HashMap<String,String>> link = links.get(linkId);
                String cronTab = link.get(0).get("PERIOD_ID").trim();
                AgentSyncObject obj = new AgentSyncObject();
                obj.setScheduleId(linkId);
                obj.setMbeanId("ED6CF9E44482013F321D96140A668908");
                obj.setClassName("com.sitech.ismp.coll.link.CollNetLink");
                obj.setObjectName("CollLink:type=Mbean");
                obj.setBeginTime(null);
                obj.setEndTime(null);
                obj.setCrontab(cronTab);
                obj.setFunction("getLinkPM");
                obj.setInterval(0);
                obj.setRepeatCount(0);
                HashMap<String, String> collParams = new HashMap<String, String>();
                collParams.put("LINK_ID",linkId);
                String json = JSONUtil.toJSON(link);
                collParams.put("JSON_STR",json);
                obj.setParams(collParams);
                result.add(obj);
            }

        }catch (Exception e){
            error.error("Exception while selectLinkCollList", e);
        }
        return result;
    }

	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectDevCollList(String agentId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();

		try {
			HashMap<String, String> map1 = new HashMap<String, String>();
			map1.put("AGENT_ID", agentId);

			List<AgentCollConfig> list = sqlmapClient.queryForList("getDevCollList", map1);
			for (AgentCollConfig config : list) {
				AgentSyncObject obj = getAgentSyncObject(config);
				result.add(obj);

				String scheduleId = SameGUID.getSameGUID(config.getMBEAN_ID()
						+ config.getDEVICE_ID() + config.getFUNCTION());
				obj.setScheduleId(scheduleId);

				// 查询采集参数
				HashMap<String, String> map2 = new HashMap<String, String>();
				map2.put("AGENT_ID", agentId);
				map2.put("MODEL_ID", config.getMBEAN_ID());
				map2.put("DEVICE_ID", config.getDEVICE_ID());
				List<HashMap<String, String>> paramList = sqlmapClient.queryForList("getDevParamList", map2);
				if (paramList != null && paramList.size() > 0) {
					for (HashMap<String, String> paramMap : paramList) {
						String paramName = paramMap.get("PARAM_NAME");
						String paramValue = paramMap.get("PARAM_VALUE");
						obj.getParams().put(paramName, paramValue);
					}
				}
				
				try{
					if("getPort".equals(config.getFUNCTION().trim())){
						// 网络端口采集配置
						List<HashMap<String, String>> netIfList = sqlmapClient.queryForList("getNetIfList", map2);
						if (null != netIfList && !netIfList.isEmpty()) {
							List<String> ifList = new ArrayList<String>();
							for(HashMap<String, String> netIf : netIfList){
								ifList.add(netIf.get("UNIT_ID"));
							}
							obj.getParams().put("IF_FILTER", JSONUtil.toJSON(ifList));
						}
					}
				}catch(Exception e){
					error.error("Exception while getNetIfList", e);
				}

			}
		} catch (Exception e) {
			error.error("Exception while selectDevCollList", e);
		}

		return result;
	}

	/**
	 * 查询业务采集配置
	 */
	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectBusiCollList(String agentId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();

		try {
			HashMap<String, String> map1 = new HashMap<String, String>();
			map1.put("AGENT_ID", agentId);
			List<AgentCollConfig> list = sqlmapClient.queryForList(
					"getBusiCollList", map1);
			for (AgentCollConfig config : list) {
				AgentSyncObject obj = getAgentSyncObject(config);
				result.add(obj);

				// 查询采集参数
				HashMap<String, String> map2 = new HashMap<String, String>();
				map2.put("AGENT_ID", agentId);
				map2.put("SCHEDULE_ID", config.getSCHEDULE_ID());
				List<HashMap<String, String>> paramList = sqlmapClient
						.queryForList("getBusiParamList", map2);
				if (paramList != null && paramList.size() > 0) {
					for (HashMap<String, String> paramMap : paramList) {
						String paramName = paramMap.get("PARAM_NAME");
						String paramValue = paramMap.get("PARAM_VALUE");
						obj.getParams().put(paramName, paramValue);
					}
				}
			}

		} catch (Exception e) {
			error.error("Exception while selectAgentDetail", e);
		}

		return result;
	}

	/**
	 * 查询智能巡检配置
	 */
	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectCheckList(String agentId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();
		try {

			HashMap<String, String> map1 = new HashMap<String, String>();
			map1.put("AGENT_ID", agentId);
			List<AgentCollConfig> list = sqlmapClient.queryForList(
					"getDevCheckList", map1);
			for (AgentCollConfig config : list) {
				result.add(getAgentSyncObject(config));
			}
		} catch (Exception e) {
			error.error("Exception while selectAgentDetail", e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public AlarmEventConfigSyncObject selectEventCfgList(String agentId) {
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("AGENT_ID", agentId);
			
			
			String sql = "select distinct t.*\n"
					+ "  from tb_cfg_event t,\n"
					+ "       (select agent_id,\n"
					+ "               unit_id,\n"
					+ "               substr(unit_id, 0, decode(instr(unit_id, ':'), 0, length(unit_id), instr(unit_id, ':') - 1)) kbp_class,\n"
					+ "               decode(instr(unit_id, ':'), 0, '', substr(unit_id, instr(unit_id, ':') + 1)) device_id\n"
					+ "          from v_agent_res) v\n"
					+ " where t.ENABLE = 1\n"
					+ "   and t.IS_ORG_TYPE = 1\n"
					+ "   and v.agent_id = '"+agentId+"'\n"
					+ "   and v.UNIT_ID is not null\n"
					+ "   and (t.unit_id like v.kbp_class || '%' || v.DEVICE_ID || '%')\n";

			logger.info(sql);
			
			List<TbCfgEvent> tbCfgEventList = sqlmapClient.queryForList(
					"selectTbCfgEventByAgentId", params);
			
			logger.info("-- return " + tbCfgEventList.size());

			return getEventSyncObject(agentId, SyncType.SYNC_ALL,
					tbCfgEventList);
		} catch (Exception e) {
			error.error("Exception while selectEventCfgList(" + agentId + ")",
					e);
			return null;
		}

	}

	private AlarmEventConfigSyncObject getEventSyncObject(String agentId,
			int type, List<TbCfgEvent> tbCfgEventList) {
		AlarmEventConfigSyncObject obj = new AlarmEventConfigSyncObject();
		obj.setAgentId(agentId);
		obj.setSyncType(type);
		List<AlarmEventConfig> list = new ArrayList<AlarmEventConfig>();
		obj.setConfigList(list);

		for (TbCfgEvent elem : tbCfgEventList) {
			AlarmEventConfig config = new AlarmEventConfig();
			String guid = elem.getGUID();
			String kpiId = elem.getKPI_ID();
			String unitId = elem.getUNIT_ID();
			String expression = elem.getEXPRESSION() == null ? "" : elem
					.getEXPRESSION();
			String expression1 = elem.getEXPRESSION1() == null ? "" : elem
					.getEXPRESSION1();
			String expression2 = elem.getEXPRESSION2() == null ? "" : elem
					.getEXPRESSION2();
			String expression3 = elem.getEXPRESSION3() == null ? "" : elem
					.getEXPRESSION3();
			String expression4 = elem.getEXPRESSION4() == null ? "" : elem
					.getEXPRESSION4();
			String expression5 = elem.getEXPRESSION5() == null ? "" : elem
					.getEXPRESSION5();
			String expression6 = elem.getEXPRESSION6() == null ? "" : elem
					.getEXPRESSION6();
			int enable = Integer.parseInt(elem.getENABLE());
			String extUnitId = elem.getEXT_UNIT_ID() == null ? "" : elem
					.getEXT_UNIT_ID();

			config.setGuid(guid);
			config.setKpiId(kpiId);
			config.setUnitId(unitId);
			config.setExpression(expression);
			config.setExpression1(expression1);
			config.setExpression2(expression2);
			config.setExpression3(expression3);
			config.setExpression4(expression4);
			config.setExpression5(expression5);
			config.setExpression6(expression6);
			config.setEnable(enable);
			config.setExtUnitId(extUnitId);

			list.add(config);
		}
		return obj;
	}

	private AgentSyncObject getAgentSyncObject(AgentCollConfig config) {
		AgentSyncObject obj = new AgentSyncObject();
		obj.setScheduleId(config.getSCHEDULE_ID());
		obj.setMbeanId(config.getMBEAN_ID());
		obj.setClassName(config.getCLASS_NAME());
		obj.setObjectName(config.getOBJECT_NAME());
		obj.setBeginTime(config.getBEGIN_TIME());
		obj.setEndTime(config.getEND_TIME());
		obj.setCrontab(config.getCRONTAB());
		obj.setFunction(config.getFUNCTION());
		obj.setInterval(config.getINTERVAL());
		obj.setRepeatCount(config.getREPEAT_COUNT());

		HashMap<String, String> params = new HashMap<String, String>();
		obj.setParams(params);
		return obj;
	}

	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectDevCollCfgSpec(String agentId,
			String modelId, String deviceId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();

		try {
			HashMap<String, String> map1 = new HashMap<String, String>();
			map1.put("AGENT_ID", agentId);
			map1.put("MBEAN_ID", modelId);
			map1.put("DEVICE_ID", deviceId);

			List<AgentCollConfig> list = sqlmapClient.queryForList(
					"getDevCollListSpec", map1);
			for (AgentCollConfig config : list) {
				AgentSyncObject obj = getAgentSyncObject(config);
				result.add(obj);

				String scheduleId = SameGUID.getSameGUID(config.getMBEAN_ID()
						+ config.getDEVICE_ID() + config.getFUNCTION());
				obj.setScheduleId(scheduleId);

				// 查询采集参数
				HashMap<String, String> map2 = new HashMap<String, String>();
				map2.put("AGENT_ID", agentId);
				map2.put("MODEL_ID", modelId);
				map2.put("DEVICE_ID", deviceId);
				List<HashMap<String, String>> paramList = sqlmapClient
						.queryForList("getDevParamList", map2);
				if (paramList != null && paramList.size() > 0) {
					for (HashMap<String, String> paramMap : paramList) {
						String paramName = paramMap.get("PARAM_NAME");
						String paramValue = paramMap.get("PARAM_VALUE");
						obj.getParams().put(paramName, paramValue);
					}
				}
			}

		} catch (Exception e) {
			error.error("Exception while selectDevCollCfgSpec(" + agentId
					+ ")", e);
			return null;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectBusiCollCfgSpec(String agentId,
			String scheduleId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();
		try {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("AGENT_ID", agentId);
			map.put("SCHEDULE_ID", scheduleId);
			List<AgentCollConfig> list = sqlmapClient.queryForList(
					"getBusiCollListSpec", map);
			for (AgentCollConfig config : list) {
				AgentSyncObject obj = getAgentSyncObject(config);
				result.add(obj);

				List<HashMap<String, String>> paramList = sqlmapClient
						.queryForList("getBusiParamList", map);
				if (paramList != null && paramList.size() > 0) {
					for (HashMap<String, String> paramMap : paramList) {
						String paramName = paramMap.get("PARAM_NAME");
						String paramValue = paramMap.get("PARAM_VALUE");
						obj.getParams().put(paramName, paramValue);
					}
				}
			}
		} catch (Exception e) {
			error.error("Exception while selectBusiCollCfgSpec(" + scheduleId
					+ ")", e);
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	public List<AgentSyncObject> selectCheckCfgSpec(String agentId) {
		List<AgentSyncObject> result = new ArrayList<AgentSyncObject>();

		HashMap<String, String> map1 = new HashMap<String, String>();
		map1.put("AGENT_ID", agentId);
		List<AgentCollConfig> list;
		try {
			list = sqlmapClient.queryForList("getDevCheckList", map1);

			for (AgentCollConfig config : list) {
				result.add(getAgentSyncObject(config));
			}
		} catch (SQLException e) {
			error.error("Exception while selectCheckCfgSpec(" + agentId + ")",
					e);
		}

		return result;
	}

	/**
	 * 查询与UnitId相关的AgentId
	 */
	@SuppressWarnings("unchecked")
	public List<String> queryAgentIdByUnitId(String unitId) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("UNIT_ID", unitId);
		try {
			return sqlmapClient.queryForList("queryAgentIdByUnitId", params);
		} catch (SQLException e) {
			error.error(
					"Exception while queryAgentIdByUnitId(" + unitId + ")", e);
			return null;
		}
	}

    public String getActProvince(){
        HashMap<String, String> params = new HashMap<String, String>();
        try {
            return (String) sqlmapClient.queryForObject("getActProvince", params);
        } catch (SQLException e) {
            error.error(
                    "Exception while getActProvince", e);
            return null;
        }
    }

	public List<HashMap<String, String>> selectCronTask(String agentId) throws SQLException {
		return sqlmapClient.queryForList("selectCronTask", agentId);
	}

	public List<String> selectCronTaskContent(String version) throws SQLException{
		return sqlmapClient.queryForList("selectCronTaskContent", version);
	}


}