From ac5bbbdafffd25bcabca883d8e2542e25fee57a5 Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Mon, 1 Apr 2019 17:10:26 +0800 Subject: Remove Alarm Info from DB when alarms are cleared Change-Id: If3350d64a6d2a104bc001b433ed98c410b9bb5a9 Issue-ID: HOLMES-204 Signed-off-by: GuangrongFu --- .../org/onap/holmes/engine/db/AlarmInfoDao.java | 34 +++++++++++-------- .../holmes/engine/dmaap/DMaaPAlarmPolling.java | 11 +++++-- .../onap/holmes/engine/manager/DroolsEngine.java | 38 +++++++++------------- .../holmes/engine/manager/DroolsEngineTest.java | 13 ++++---- 4 files changed, 51 insertions(+), 45 deletions(-) (limited to 'engine-d') diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java index c7094c3..bab3084 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java @@ -1,12 +1,12 @@ /** * Copyright 2017 ZTE Corporation. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -33,14 +33,16 @@ public abstract class AlarmInfoDao { @SqlQuery("SELECT * FROM ALARM_INFO") protected abstract List queryAlarm(); - @SqlUpdate("DELETE FROM ALARM_INFO WHERE ALARMISCLEARED=:alarmiscleared") - protected abstract int deleteAlarmByAlarmIsCleared(@Bind("alarmiscleared") int alarmIsCleared); + @SqlUpdate("DELETE FROM ALARM_INFO WHERE EVENTNAME=:eventName AND SOURCEID=:sourceId AND SOURCENAME=:sourceName") + protected abstract int deleteAlarmByAlarmIsCleared(@Bind("eventName") String eventName, + @Bind("sourceId") String sourceId, + @Bind("sourceName") String sourceName); public AlarmInfo saveAlarm(AlarmInfo alarmInfo) throws AlarmInfoException { try { addAlarm(alarmInfo); return alarmInfo; - } catch(Exception e) { + } catch (Exception e) { throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); } } @@ -48,15 +50,21 @@ public abstract class AlarmInfoDao { public List queryAllAlarm() throws AlarmInfoException { try { return queryAlarm(); - } catch(Exception e) { + } catch (Exception e) { throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); } } - public void deleteClearedAlarm(AlarmInfo alarmInfo) { - int alarmIsCleared = alarmInfo.getAlarmIsCleared(); - if(alarmIsCleared == 1) { - deleteAlarmByAlarmIsCleared(alarmIsCleared); - } + public void deleteAlarm(AlarmInfo alarmInfo) { + if (alarmInfo.getAlarmIsCleared() != 1) { + return; + } + + String sourceId = alarmInfo.getSourceId(); + String sourceName = alarmInfo.getSourceName(); + String eventName = alarmInfo.getEventName(); + eventName = eventName.substring(0, eventName.lastIndexOf("Cleared")); + + deleteAlarmByAlarmIsCleared(eventName, sourceId, sourceName); } } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java index 1446b14..99ba3d7 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/DMaaPAlarmPolling.java @@ -49,9 +49,15 @@ public class DMaaPAlarmPolling implements Runnable { vesAlarmList = subscriber.subscribe(); vesAlarmList.forEach(vesAlarm -> { try { - alarmInfoDao.saveAlarm(getAlarmInfo(vesAlarm)); + AlarmInfo alarmInfo = getAlarmInfo(vesAlarm); + if (alarmInfo.getAlarmIsCleared() != 1) { + alarmInfoDao.saveAlarm(alarmInfo); + } else { + alarmInfoDao.deleteAlarm(alarmInfo); + } droolsEngine.putRaisedIntoStream(vesAlarm); - } catch(AlarmInfoException e) { + + } catch (AlarmInfoException e) { log.error("Failed to save alarm to database", e); } }); @@ -74,6 +80,7 @@ public class DMaaPAlarmPolling implements Runnable { } } } + private AlarmInfo getAlarmInfo(VesAlarm vesAlarm) { AlarmInfo alarmInfo = new AlarmInfo(); alarmInfo.setAlarmIsCleared(vesAlarm.getAlarmIsCleared()); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java index b474e69..c397b28 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java @@ -15,17 +15,10 @@ */ package org.onap.holmes.engine.manager; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; -import javax.annotation.PostConstruct; -import javax.inject.Inject; - import lombok.extern.slf4j.Slf4j; import org.drools.compiler.kie.builder.impl.InternalKieModule; import org.drools.core.util.StringUtils; import org.jvnet.hk2.annotations.Service; - import org.kie.api.KieServices; import org.kie.api.builder.*; import org.kie.api.builder.Message.Level; @@ -33,20 +26,26 @@ import org.kie.api.io.Resource; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.rule.FactHandle; - import org.onap.holmes.common.api.entity.AlarmInfo; - +import org.onap.holmes.common.api.entity.CorrelationRule; import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.dmaap.DmaapService; import org.onap.holmes.common.exception.AlarmInfoException; +import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.common.utils.ExceptionUtil; import org.onap.holmes.engine.db.AlarmInfoDao; import org.onap.holmes.engine.request.DeployRuleRequest; -import org.onap.holmes.common.api.entity.CorrelationRule; -import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.ExceptionUtil; import org.onap.holmes.engine.wrapper.RuleMgtWrapper; +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + @Slf4j @Service public class DroolsEngine { @@ -99,8 +98,8 @@ public class DroolsEngine { } catch (Exception e) { log.error("Failed to initialize the engine service module.", e); } - if(null!=km) { - container = ks.newKieContainer(km.getReleaseId()); + if (null != km) { + container = ks.newKieContainer(km.getReleaseId()); } session = container.newKieSession(); deployed.clear(); @@ -123,7 +122,6 @@ public class DroolsEngine { } public void syncAlarms() throws AlarmInfoException { - alarmInfoDao.queryAllAlarm().forEach(alarmInfo -> alarmInfoDao.deleteClearedAlarm(alarmInfo)); alarmInfoDao.queryAllAlarm().forEach(alarmInfo -> putRaisedIntoStream(convertAlarmInfo2VesAlarm(alarmInfo))); } @@ -209,14 +207,10 @@ public class DroolsEngine { alarm.setRootFlag(((VesAlarm) obj).getRootFlag()); } this.session.delete(factHandle); - - if (alarm.getAlarmIsCleared() == 1) { - alarmInfoDao.deleteClearedAlarm(convertVesAlarm2AlarmInfo(alarm)); - } - } else { - this.session.insert(alarm); } + this.session.insert(alarm); + this.session.fireAllRules(); } @@ -227,8 +221,6 @@ public class DroolsEngine { .collect(Collectors.toList()); } - - private VesAlarm convertAlarmInfo2VesAlarm(AlarmInfo alarmInfo) { VesAlarm vesAlarm = new VesAlarm(); vesAlarm.setEventId(alarmInfo.getEventId()); diff --git a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java index 4ad9107..8da4910 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java @@ -17,7 +17,6 @@ package org.onap.holmes.engine.manager; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -32,12 +31,11 @@ import org.onap.holmes.engine.wrapper.RuleMgtWrapper; import org.powermock.api.easymock.PowerMock; import org.powermock.reflect.Whitebox; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; -import static org.easymock.EasyMock.anyObject; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; @@ -295,17 +293,18 @@ class AlarmInfoDaoStub extends AlarmInfoDao { } @Override - protected int deleteAlarmByAlarmIsCleared(int alarmIsCleared) { + protected int deleteAlarmByAlarmIsCleared(String alarmName, String sourceName, String sourceId) { return 1; } } class DbDaoUtilStub extends DbDaoUtil { private AlarmInfoDao dao = new AlarmInfoDaoStub(); + @Override public T getJdbiDaoByOnDemand(Class daoClazz) { - return (T)dao; + return (T) dao; } } -- cgit 1.2.3-korg