package com.bocloud.ims.service.resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.secuirty.FirewallDriver;
import com.bocloud.cmp.driver.secuirty.SangforFirewallRuleDriver;
import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.model.FirewallRuleModel;
import com.bocloud.cmp.model.sangfor.SangforFirewallRuleModel;
import com.bocloud.cmp.model.sangfor.SangforIpgpModel;
import com.bocloud.cmp.model.sangfor.SangforServiceModel;
import com.bocloud.common.model.*;
import com.bocloud.common.model.RequestContext.Catalog;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.*;
import com.bocloud.ims.entity.resource.manageone.ManageOneAccount;
import com.bocloud.ims.interfaces.manageone.ManageOneAccountService;
import com.bocloud.ims.interfaces.resource.FirewallRuleService;
import com.bocloud.ims.interfaces.resource.VendorProjectService;
import com.bocloud.ims.interfaces.resource.sangfor.SangforAzService;
import com.bocloud.ims.interfaces.resource.sangfor.SangforFireWallService;
import com.bocloud.ims.repository.resource.*;
import com.bocloud.ims.service.transporter.vendor.DataUtil;
import com.bocloud.ims.service.utils.ResourceEventPublisher;
import com.bocloud.ims.service.utils.SangforConvertUtils;
import com.bocloud.service.lock.BocloudACLock;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 防火墙规则service实现
 *
 * @author wangyu
 * @version 1.0
 * @since 2017年8月31日
 */
@Service("firewallRuleService")
public class FirewallRuleServiceImpl implements FirewallRuleService {
	private static Logger logger = LoggerFactory.getLogger(FirewallRuleServiceImpl.class);
	@Autowired
	private FirewallRuleRepository firewallRuleRepository;
	@Autowired
	private FirewallPolicyRepository firewallPolicyRepository;
	@Autowired
	private FirewallDriver firewallDriver;
	@Autowired
	private CloudVendorRepository cloudVendorRepository;
	@Autowired
	private OSTenantRepository osTenantRepository;
	@Autowired
	private VendorProjectService vendorProjectService;
	@Autowired
	private ResourceEventPublisher resourceEventPublisher;
	@Autowired
	private FirewallRepository firewallRepository;
	@Autowired
	private ManageOneAccountService accountService;
	@Autowired
	private LockFactory lockFactory;
	@Autowired
	SangforFirewallRuleDriver sangforFirewallRuleDriver;
	@Autowired
	private SangforAzService sangforAzService;
	@Autowired
	private SangforFireWallService sangforFireWallService;

	@Override
	public BocloudResult<GridBean<FirewallRule>> list(Pager pager, RequestContext context) {
		try {
			List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
			Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);

			Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newLinkedHashMap());
			if (sorter.getClass().equals(HashMap.class)) {
				sorter = new LinkedHashMap<>(sorter);
				pager.setSorter(sorter);
			}
			sorter.put("azId", Common.ZERO);
			sorter.put("name", Common.ZERO);

