From c4bd70c1f74cd04c441f76de4359e166a089660a Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Sat, 18 Dec 2021 15:08:14 +0800 Subject: Switched from Dropwizard to Springboot Issue-ID: HOLMES-511 Signed-off-by: GuangrongFu Change-Id: I72b53ad4073e0a07a4c6a234781dc0d139584dda --- .../holmes/dsa/dmaappolling/DMaaPResponseUtil.java | 14 ++-- .../onap/holmes/dsa/dmaappolling/Subscriber.java | 8 +- .../org/onap/holmes/engine/EngineDActiveApp.java | 32 ++++---- .../org/onap/holmes/engine/EngineDAppConfig.java | 51 ------------ .../java/org/onap/holmes/engine/Initializer.java | 22 +++--- .../org/onap/holmes/engine/db/AlarmInfoDao.java | 70 ---------------- .../onap/holmes/engine/db/AlarmInfoDaoService.java | 62 +++++++++++++++ .../onap/holmes/engine/db/CorrelationRuleDao.java | 37 --------- .../engine/db/CorrelationRuleDaoService.java | 35 ++++++++ .../org/onap/holmes/engine/db/DaoProvider.java | 37 +++++++++ .../onap/holmes/engine/db/jdbi/AlarmInfoDao.java | 43 ++++++++++ .../holmes/engine/db/jdbi/CorrelationRuleDao.java | 32 ++++++++ .../holmes/engine/dcae/ConfigFileScanningTask.java | 8 +- .../engine/dcae/DcaeConfigurationPolling.java | 15 ++-- .../holmes/engine/dmaap/DMaaPAlarmPolling.java | 13 ++- .../onap/holmes/engine/dmaap/SubscriberAction.java | 33 ++++---- .../onap/holmes/engine/manager/DroolsEngine.java | 40 +++++----- .../manager/status/EntityStatusRefreshTask.java | 8 +- .../holmes/engine/request/CompileRuleRequest.java | 11 +-- .../holmes/engine/request/DeployRuleRequest.java | 14 ++-- .../holmes/engine/request/DmaapConfigRequest.java | 3 +- .../resources/DmaapConfigurationService.java | 52 +++++------- .../holmes/engine/resources/EngineResources.java | 92 ++++++++-------------- .../onap/holmes/engine/resources/HealthCheck.java | 23 ++---- .../holmes/engine/resources/SwaggerResource.java | 24 +++--- .../engine/response/CorrelationRuleResponse.java | 8 +- .../org/onap/holmes/engine/utils/AlarmUtil.java | 27 +++---- .../onap/holmes/engine/wrapper/RuleMgtWrapper.java | 30 +++---- 28 files changed, 421 insertions(+), 423 deletions(-) delete mode 100644 engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java delete mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDaoService.java delete mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDaoService.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/DaoProvider.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/AlarmInfoDao.java create mode 100644 engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/CorrelationRuleDao.java (limited to 'engine-d/src/main/java') diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java index 394dc08..4abf296 100644 --- a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java @@ -15,22 +15,18 @@ */ package org.onap.holmes.dsa.dmaappolling; +import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.api.stat.AlarmAdditionalField; -import com.google.gson.Gson; +import org.onap.holmes.common.api.stat.VesAlarm; +import org.springframework.stereotype.Component; -import java.util.Map; -import java.util.TimeZone; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; +import java.util.*; import static org.onap.holmes.common.utils.GsonUtil.*; -@Service +@Component public class DMaaPResponseUtil { public VesAlarm convertJsonToVesAlarm(String responseJson) { diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java index af772fb..4fd52ef 100644 --- a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,10 @@ import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.onap.holmes.common.api.stat.VesAlarm; -import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.JerseyClient; +import org.onap.holmes.common.utils.SpringContextUtil; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -34,8 +33,7 @@ import java.util.UUID; @Slf4j public class Subscriber { - private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator() - .getService(DMaaPResponseUtil.class); + private DMaaPResponseUtil dMaaPResponseUtil = SpringContextUtil.getBean(DMaaPResponseUtil.class); /** * The number of milliseconds to wait for messages if none are immediately available. This diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java index b1cc335..f5d6d8a 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2021 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,40 +15,36 @@ */ package org.onap.holmes.engine; -import io.dropwizard.setup.Environment; import lombok.extern.slf4j.Slf4j; import org.onap.holmes.common.ConfigFileScanner; -import org.onap.holmes.common.dropwizard.ioc.bundle.IOCApplication; -import org.onap.holmes.common.utils.transactionid.TransactionIdFilter; import org.onap.holmes.engine.dcae.ConfigFileScanningTask; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.context.annotation.ComponentScan; -import javax.servlet.DispatcherType; -import java.util.EnumSet; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @Slf4j -public class EngineDActiveApp extends IOCApplication { - - public static void main(String[] args) throws Exception { - new EngineDActiveApp().run(args); +@SpringBootApplication +@ServletComponentScan +@ComponentScan(basePackages = {"org.onap.holmes"}) +public class EngineDActiveApp implements ApplicationRunner { + public static void main(String[] args) { + SpringApplication.run(EngineDActiveApp.class, args); } @Override - public void run(EngineDAppConfig configuration, Environment environment) throws Exception { - super.run(configuration, environment); - - + public void run(ApplicationArguments args) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate( new ConfigFileScanningTask(new ConfigFileScanner()), 60L, ConfigFileScanningTask.POLLING_PERIOD, TimeUnit.SECONDS); - - environment.servlets().addFilter("logFilter", new TransactionIdFilter()).addMappingForUrlPatterns(EnumSet - .allOf(DispatcherType.class), true, "/*"); - Initializer.setReadyForMsbReg(true); } } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java deleted file mode 100644 index 836912f..0000000 --- a/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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 - * - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.holmes.engine; - -import com.fasterxml.jackson.annotation.JsonProperty; -import io.dropwizard.Configuration; -import io.dropwizard.db.DataSourceFactory; -import javax.validation.Valid; -import javax.validation.constraints.NotNull; -import org.jvnet.hk2.annotations.Service; - -@Service -public class EngineDAppConfig extends Configuration { - - private String apidescription = "Holmes rule management rest API"; - - @Valid - @NotNull - private DataSourceFactory database = new DataSourceFactory(); - - @JsonProperty("database") - public DataSourceFactory getDataSourceFactory() { - return database; - } - - @JsonProperty("database") - public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { - this.database = dataSourceFactory; - } - - public String getApidescription() { - return apidescription; - } - - public void setApidescription(String apidescription) { - this.apidescription = apidescription; - } -} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java index 4474fec..60869a1 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2018 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.onap.holmes.engine; -import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.CommonUtils; import org.onap.holmes.common.utils.MsbRegister; @@ -24,32 +23,33 @@ import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; import org.onap.msb.sdk.discovery.entity.Node; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; -import javax.inject.Inject; import java.util.HashSet; import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import static org.onap.holmes.common.config.MicroServiceConfig.POD_IP; import static org.onap.holmes.common.config.MicroServiceConfig.getMicroServiceIpAndPort; import static org.onap.holmes.common.utils.CommonUtils.getEnv; import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; -@Service -public class Initializer { +@Component +public class Initializer implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(Initializer.class); private volatile static boolean readyForMsbReg = false; private MsbRegister msbRegister; - @Inject + @Autowired public Initializer(MsbRegister msbRegister) { this.msbRegister = msbRegister; } - @PostConstruct - private void init() { + @Override + public void run(ApplicationArguments args) { Executors.newSingleThreadExecutor().execute(() -> { waitUntilReady(); try { @@ -93,7 +93,7 @@ public class Initializer { msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled()); Set nodes = new HashSet<>(); Node node = new Node(); - node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv(POD_IP)); + node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_ENGINE_MGMT_SERVICE_HOST")); node.setPort("9102"); /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http") 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 deleted file mode 100644 index 83fa3f6..0000000 --- a/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDao.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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 - *

- * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.holmes.engine.db; - -import org.onap.holmes.common.api.entity.AlarmInfo; -import org.onap.holmes.common.exception.AlarmInfoException; -import org.onap.holmes.common.utils.AlarmInfoMapper; -import org.skife.jdbi.v2.sqlobject.*; -import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; - -import java.util.List; - -@RegisterMapper(AlarmInfoMapper.class) -public abstract class AlarmInfoDao { - - @GetGeneratedKeys - @SqlUpdate("INSERT INTO ALARM_INFO (EVENTID,EVENTNAME,STARTEPOCHMICROSEC,SOURCEID,SOURCENAME,SEQUENCE,ALARMISCLEARED,ROOTFLAG,LASTEPOCHMICROSEC) VALUES (:eventId,:eventName,:startEpochMicroSec,:sourceId,:sourceName,:sequence,:alarmIsCleared,:rootFlag,:lastEpochMicroSec)") - protected abstract String addAlarm(@BindBean AlarmInfo alarmInfo); - - @SqlQuery("SELECT * FROM ALARM_INFO") - protected abstract List queryAlarm(); - - @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) { - throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); - } - } - - public List queryAllAlarm() throws AlarmInfoException { - try { - return queryAlarm(); - } catch (Exception e) { - throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); - } - } - - 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/db/AlarmInfoDaoService.java b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDaoService.java new file mode 100644 index 0000000..5c3444e --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/AlarmInfoDaoService.java @@ -0,0 +1,62 @@ +/** + * Copyright 2021-2022 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 + *

+ * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.engine.db; + +import org.onap.holmes.common.api.entity.AlarmInfo; +import org.onap.holmes.common.exception.AlarmInfoException; +import org.onap.holmes.engine.db.jdbi.AlarmInfoDao; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service + +public class AlarmInfoDaoService { + + @Autowired + private AlarmInfoDao alarmInfoDao; + + public AlarmInfo saveAlarm(AlarmInfo alarmInfo) throws AlarmInfoException { + try { + alarmInfoDao.addAlarm(alarmInfo); + return alarmInfo; + } catch (Exception e) { + throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); + } + } + + public List queryAllAlarm() throws AlarmInfoException { + try { + return alarmInfoDao.queryAlarm(); + } catch (Exception e) { + throw new AlarmInfoException("Can not access the database. Please contact the administrator for help.", e); + } + } + + 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")); + + alarmInfoDao.deleteAlarmByAlarmIsCleared(eventName, sourceId, sourceName); + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java deleted file mode 100644 index 8cd61ef..0000000 --- a/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * 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 - * - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.holmes.engine.db; - - -import java.util.List; -import org.onap.holmes.common.api.entity.CorrelationRule; -import org.onap.holmes.common.utils.CorrelationRuleMapper; -import org.skife.jdbi.v2.sqlobject.Bind; -import org.skife.jdbi.v2.sqlobject.SqlQuery; -import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper; - -@RegisterMapper(CorrelationRuleMapper.class) -public abstract class CorrelationRuleDao { - - - @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable") - public abstract List queryRuleByEnable(@Bind("enable") int enable); - - public List queryRuleByRuleEnable(int enable) { - return queryRuleByEnable(enable); - } -} - diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDaoService.java b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDaoService.java new file mode 100644 index 0000000..a80a255 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDaoService.java @@ -0,0 +1,35 @@ +/** + * Copyright 2021 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 + *

+ * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.engine.db; + +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.engine.db.jdbi.CorrelationRuleDao; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CorrelationRuleDaoService { + + @Autowired + private CorrelationRuleDao correlationRuleDao; + + public List queryRuleByRuleEnable(int enable) { + return correlationRuleDao.queryRuleByEnable(enable); + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/DaoProvider.java b/engine-d/src/main/java/org/onap/holmes/engine/db/DaoProvider.java new file mode 100644 index 0000000..3a52fd0 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/DaoProvider.java @@ -0,0 +1,37 @@ +/** + * Copyright 2022 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 + *

+ * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.engine.db; + +import org.jdbi.v3.core.Jdbi; +import org.onap.holmes.engine.db.jdbi.AlarmInfoDao; +import org.onap.holmes.engine.db.jdbi.CorrelationRuleDao; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class DaoProvider { + + @Bean + public AlarmInfoDao alarmInfoDao(Jdbi jdbi) { + return jdbi.onDemand(AlarmInfoDao.class); + } + + @Bean + public CorrelationRuleDao correlationRuleDao(Jdbi jdbi) { + return jdbi.onDemand(CorrelationRuleDao.class); + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/AlarmInfoDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/AlarmInfoDao.java new file mode 100644 index 0000000..e4b2f1a --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/AlarmInfoDao.java @@ -0,0 +1,43 @@ +/** + * Copyright 2017-2022 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 + *

+ * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.engine.db.jdbi; + +import org.jdbi.v3.sqlobject.config.RegisterRowMapper; +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; +import org.onap.holmes.common.api.entity.AlarmInfo; +import org.onap.holmes.common.utils.AlarmInfoMapper; + +import java.util.List; + +@RegisterRowMapper(AlarmInfoMapper.class) +public interface AlarmInfoDao { + + @GetGeneratedKeys + @SqlUpdate("INSERT INTO ALARM_INFO (EVENTID,EVENTNAME,STARTEPOCHMICROSEC,SOURCEID,SOURCENAME,SEQUENCE,ALARMISCLEARED,ROOTFLAG,LASTEPOCHMICROSEC) VALUES (:eventId,:eventName,:startEpochMicroSec,:sourceId,:sourceName,:sequence,:alarmIsCleared,:rootFlag,:lastEpochMicroSec)") + String addAlarm(@BindBean AlarmInfo alarmInfo); + + @SqlQuery("SELECT * FROM ALARM_INFO") + List queryAlarm(); + + @SqlUpdate("DELETE FROM ALARM_INFO WHERE EVENTNAME=:eventName AND SOURCEID=:sourceId AND SOURCENAME=:sourceName") + int deleteAlarmByAlarmIsCleared(@Bind("eventName") String eventName, + @Bind("sourceId") String sourceId, + @Bind("sourceName") String sourceName); +} diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/CorrelationRuleDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/CorrelationRuleDao.java new file mode 100644 index 0000000..bb0f128 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/engine/db/jdbi/CorrelationRuleDao.java @@ -0,0 +1,32 @@ +/** + * 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 + *

+ * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.engine.db.jdbi; + + +import org.jdbi.v3.sqlobject.config.RegisterRowMapper; +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.onap.holmes.common.api.entity.CorrelationRule; +import org.onap.holmes.common.utils.CorrelationRuleMapper; + +import java.util.List; + +@RegisterRowMapper(CorrelationRuleMapper.class) +public interface CorrelationRuleDao { + @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable") + public List queryRuleByEnable(@Bind("enable") int enable); +} + diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dcae/ConfigFileScanningTask.java b/engine-d/src/main/java/org/onap/holmes/engine/dcae/ConfigFileScanningTask.java index 15cb327..85e4a74 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dcae/ConfigFileScanningTask.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dcae/ConfigFileScanningTask.java @@ -20,14 +20,15 @@ import org.onap.holmes.common.ConfigFileScanner; import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser; -import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.Md5Util; +import org.onap.holmes.common.utils.SpringContextUtil; import org.onap.holmes.dsa.dmaappolling.Subscriber; import org.onap.holmes.engine.dmaap.SubscriberAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; import java.util.Map; public class ConfigFileScanningTask implements Runnable { @@ -35,7 +36,7 @@ public class ConfigFileScanningTask implements Runnable { final private static Logger LOGGER = LoggerFactory.getLogger(ConfigFileScanningTask.class); private String configFile = "/opt/hemtopics/cfy.json"; private ConfigFileScanner configFileScanner; - private String prevConfigMd5 = Md5Util.md5(null); + private String prevConfigMd5 = Md5Util.md5(new HashMap()); public ConfigFileScanningTask(ConfigFileScanner configFileScanner) { this.configFileScanner = configFileScanner; @@ -76,8 +77,7 @@ public class ConfigFileScanningTask implements Runnable { } private void addSubscribers(DcaeConfigurations dcaeConfigurations) { - SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator() - .getService(SubscriberAction.class); + SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class); for (String key : dcaeConfigurations.getSubKeys()) { Subscriber subscriber = new Subscriber(); subscriber.setTopic(key); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dcae/DcaeConfigurationPolling.java b/engine-d/src/main/java/org/onap/holmes/engine/dcae/DcaeConfigurationPolling.java index 15d77d6..6009b20 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dcae/DcaeConfigurationPolling.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dcae/DcaeConfigurationPolling.java @@ -1,12 +1,12 @@ /** * Copyright 2017 - 2021 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. @@ -19,9 +19,9 @@ import lombok.extern.slf4j.Slf4j; import org.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; -import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.Md5Util; +import org.onap.holmes.common.utils.SpringContextUtil; import org.onap.holmes.dsa.dmaappolling.Subscriber; import org.onap.holmes.engine.dmaap.SubscriberAction; @@ -45,7 +45,7 @@ public class DcaeConfigurationPolling implements Runnable { try { dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname); String md5 = Md5Util.md5(dcaeConfigurations); - if (prevConfigMd5.equals(md5)){ + if (prevConfigMd5.equals(md5)) { log.info("Operation aborted due to identical Configurations."); return; } @@ -62,8 +62,7 @@ public class DcaeConfigurationPolling implements Runnable { } private void addSubscribers(DcaeConfigurations dcaeConfigurations) { - SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator() - .getService(SubscriberAction.class); + SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class); for (String key : dcaeConfigurations.getSubKeys()) { Subscriber subscriber = new Subscriber(); subscriber.setTopic(key); 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 e2ad89c..5e8da83 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 @@ -21,10 +21,9 @@ import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.exception.AlarmInfoException; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.dsa.dmaappolling.Subscriber; -import org.onap.holmes.engine.db.AlarmInfoDao; +import org.onap.holmes.engine.db.AlarmInfoDaoService; import org.onap.holmes.engine.manager.DroolsEngine; -import java.util.ArrayList; import java.util.List; @Slf4j @@ -33,13 +32,13 @@ public class DMaaPAlarmPolling implements Runnable { private Subscriber subscriber; private DroolsEngine droolsEngine; private volatile boolean isAlive = true; - private AlarmInfoDao alarmInfoDao; + private AlarmInfoDaoService alarmInfoDaoService; - public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine, AlarmInfoDao alarmInfoDao) { + public DMaaPAlarmPolling(Subscriber subscriber, DroolsEngine droolsEngine, AlarmInfoDaoService alarmInfoDaoService) { this.subscriber = subscriber; this.droolsEngine = droolsEngine; - this.alarmInfoDao = alarmInfoDao; + this.alarmInfoDaoService = alarmInfoDaoService; } public void run() { @@ -51,9 +50,9 @@ public class DMaaPAlarmPolling implements Runnable { try { AlarmInfo alarmInfo = getAlarmInfo(vesAlarm); if (alarmInfo.getAlarmIsCleared() != 1) { - alarmInfoDao.saveAlarm(alarmInfo); + alarmInfoDaoService.saveAlarm(alarmInfo); } else { - alarmInfoDao.deleteAlarm(alarmInfo); + alarmInfoDaoService.deleteAlarm(alarmInfo); } droolsEngine.putRaisedIntoStream(vesAlarm); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java index 1297f11..21f5961 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2021 ZTE Corporation. + * Copyright 2017 - 2022 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,26 +15,31 @@ */ package org.onap.holmes.engine.dmaap; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import javax.annotation.PreDestroy; -import javax.inject.Inject; +import jakarta.annotation.PreDestroy; import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.common.database.DbDaoUtil; import org.onap.holmes.dsa.dmaappolling.Subscriber; -import org.onap.holmes.engine.db.AlarmInfoDao; +import org.onap.holmes.engine.db.AlarmInfoDaoService; +import org.onap.holmes.engine.db.jdbi.AlarmInfoDao; import org.onap.holmes.engine.manager.DroolsEngine; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; -@Service +@Component @Slf4j public class SubscriberAction { - @Inject + @Autowired private DroolsEngine droolsEngine; - @Inject + @Autowired private DbDaoUtil daoUtil; + @Autowired + private AlarmInfoDaoService alarmInfoDaoService; + private HashMap pollingTasks = new HashMap<>(); public synchronized void addSubscriber(Subscriber subscriber) { @@ -44,7 +49,7 @@ public class SubscriberAction { removeSubscriber(subscriber); } AlarmInfoDao alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class); - DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDao); + DMaaPAlarmPolling pollingTask = new DMaaPAlarmPolling(subscriber, droolsEngine, alarmInfoDaoService); Thread thread = new Thread(pollingTask); thread.start(); pollingTasks.put(topic, pollingTask); @@ -67,7 +72,7 @@ public class SubscriberAction { public void stopPollingTasks() { Iterator iterator = pollingTasks.entrySet().iterator(); while (iterator.hasNext()) { - Map.Entry entry = (Map.Entry)iterator.next(); + Map.Entry entry = (Map.Entry) iterator.next(); String key = (String) entry.getKey(); pollingTasks.get(key).stopTask(); } 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 cbda858..5970b59 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 @@ -18,7 +18,6 @@ package org.onap.holmes.engine.manager; 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; @@ -37,14 +36,15 @@ import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache; 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.db.AlarmInfoDaoService; import org.onap.holmes.engine.request.DeployRuleRequest; import org.onap.holmes.engine.wrapper.RuleMgtWrapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; -import javax.inject.Inject; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -52,15 +52,13 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @Slf4j -@Service -public class DroolsEngine { - +@Component +public class DroolsEngine implements ApplicationRunner { private final static int ENABLE = 1; private final Map deployed = new ConcurrentHashMap<>(); private RuleMgtWrapper ruleMgtWrapper; - private DbDaoUtil daoUtil; private ClosedLoopControlNameCache closedLoopControlNameCache; - private AlarmInfoDao alarmInfoDao; + private AlarmInfoDaoService alarmInfoDaoService; private KieServices ks = KieServices.Factory.get(); private ReleaseId releaseId = ks.newReleaseId("org.onap.holmes", "rules", "1.0.0-SNAPSHOT"); private ReleaseId compilationRelease = ks.newReleaseId("org.onap.holmes", "compilation", "1.0.0-SNAPSHOT"); @@ -68,24 +66,23 @@ public class DroolsEngine { private KieSession session; private String instanceIp; - @Inject - public void setRuleMgtWrapper(RuleMgtWrapper ruleMgtWrapper) { - this.ruleMgtWrapper = ruleMgtWrapper; + @Autowired + public void setAlarmInfoDaoService(AlarmInfoDaoService alarmInfoDaoService) { + this.alarmInfoDaoService = alarmInfoDaoService; } - @Inject - public void setDaoUtil(DbDaoUtil daoUtil) { - this.daoUtil = daoUtil; + @Autowired + public void setRuleMgtWrapper(RuleMgtWrapper ruleMgtWrapper) { + this.ruleMgtWrapper = ruleMgtWrapper; } - @Inject + @Autowired public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) { this.closedLoopControlNameCache = closedLoopControlNameCache; } - @PostConstruct - private void init() { - alarmInfoDao = daoUtil.getJdbiDaoByOnDemand(AlarmInfoDao.class); + @Override + public void run(ApplicationArguments args) { instanceIp = MicroServiceConfig.getMicroServiceIpAndPort()[0]; try { log.info("Drools engine initializing..."); @@ -146,7 +143,7 @@ public class DroolsEngine { } public void syncAlarms() throws AlarmInfoException { - alarmInfoDao.queryAllAlarm().forEach(alarmInfo -> putRaisedIntoStream(convertAlarmInfo2VesAlarm(alarmInfo))); + alarmInfoDaoService.queryAllAlarm().forEach(alarmInfo -> putRaisedIntoStream(convertAlarmInfo2VesAlarm(alarmInfo))); } public String deployRule(DeployRuleRequest rule) throws CorrelationException { @@ -330,5 +327,4 @@ public class DroolsEngine { Resource jarRes = ks.getResources().newByteArrayResource(jar); return ks.getRepository().addKieModule(jarRes); } - } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/manager/status/EntityStatusRefreshTask.java b/engine-d/src/main/java/org/onap/holmes/engine/manager/status/EntityStatusRefreshTask.java index 0b15999..0e65a28 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/manager/status/EntityStatusRefreshTask.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/status/EntityStatusRefreshTask.java @@ -16,22 +16,22 @@ package org.onap.holmes.engine.manager.status; -import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.engine.entity.EngineEntity; import org.onap.holmes.common.engine.service.EngineEntityService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import javax.inject.Inject; import java.util.Optional; import java.util.Timer; import java.util.TimerTask; import static java.util.concurrent.TimeUnit.SECONDS; -@Service +@Component public class EntityStatusRefreshTask extends TimerTask { private static final Logger logger = LoggerFactory.getLogger(EntityStatusRefreshTask.class); private final long INTERVAL = SECONDS.toMillis(15); @@ -39,7 +39,7 @@ public class EntityStatusRefreshTask extends TimerTask { private Timer timer = new Timer("EntityStatusRefreshTimer", true); private EngineEntityService engineEntityService; - @Inject + @Autowired public EntityStatusRefreshTask(EngineEntityService engineEntityService) { this.engineEntityService = engineEntityService; } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java index b38ee5f..1baa8ad 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.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. @@ -15,10 +15,11 @@ */ package org.onap.holmes.engine.request; -import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; +import javax.validation.constraints.NotNull; + @Getter @Setter public class CompileRuleRequest { diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java index f2ef369..3e1b6ca 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java @@ -1,12 +1,12 @@ /** - * Copyright 2017 ZTE Corporation. - * + * Copyright 2017-2021 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. @@ -15,10 +15,11 @@ */ package org.onap.holmes.engine.request; -import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; +import javax.validation.constraints.NotNull; + @Getter @Setter public class DeployRuleRequest { @@ -26,6 +27,7 @@ public class DeployRuleRequest { @NotNull private String content; + @NotNull private String engineId; @NotNull diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/DmaapConfigRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/DmaapConfigRequest.java index 6fa1928..ede1d71 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/request/DmaapConfigRequest.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/DmaapConfigRequest.java @@ -16,10 +16,11 @@ package org.onap.holmes.engine.request; -import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; +import javax.validation.constraints.NotNull; + @Getter @Setter public class DmaapConfigRequest { diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/DmaapConfigurationService.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/DmaapConfigurationService.java index 548b9b2..576170f 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/DmaapConfigurationService.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/DmaapConfigurationService.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,45 +18,35 @@ package org.onap.holmes.engine.resources; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.DELETE; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.MediaType; import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dcae.entity.SecurityInfo; -import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; +import org.onap.holmes.common.utils.SpringContextUtil; import org.onap.holmes.dsa.dmaappolling.Subscriber; import org.onap.holmes.engine.dmaap.SubscriberAction; import org.onap.holmes.engine.request.DmaapConfigRequest; +import org.springframework.web.bind.annotation.*; -@Service @Slf4j -//@Api(tags = {"DMaaP Configurations"}) -@Path("/dmaap") +@RestController +@RequestMapping("/dmaap") public class DmaapConfigurationService { - @PUT - @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Subscribe to a new topic. " + "If the topic already exists, it is replaced with the new configuration.") - @Path("/sub") + @RequestMapping(value = "/sub", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON) public String addSubInfo( - @ApiParam (value = "A JSON object with the fields named name" - + " and url. Both fields are required.") DmaapConfigRequest config, - @Context HttpServletRequest request){ + @ApiParam(value = "A JSON object with the fields named name" + + " and url. Both fields are required.") + @RequestBody DmaapConfigRequest config) { String url = config.getUrl(); if (url.startsWith("http://") || url.startsWith("https://")) { Subscriber subscriber = new Subscriber(); subscriber.setTopic(config.getName()); subscriber.setUrl(url); - SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator() - .getService(SubscriberAction.class); + SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class); subscriberAction.removeSubscriber(subscriber); subscriberAction.addSubscriber(subscriber); @@ -67,30 +57,26 @@ public class DmaapConfigurationService { return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}"; } - @DELETE @Path("/sub/{topic}") @ApiOperation(value = "Unsubscribe a topic from DMaaP.") - @Produces(MediaType.APPLICATION_JSON) - public String removeSubInfo(@PathParam("topic") String topic){ + @RequestMapping(value = "/sub/{topic}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON) + public String removeSubInfo(@PathVariable("topic") String topic) { Subscriber subscriber = new Subscriber(); subscriber.setTopic(topic); - SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator() - .getService(SubscriberAction.class); + SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class); subscriberAction.removeSubscriber(subscriber); return "{\"message\": \"Topic unsubscribed.\"}"; } - @PUT - @Produces(MediaType.APPLICATION_JSON) - @Path("/pub") @ApiOperation(value = "Add/Update a publishing topic. " + "If the topic already exists, it is replaced with the new configuration.") + @RequestMapping(value = "/pub", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON) public String updatePubInfo( - @ApiParam (value = "A JSON object with the fields named name" - + " and url. Both fields are required.") DmaapConfigRequest config, - @Context HttpServletRequest request){ + @ApiParam(value = "A JSON object with the fields named name" + + " and url. Both fields are required.") + @RequestBody DmaapConfigRequest config) { String url = config.getUrl(); if (url.startsWith("http://") || url.startsWith("https://")) { SecurityInfo securityInfo = new SecurityInfo(); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java index 0aa3a59..cb41533 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.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 - * + *

* 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. @@ -16,64 +16,59 @@ package org.onap.holmes.engine.resources; -import com.codahale.metrics.annotation.Timed; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache; import org.onap.holmes.common.exception.CorrelationException; import org.onap.holmes.common.utils.ExceptionUtil; -import org.onap.holmes.common.utils.LanguageUtil; import org.onap.holmes.engine.manager.DroolsEngine; import org.onap.holmes.engine.request.CompileRuleRequest; import org.onap.holmes.engine.request.DeployRuleRequest; import org.onap.holmes.engine.response.CorrelationRuleResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import java.util.Locale; +import jakarta.ws.rs.core.MediaType; import java.util.regex.Matcher; import java.util.regex.Pattern; -@Service -@Path("/rule") -@Api(tags = {"Holmes Engine Management"}) -@Produces(MediaType.APPLICATION_JSON) @Slf4j +@RestController +@RequestMapping("/rule") +@Api(tags = {"Holmes Engine Management"}) public class EngineResources { - @Inject - DroolsEngine droolsEngine; - private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*"); + private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*"); private ClosedLoopControlNameCache closedLoopControlNameCache; + private DroolsEngine droolsEngine; + + @Autowired + public void setDroolsEngine(DroolsEngine droolsEngine) { + this.droolsEngine = droolsEngine; + } - @Inject + @Autowired public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) { this.closedLoopControlNameCache = closedLoopControlNameCache; } - @PUT - @Produces(MediaType.APPLICATION_JSON) - @Timed + @ResponseBody + @PutMapping(produces = MediaType.APPLICATION_JSON) public CorrelationRuleResponse deployRule( @ApiParam(value = "The request entity of the HTTP call, which comprises three " + "fields: \"content\" , \"loopControlName\" and \"engineId\". " + "The \"content\" should be a valid Drools rule string and the \"engineId\" " - + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest, - @Context HttpServletRequest httpRequest) { + + "has to be \"engine-d\" in the Amsterdam release.", required = true) + @RequestBody DeployRuleRequest deployRuleRequest) { CorrelationRuleResponse crResponse = new CorrelationRuleResponse(); - Locale locale = LanguageUtil.getLocale(httpRequest); try { String packageName = getPackageName(deployRuleRequest.getContent()); - if(packageName == null) { - throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent()); + if (packageName == null) { + throw new CorrelationException("Could not find package name in rule: " + deployRuleRequest.getContent()); } - + closedLoopControlNameCache .put(packageName, deployRuleRequest.getLoopControlName()); String packageNameRet = droolsEngine.deployRule(deployRuleRequest); @@ -97,15 +92,8 @@ public class EngineResources { return crResponse; } - @DELETE - @Produces(MediaType.APPLICATION_JSON) - @Timed - @Path("/{packageName}") - public boolean undeployRule(@PathParam("packageName") String packageName, - @Context HttpServletRequest httpRequest) { - - Locale locale = LanguageUtil.getLocale(httpRequest); - + @DeleteMapping(value = "/{packageName}") + public void undeployRule(@PathVariable("packageName") String packageName) { try { droolsEngine.undeployRule(packageName); closedLoopControlNameCache.remove(packageName); @@ -113,36 +101,26 @@ public class EngineResources { log.error(correlationException.getMessage(), correlationException); throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); } - - return true; } - - @POST + @PostMapping @ApiOperation(value = "Check the validity of a rule.") - @Produces(MediaType.APPLICATION_JSON) - @Timed - public boolean compileRule(CompileRuleRequest compileRuleRequest, - @Context HttpServletRequest httpRequest) { - - Locale locale = LanguageUtil.getLocale(httpRequest); - + public void compileRule(@RequestBody CompileRuleRequest compileRuleRequest) { try { droolsEngine.compileRule(compileRuleRequest.getContent()); } catch (CorrelationException correlationException) { log.error(correlationException.getMessage(), correlationException); throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage()); } - return true; } - - private String getPackageName(String contents){ + + private String getPackageName(String contents) { Matcher m = packagePattern.matcher(contents); - - if (m.find( )) { - return m.group(1); - }else { - return null; + + if (m.find()) { + return m.group(1); + } else { + return null; } } } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/HealthCheck.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/HealthCheck.java index c86bf06..44d238a 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/HealthCheck.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/HealthCheck.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,24 +19,17 @@ package org.onap.holmes.engine.resources; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.SwaggerDefinition; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -@Service +@RestController @SwaggerDefinition -@Path("/healthcheck") +@RequestMapping("/healthcheck") @Api(tags = {"Health Check"}) -@Produces(MediaType.TEXT_PLAIN) -@Slf4j public class HealthCheck { - @GET - @Produces(MediaType.TEXT_PLAIN) + @GetMapping @ApiOperation(value = "Interface for the health check of the engine management module for Holmes") - public boolean healthCheck(){ - return true; + public void healthCheck() { } } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java index 6743b8e..494df77 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2022 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,27 +16,25 @@ package org.onap.holmes.engine.resources; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.ws.rs.core.MediaType; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.net.URLDecoder; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -@Service -@Path("/swagger.json") -@Produces(MediaType.APPLICATION_JSON) @Slf4j +@RestController +@RequestMapping("/swagger.json") public class SwaggerResource { - @GET - @Produces(MediaType.APPLICATION_JSON) + @GetMapping(produces = MediaType.APPLICATION_JSON) public String getSwaggerJson() { URL url = SwaggerResource.class.getResource("/swagger.json"); String ret = "{}"; @@ -54,7 +52,7 @@ public class SwaggerResource { return ret; } - try(BufferedReader br = new BufferedReader(new FileReader(file))) { + try (BufferedReader br = new BufferedReader(new FileReader(file))) { StringBuffer buffer = new StringBuffer(); String line = " "; while ((line = br.readLine()) != null) { diff --git a/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java index 306bd7f..96913a2 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.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. diff --git a/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java index c7eb02b..19d6f38 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.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. @@ -15,12 +15,11 @@ */ package org.onap.holmes.engine.utils; +import org.onap.holmes.common.api.stat.Alarm; + import java.util.HashMap; import java.util.Map; -import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.common.api.stat.Alarm; -@Service public class AlarmUtil { private final static AlarmUtil alarmUtil = new AlarmUtil(); @@ -28,7 +27,7 @@ public class AlarmUtil { * Map> */ private final Map> rootPriorityMap = - new HashMap>(); + new HashMap>(); /** * Map */ @@ -49,7 +48,7 @@ public class AlarmUtil { String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(","); for (int i = 0; i < probableCauseStrs.length; i++) { if (alarm.getProbableCause() == Long.parseLong(probableCauseStrs[i]) - && alarm.getEquipType().equals(equipTypes[i])) { + && alarm.getEquipType().equals(equipTypes[i])) { return true; } } @@ -57,10 +56,10 @@ public class AlarmUtil { } public Integer getPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, - String equipTypeStr, Alarm alarm) { + String equipTypeStr, Alarm alarm) { if (rootPriorityMap.containsKey(ruleId)) { if (!saveRuleMsg.get(ruleId) - .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) { + .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) { setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr); } } else { @@ -68,7 +67,7 @@ public class AlarmUtil { } Integer priority = - rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType()); + rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType()); if (priority == null) { priority = 0; } @@ -76,7 +75,7 @@ public class AlarmUtil { } private void setPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr, - String equipTypeStr) { + String equipTypeStr) { saveRuleMsg.put(ruleId, probableCauseStr + equipTypeStr + rootAlarmFeatureStr); Map map = new HashMap(); @@ -85,7 +84,7 @@ public class AlarmUtil { String[] equipTypes = equipTypeStr.replace(" ", "").split(","); for (int i = 0; i < rootAlarmFeatureStrs.length; i++) { map.put(probableCauseStrs[i] + "-" + equipTypes[i], - Integer.parseInt(rootAlarmFeatureStrs[i])); + Integer.parseInt(rootAlarmFeatureStrs[i])); } rootPriorityMap.put(ruleId, map); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java index 1fbbfe7..08ec63e 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.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. @@ -15,29 +15,29 @@ */ package org.onap.holmes.engine.wrapper; -import java.util.List; -import javax.inject.Inject; -import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; -import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.engine.db.CorrelationRuleDao; import org.onap.holmes.common.api.entity.CorrelationRule; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.DbDaoUtil; +import org.onap.holmes.engine.db.CorrelationRuleDaoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; @Service -@Singleton @Slf4j public class RuleMgtWrapper { + private CorrelationRuleDaoService correlationRuleDaoService; - @Inject - private DbDaoUtil daoUtil; + @Autowired + public RuleMgtWrapper(CorrelationRuleDaoService correlationRuleDaoService) { + this.correlationRuleDaoService = correlationRuleDaoService; + } public List queryRuleByEnable(int enable) throws CorrelationException { - List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) - .queryRuleByRuleEnable(enable); + List ruleTemp = correlationRuleDaoService.queryRuleByRuleEnable(enable); return ruleTemp; } } -- cgit 1.2.3-korg