			GridBean gridBean = null;
			Catalog catalog = context.getCatalog();
			String vendorType = null;
			if (Catalog.Tenant == catalog || Catalog.User == catalog) {
				for (Param param : params) {
					Map<String, Object> paramMap = param.getParam();
					if (paramMap.containsKey("vendorType")) {
						vendorType = String.valueOf(paramMap.get("vendorType"));
						paramMap.remove("vendorType");
					}
				}
			}
			if (0 != context.getTenant()) {
				params.add(new Param(MapTools.simpleMap(Common.TENANTID, context.getTenant()), Sign.EQ));
			}
			int total = firewallRuleRepository.count(params, vendorType);
			if (simple) {
				if (0 == context.getTenant()) {
					params.add(new Param(MapTools.simpleMap(Common.TENANTID, 0), Sign.EQ));
				}
				List<SimpleBean> beans = firewallRuleRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				List<FirewallRule> list = firewallRuleRepository.list(pager.getPage(), pager.getRows(), params, sorter,
						vendorType);
				Map<String, String> allAzUuidNameMap = sangforAzService.getAllAzUuidNameMap();

				String azUuid, azName;
				for (FirewallRule fr : list) {
					azUuid = fr.getAzId();
					if (StringUtils.isNotEmpty(azUuid)) {
						azName = allAzUuidNameMap.get(azUuid);
						if (StringUtils.isNotEmpty(azName)) {
							fr.setAzName(azName);
						}
					}
				}

				gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list);
			}
			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list firewall rule failure:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	@Override
	public BocloudResult condition(String condition, RequestContext context) {
		try {
			JSONObject object = JSONObject.parseObject(condition);
			if ("listRuleByNoBoundPolicy".equalsIgnoreCase(object.getString("condition"))) {
				return this.listRuleByNoBoundPolicy(object.getLong("vendorId"), context);
			} else if ("listRuleByPolicyId".equalsIgnoreCase(object.getString("condition"))) {
				return this.listRuleByPolicyId(object.getLong("policyId"));
			}
			return null;
		} catch (Exception e) {
			logger.error("Get error message:{}", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	@Override
	public BocloudResult<FirewallRule> create(FirewallRule rule, RequestContext context) {
		try {
			CloudVendor vendor = cloudVendorRepository.query(rule.getVendorId());
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			if (CloudProvider.MANAGEONE.name().equals(vendor.getType())) {
				return manageOneCreate(rule, vendor, context);
			}
			FirewallRule exist = firewallRuleRepository.getByName(rule.getName(), rule.getVendorId());
			if (null != exist) {
				return new BocloudResult<>(false, "防火墙规则名称已存在");
			}
			FirewallRuleModel model = JSONObject.parseObject(JSONObject.toJSONString(rule), FirewallRuleModel.class);
			BocloudResult result = null;
			if (CloudProvider.SANGFOR.name().equals(vendor.getType())){
				SangforFirewallRuleModel ruleModel = new SangforFirewallRuleModel();
				ruleModel.setName(rule.getName());
				ruleModel.setEnable(rule.getEnabled());
				ruleModel.setAction(rule.getAction());
				ruleModel.setSrc_type(rule.getSrcType());
				ruleModel.setDst_type(rule.getDstType());
				ruleModel.setSrv(rule.getSrv() == null? null:JSONObject.parseArray(rule.getSrv()));
				ruleModel.setDomain_id(rule.getDomainId());
				ruleModel.setSrvFlag(rule.getSrvFlag());
				ruleModel.setSrcip(rule.getSrcip() == null ? null:JSONObject.parseArray(rule.getSrcip()));
				ruleModel.setDstip(rule.getDstip() == null ? null:JSONObject.parseArray(rule.getDstip()));
				ruleModel.setSrc_vm(rule.getSrcVm());
				ruleModel.setDst_vm(rule.getDstVm());
				ruleModel.setSrcipg(rule.getSrcipg());
				ruleModel.setDstipg(rule.getDstipg());
				result = sangforFirewallRuleDriver.create(vendor.getUuid(),ruleModel);
			} else {
				OSTenant osTenant = osTenantRepository.getByTenantId(context.getTenant(), vendor.getId());
				if (null == osTenant) {
					result = firewallDriver.createRule(model, vendor.getUuid(), rule.getOsTenantName());
				} else {
					result = firewallDriver.createRule(model, vendor.getUuid(), osTenant.getName());
				}
			}

			if (result.isFailed()) {
				return new BocloudResult<>(false, result.getMessage());
			}
			if (CloudProvider.SANGFOR.name().equals(vendor.getType())){
				String uuid = result.getData().toString();
				rule.setRuleUuid(uuid);
				rule.setGmtCreate(new Date());
				rule.setGmtModify(new Date());
				rule.setCreatorId(1L);
				rule.setDeleted(false);
				firewallRuleRepository.save(rule);
				new Thread(()->{
					BocloudResult list = sangforFirewallRuleDriver.list(vendor.getUuid());
					if (list.isSuccess() && list.getData() != null){
						List<SangforFirewallRuleModel> ruleModelList = JSON.parseArray(JSON.toJSON(list.getData()).toString(), SangforFirewallRuleModel.class);
						List<FirewallRule> firewallRules = SangforConvertUtils.firewallConver(ruleModelList)
								.stream().filter(e-> rule.getRuleUuid().equals(e.getRuleUuid())).collect(Collectors.toList());
						if (!firewallRules.isEmpty()){
							FirewallRule firewallRule = firewallRules.get(0);
							com.bocloud.ims.service.utils.BeanUtils.copyPropertiesIgnoreNull(firewallRule,rule);
							firewallRuleRepository.update(rule);
						} else {
							logger.error("未找新增的网络策略!");
						}
					} else {
						logger.error("获取新增网络策略失败!");
					}
				}).start();
				return new BocloudResult<>(true, rule, "创建成功!");
			}
			model = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), FirewallRuleModel.class);
			BeanUtils.copyProperties(model, rule);
			rule.preSave(context.getTarget(), context.getTarget(), rule.getVendorId(), context.getTenant(),
					context.getProject());
			rule.setId(null);
			firewallRuleRepository.save(rule);
			return new BocloudResult<>(true, rule, "创建成功!");
		} catch (Exception e) {
			logger.error("create firewall policy fail:", e);
			return new BocloudResult<>(false, "创建防火墙策略失败");
		}
	}

	private BocloudResult manageOneCreate(FirewallRule rule, CloudVendor vendor, RequestContext context)
			throws Exception {
		Firewall firewall = firewallRepository.query(rule.getFirewallId());
		if (null == firewall) {
			return new BocloudResult<>(false, "防火墙数据异常！");
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				String operate = vendor.getType().toLowerCase() + ".firewallRule";
				try {
					FirewallRuleModel model = JSONObject.parseObject(JSONObject.toJSONString(rule),
							FirewallRuleModel.class);
					model.setPolicyUuid(firewall.getPolicyUuid());
					BocloudResult result = new BocloudResult<>(false, "操作失败！");
					if (null != context.getTenant() && 0L != context.getTenant()) {
						ManageOneAccount account = accountService.queryAccount(vendor.getId(), context.getTenant());
						Assert.notNull(account, "认证失败！");
						result = firewallDriver.createRule(vendor.getUuid(), account.getUsername(),
								account.getPassword(), account.getProjectId(), account.getDomainName(), model);
					} else {
						result = firewallDriver.createRule(model, vendor.getUuid());
					}
					if (result.isFailed()) {
						resourceEventPublisher.send(new OperateResult(false, result.getMessage(), operate,
								MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
						return;
					}
					model = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), FirewallRuleModel.class);
					BeanUtils.copyProperties(model, rule);
					rule.setPolicyUuid(firewall.getPolicyUuid());
					rule.setPolicyId(firewall.getPolicyId());
					rule.preSave(context.getTarget(), context.getTarget(), rule.getVendorId(), context.getTenant(),
							context.getProject());
					rule.setId(null);
					rule.setVendorType(vendor.getType());
					firewallRuleRepository.save(rule);
					resourceEventPublisher.send(new OperateResult(true, "防火墙规则创建成功！", operate,
							MapTools.simpleMap("vendorId", vendor.getId()), OperateResult.OperateCategory.ResourceEvent,
							context));
				} catch (Exception e) {
					logger.error("create firewall policy fail:", e);
					resourceEventPublisher.send(new OperateResult(false, "create firewall rule fail:" + e.getMessage(),
							operate, MapTools.simpleMap("vendorId", vendor.getId()),
							OperateResult.OperateCategory.ResourceEvent, context));
				}
			}
		}).start();
		return new BocloudResult<>(true, "任务已下发，正在执行......");
	}

	@Override
	public BocloudResult<FirewallRule> modify(FirewallRule bean, Long userId) {
		String path = FirewallPolicy.class.getSimpleName() + "_" + bean.getId();
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			FirewallRule rule = firewallRuleRepository.query(bean.getId());
			if (null == rule) {
				return new BocloudResult<>(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(rule.getVendorId());
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			// 修改防火墙规则信息时名称不能与已有名称重复
			if (!CloudProvider.MANAGEONE.name().equals(vendor.getType()) && !bean.getName().equals(rule.getName())) {
				FirewallRule exist = firewallRuleRepository.getByName(bean.getName(), vendor.getId());
				if (null != exist) {
					return new BocloudResult<>(false, "防火墙规则名称已存在");
				}
			}
			FirewallRuleModel model = null;
			BocloudResult bocloudResult = null;
			if(CloudProvider.SANGFOR.name().equals(vendor.getType())){
				SangforFirewallRuleModel ruleModel = new SangforFirewallRuleModel();
				ruleModel.setId(bean.getRuleUuid());
				ruleModel.setName(bean.getName());
				ruleModel.setEnable(bean.getEnabled());
				ruleModel.setAction(bean.getAction());
				ruleModel.setSrc_type(bean.getSrcType());
				ruleModel.setDst_type(bean.getDstType());
				ruleModel.setSrv(bean.getSrv() == null? null:JSONObject.parseArray(bean.getSrv()));
				ruleModel.setSrvFlag(bean.getSrvFlag());
				ruleModel.setDomain_id(bean.getDomainId());
				ruleModel.setSrcip(bean.getSrcip() == null ? null:JSONObject.parseArray(bean.getSrcip()));
				ruleModel.setDstip(bean.getDstip() == null ? null:JSONObject.parseArray(bean.getDstip()));
				ruleModel.setSrc_vm(bean.getSrcVm());
				ruleModel.setDst_vm(bean.getDstVm());
				ruleModel.setSrcipg(bean.getSrcipg());
				ruleModel.setDstipg(bean.getDstipg());
				bocloudResult = sangforFirewallRuleDriver.updateRule(vendor.getUuid(),ruleModel);
			} else {
				model = JSONObject.parseObject(JSONObject.toJSONString(bean), FirewallRuleModel.class);
				model.setRuleUuid(rule.getRuleUuid());
				bocloudResult = firewallDriver.updateRule(model, vendor.getUuid());
			}
			if (bocloudResult.isFailed()) {
				return new BocloudResult<>(false, bocloudResult.getMessage());
			}
			if (CloudProvider.SANGFOR.name().equals(vendor.getType())){
				BocloudResult list = sangforFirewallRuleDriver.list(vendor.getUuid());
				if (list.isSuccess() && list.getData() != null){
					List<SangforFirewallRuleModel> ruleModelList = JSON.parseArray(JSON.toJSON(list.getData()).toString(), SangforFirewallRuleModel.class);
					List<FirewallRule> firewallRules = SangforConvertUtils.firewallConver(ruleModelList)
							.stream().filter(e-> rule.getRuleUuid().equals(e.getRuleUuid())).collect(Collectors.toList());
					if (!firewallRules.isEmpty()){
						FirewallRule firewallRule = firewallRules.get(0);
						com.bocloud.ims.service.utils.BeanUtils.copyPropertiesIgnoreNull(firewallRule,rule);
					} else {
						logger.error("未找到更新的网络策略!");
					}
				} else {
					logger.error("获取网络策略失败!");
				}
				firewallRuleRepository.update(rule);
				return new BocloudResult<>(true, bocloudResult.getMessage());
			}
			model = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()), FirewallRuleModel.class);
			BeanUtils.copyProperties(model, rule);
			rule.setMenderId(userId);
			firewallRuleRepository.update(rule);
			// 如果该规则已被策略绑定，则绑定策略审计状态需要更新为否
			if (null != rule.getPolicyId() && 0L != rule.getPolicyId()) {
				FirewallPolicy policy = firewallPolicyRepository.query(rule.getPolicyId());
				if (null != policy) {
					policy.setAudited(false);// 未审计状态
					firewallPolicyRepository.update(policy);
				}
			}
			return new BocloudResult<>(true, bocloudResult.getMessage());
		} catch (Exception e) {
			logger.error("update firewall rule fail:", e);
			return new BocloudResult<>(false, "修改防火墙规则失败");
		}
	}

	@Override
	public BocloudResult<FirewallRule> detail(Long id) {
		try {
			FirewallRule rule = firewallRuleRepository.query(id);
			if (!StringUtils.isEmpty(rule.getTenantUuid())) {
				OSTenant osTenant = osTenantRepository.getByTenantUuid(rule.getTenantUuid(), rule.getVendorId());
				if (null != osTenant) {
					rule.setOsTenantName(osTenant.getName());
				}
			}
			return new BocloudResult<>(true, rule, "查询成功");
		} catch (Exception e) {
			logger.error("query firewall rule fail:", e);
			return new BocloudResult<>(false, "查询防火墙规则失败");
		}
	}

	@Override
	public BocloudResult remove(Long id, RequestContext context) {
		String path = FirewallRule.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			FirewallRule rule = firewallRuleRepository.query(id);
			if (null == rule) {
				return new BocloudResult<>(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(rule.getVendorId());
			if (vendor.getVersion().equalsIgnoreCase("6.8") && CloudProvider.SANGFOR.name().equalsIgnoreCase(vendor.getType())){
				return sangforFireWallService.remove(context,id);
			}
			if (0L == context.getTenant() && 0L != rule.getTenantId()) {
				return new BocloudResult<>(false, "不能删除租户下的防火墙规则");
			}
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			BocloudResult result = null;
			if (CloudProvider.MANAGEONE.name().equals(vendor.getType())) {
				result = firewallDriver.removeRule(vendor.getUuid(), rule.getPolicyUuid(), rule.getRuleUuid());
			} else if(CloudProvider.SANGFOR.name().equals(vendor.getType())){
				result = sangforFirewallRuleDriver.remove(vendor.getUuid(),rule.getFirewallUuid());
			} else {
				if (null != rule.getPolicyId()) {
					return new BocloudResult<>(false, "该防火墙规则正在使用，不能删除");
				}
				result = firewallDriver.removeRule(rule.getRuleUuid(), vendor.getUuid());
			}
			if (result.isFailed()) {
				return new BocloudResult<>(false, result.getMessage());
			}
			firewallRuleRepository.remove(id, context.getTarget());
			return new BocloudResult<>(true, "删除成功");
		} catch (Exception e) {
			logger.error("remove firewall rule fail:", e);
			return new BocloudResult<>(false, "删除防火墙规则失败");
		}
	}

	@Override
	public BocloudResult<List<FirewallRule>> listRuleByNoBoundPolicy(Long vendorId, RequestContext context) {
		try {
			List<FirewallRule> list = firewallRuleRepository.listRuleByNoBoundPolicy(vendorId, context.getTenant());
			return new BocloudResult<>(true, list, "查询成功");
		} catch (Exception e) {
			logger.error("list firewall rule fail:", e);
			return new BocloudResult<>(false, "查询防火墙规则失败");
		}
	}

	@Override
	public BocloudResult<List<FirewallRule>> listRuleByPolicyId(Long policyId) {
		try {
			List<FirewallRule> list = firewallRuleRepository.listRuleByPolicyId(policyId);
			return new BocloudResult<>(true, list, "查询成功");
		} catch (Exception e) {
			logger.error("list firewall rule fail:", e);
			return new BocloudResult<>(false, "查询防火墙规则失败");
		}
	}

	@Override
	public BocloudResult batchRemove(List<Long> idArray, RequestContext context) {
		if (ListTool.isEmpty(idArray)) {
			return new BocloudResult<>(false, "没有需要删除的防火墙规则！");
		}
		try {
			List<BocloudResult> succList = Lists.newArrayList();
			List<BocloudResult> failList = Lists.newArrayList();
			Long vendorId = null;
			for (Long id : idArray) {
				FirewallRule rule = firewallRuleRepository.query(id);
				if (null != rule) {
					if (null == vendorId) {
						vendorId = rule.getVendorId();
					}
					BocloudResult result = remove(id, context);
					if (result.isSuccess()) {
						succList.add(new BocloudResult<>(true, "【" + rule.getName() + "】删除成功"));
					} else {
						failList.add(new BocloudResult<>(false, "【" + rule.getName() + "】删除失败：" + result.getMessage()));
					}
				}
			}
			CloudVendor vendor = cloudVendorRepository.query(vendorId);
			String operate = vendor.getType().toLowerCase() + ".rule";
			// 删除成功的数量
			int success = succList.size();
			// 删除失败的数量
			int fail = failList.size();
			String content = null;
			if (fail == idArray.size()) {
				content = "防火墙规则批量删除失败，详情如下：\n";
				for (BocloudResult bocloudResult : failList) {
					content += bocloudResult.getMessage() + "\n";
				}
				resourceEventPublisher
						.send(new OperateResult(false, content, operate, MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
			} else {
				content = "防火墙规则删除成功【" + success + "】个,失败【" + fail + "】个，详情如下：\n";
				for (BocloudResult bocloudResult : succList) {
					content += bocloudResult.getMessage() + "\n";
				}
				for (BocloudResult bocloudResult : failList) {
					content += bocloudResult.getMessage() + "\n";
				}
				resourceEventPublisher
						.send(new OperateResult(true, content, operate, MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
			}
		} catch (Exception e) {
			logger.error("batch remove firewall rule fail:", e);
			return new BocloudResult<>(false, "批量删除任务下发失败");
		}
		return new BocloudResult<>(true, "批量删除任务已下发，正在执行...");
	}

	@Override
	public BocloudResult getIpgps(Long vendorId, String query) {
		CloudVendor vendor = cloudVendorRepository.query(vendorId);
		Assert.notNull(vendor,"云平台不存在");

		try {
			InetAddress localHost = Inet4Address.getLocalHost();
			String hostAddress = localHost.getHostAddress();
			logger.info("获取到的本机ip地址为{}",hostAddress);
			if ("192.168.1.64".equals(hostAddress) || "10.8.2.138".equals(hostAddress)){
				return this.local("getIpgps");
			}
		} catch (UnknownHostException e) {
			logger.error("获取本机ip地址失败",e);
			return  new BocloudResult<>(false,"获取本机ip地址失败");
		}

		return sangforFirewallRuleDriver.getIpgps(vendor.getUuid(),query);
	}


	@Override
	public BocloudResult getServiceDefaults(Long vendorId, String querySrv) {
		CloudVendor vendor = cloudVendorRepository.query(vendorId);
		Assert.notNull(vendor,"云平台不存在");
		try {
			InetAddress localHost = Inet4Address.getLocalHost();
			String hostAddress = localHost.getHostAddress();
			logger.info("获取到的本机ip地址为{}",hostAddress);
			if ("192.168.1.64".equals(hostAddress) || "10.8.2.138".equals(hostAddress)){
				return this.local("getServiceDefaults");
			}
		} catch (UnknownHostException e) {
			logger.error("获取本机ip地址失败",e);
			return  new BocloudResult<>(false,"获取本机ip地址失败");
		}
		return sangforFirewallRuleDriver.getServiceDefaults(vendor.getUuid(),querySrv);
	}

	@Override
	public BocloudResult getServiceCustom(Long vendorId) {
		CloudVendor vendor = cloudVendorRepository.query(vendorId);
		Assert.notNull(vendor,"云平台不存在");
		try {
			InetAddress localHost = Inet4Address.getLocalHost();
			String hostAddress = localHost.getHostAddress();
			logger.info("获取到的本机ip地址为{}",hostAddress);
			if ("192.168.1.64".equals(hostAddress) || "10.8.2.138".equals(hostAddress)){
				return this.local("getServiceCustom");
			}
		} catch (UnknownHostException e) {
			logger.error("获取本机ip地址失败",e);
			return  new BocloudResult<>(false,"获取本机ip地址失败");
		}
		return sangforFirewallRuleDriver.getServiceCustom(vendor.getUuid());
	}


	private BocloudResult local(String getIpgps) {
		String str = "";
		JSONObject returnData = null;
		JSONArray data = null;
		String text = null;
		List<SangforIpgpModel> sangforIpgpModels = null;
		List<SangforServiceModel> serviceModels = null;
		switch (getIpgps){
			case "getIpgps":
				str = DataUtil.ipgps;
				returnData = JSONObject.parseObject(str);
				data = returnData.getJSONArray("data");
				sangforIpgpModels = JSONObject.parseArray(data.toJSONString(), SangforIpgpModel.class);
				return new BocloudResult<>(true,sangforIpgpModels,"成功");
			case "getServiceDefaults":
				str = DataUtil.serviceDefault;
				returnData = JSONObject.parseObject(str);
				data = returnData.getJSONArray("data");
				serviceModels = JSONObject.parseArray(data.toJSONString(), SangforServiceModel.class);
				return new BocloudResult<>(true,serviceModels,"成功");
			case "getServiceCustom":
				str = DataUtil.serviceCustom;
				returnData = JSONObject.parseObject(str);
				data = returnData.getJSONArray("data");
				serviceModels = JSONObject.parseArray(data.toJSONString(), SangforServiceModel.class);
				return new BocloudResult<>(true,serviceModels,"成功");
			default:
				return  new BocloudResult(true,"调用错误");
		}
	}
}
