From da1b7d9f04453e94fb77ebe4213f456a0be63fc2 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 5 Aug 2021 14:53:19 -0400 Subject: Use lombok in drools-pdp #2 Updated feature-session-persistence thru drools-domains. Issue-ID: POLICY-3397 Change-Id: I0b1c6da8b2301c00dd792675e8cf1f49888edb9f Signed-off-by: Jim Hahn --- .../onap/policy/drools/core/DroolsExecutor.java | 13 ++----- .../onap/policy/drools/core/PolicyContainer.java | 34 +++++++----------- .../org/onap/policy/drools/core/PolicySession.java | 42 +++++----------------- .../core/PolicySessionFeatureApiConstants.java | 9 +++-- .../org/onap/policy/drools/core/jmx/PdpJmx.java | 13 ++++--- .../policy/drools/core/jmx/PdpJmxListener.java | 13 ++++--- .../policy/drools/core/lock/AlwaysFailLock.java | 11 ++---- .../drools/properties/DroolsPropertyConstants.java | 12 +++---- .../policy/drools/util/FeatureEnabledChecker.java | 18 ++++------ .../java/org/onap/policy/drools/util/KieUtils.java | 32 ++++++++--------- 10 files changed, 72 insertions(+), 125 deletions(-) (limited to 'policy-core/src/main/java/org/onap/policy') diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/DroolsExecutor.java b/policy-core/src/main/java/org/onap/policy/drools/core/DroolsExecutor.java index 538970d6..d996d5a3 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/DroolsExecutor.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/DroolsExecutor.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,24 +21,17 @@ package org.onap.policy.drools.core; import java.util.concurrent.Executor; +import lombok.AllArgsConstructor; import org.drools.core.WorkingMemory; /** * An executor that executes its tasks by inserting {@link DroolsRunnable} objects into * the working memory of a running session. */ +@AllArgsConstructor public class DroolsExecutor implements Executor { private final WorkingMemory workingMemory; - /** - * Constructs the object. - * - * @param workingMemory where tasks should be injected - */ - public DroolsExecutor(WorkingMemory workingMemory) { - this.workingMemory = workingMemory; - } - @Override public void execute(Runnable command) { DroolsRunnable runnable = command::run; diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/PolicyContainer.java b/policy-core/src/main/java/org/onap/policy/drools/core/PolicyContainer.java index 5bce3505..1eab676f 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/PolicyContainer.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/PolicyContainer.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-core * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.concurrent.ConcurrentHashMap; -import org.kie.api.KieBase; +import lombok.Getter; import org.kie.api.KieServices; import org.kie.api.builder.KieScanner; import org.kie.api.builder.Message; @@ -57,6 +57,7 @@ public class PolicyContainer implements Startable { private ConcurrentHashMap adjuncts = new ConcurrentHashMap<>(); // 'KieContainer' associated with this 'PolicyContainer' + @Getter private KieContainer kieContainer; // indicates whether the PolicyContainer is 'started' @@ -106,7 +107,7 @@ public class PolicyContainer implements Startable { * @param releaseId indicates the artifact that is to be installed in this container */ public PolicyContainer(ReleaseId releaseId) { - ReleaseId newReleaseId = releaseId; + var newReleaseId = releaseId; if (newReleaseId.getVersion().contains(",")) { // this is actually a comma-separated list of release ids newReleaseId = @@ -192,15 +193,6 @@ public class PolicyContainer implements Startable { return kieContainer.getReleaseId().toString(); } - /** - * Get kie container. - * - * @return the associated 'KieContainer' instance - */ - public KieContainer getKieContainer() { - return kieContainer; - } - /** * Get class loader. * @@ -332,8 +324,8 @@ public class PolicyContainer implements Startable { logger.info("adoptKieSession:name: {} kieSession: {}", name, kieSession); } // fetch KieBase, and verify it belongs to this KieContainer - boolean match = false; - KieBase kieBase = kieSession.getKieBase(); + var match = false; + var kieBase = kieSession.getKieBase(); logger.info("adoptKieSession:kieBase: {}", kieBase); for (String kieBaseName : kieContainer.getKieBaseNames()) { logger.info("adoptKieSession:kieBaseName: {}", kieBaseName); @@ -358,7 +350,7 @@ public class PolicyContainer implements Startable { // create the new 'PolicySession', add it to the table, // and return the object to the caller logger.info("adoptKieSession:create a new policySession with name {}", name); - PolicySession policySession = new PolicySession(name, this, kieSession); + var policySession = new PolicySession(name, this, kieSession); sessions.put(name, policySession); // notify features @@ -383,8 +375,8 @@ public class PolicyContainer implements Startable { * how to determine success/failure) */ public String updateToVersion(String newVersion) { - ReleaseId releaseId = kieContainer.getReleaseId(); - Results results = this.updateToVersion( + var releaseId = kieContainer.getReleaseId(); + var results = this.updateToVersion( kieServices.newReleaseId(releaseId.getGroupId(), releaseId.getArtifactId(), newVersion)); List messages = results == null ? null : results.getMessages(); @@ -411,7 +403,7 @@ public class PolicyContainer implements Startable { } // update the version - Results results = kieContainer.updateToVersion(releaseId); + var results = kieContainer.updateToVersion(releaseId); // add common KiePackage instances @@ -519,7 +511,7 @@ public class PolicyContainer implements Startable { * @return 'true' if the fact was inserted into at least one session, 'false' if not */ public boolean insertAll(Object object) { - boolean rval = false; + var rval = false; synchronized (sessions) { for (PolicySession session : sessions.values()) { session.insertDrools(object); @@ -553,7 +545,7 @@ public class PolicyContainer implements Startable { for (String kieSessionName : kieContainer.getKieSessionNamesInKieBase(kieBaseName)) { // if the 'PolicySession' does not currently exist, this method // call will attempt to create it - PolicySession session = activatePolicySession(kieSessionName, kieBaseName); + var session = activatePolicySession(kieSessionName, kieBaseName); if (session != null) { session.startThread(); } @@ -708,7 +700,7 @@ public class PolicyContainer implements Startable { * @param args standard 'main' arguments, which are currently ignored */ public static void globalInit(String[] args) { - String configDir = "config"; + var configDir = "config"; logger.info("PolicyContainer.main: configDir={}", configDir); // invoke 'globalInit' on all of the features diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/PolicySession.java b/policy-core/src/main/java/org/onap/policy/drools/core/PolicySession.java index fb17b10a..1f8a61c6 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/PolicySession.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/PolicySession.java @@ -22,6 +22,7 @@ package org.onap.policy.drools.core; import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; import org.kie.api.event.rule.AfterMatchFiredEvent; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.AgendaGroupPoppedEvent; @@ -52,11 +53,18 @@ public class PolicySession implements AgendaEventListener, RuleRuntimeEventListener { // get an instance of logger private static Logger logger = LoggerFactory.getLogger(PolicySession.class); + + // supports 'getCurrentSession()' method + private static ThreadLocal policySess = + new ThreadLocal<>(); + // name of the 'PolicySession' and associated 'KieSession' + @Getter private String name; // the associated 'PolicyContainer', which may have additional // 'PolicySession' instances in addition to this one + @Getter private PolicyContainer container; // maps feature objects to per-PolicyContainer data @@ -64,15 +72,12 @@ public class PolicySession new ConcurrentHashMap<>(); // associated 'KieSession' instance + @Getter private KieSession kieSession; // if not 'null', this is the thread model processing the 'KieSession' private ThreadModel threadModel = null; - // supports 'getCurrentSession()' method - private static ThreadLocal policySess = - new ThreadLocal<>(); - /** * Internal constructor - create a 'PolicySession' instance. * @@ -89,35 +94,6 @@ public class PolicySession kieSession.addEventListener((RuleRuntimeEventListener) this); } - /** - * Get policy container. - * - * @return the 'PolicyContainer' object containing this session - */ - public PolicyContainer getPolicyContainer() { - return container; - } - - /** - * Get Kie Session. - * - * @return the associated 'KieSession' instance - */ - public KieSession getKieSession() { - return kieSession; - } - - /** - * Get name. - * - * @return the local name of this session, which should either match the - * name specified in 'kmodule.xml' file associated with this session, or the - * name passed on the 'PolicyContainer.adoptKieSession' method. - */ - public String getName() { - return name; - } - /** * Get full name. * diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/PolicySessionFeatureApiConstants.java b/policy-core/src/main/java/org/onap/policy/drools/core/PolicySessionFeatureApiConstants.java index 174ac794..98808848 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/PolicySessionFeatureApiConstants.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/PolicySessionFeatureApiConstants.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-core * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,12 @@ package org.onap.policy.drools.core; +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; import org.onap.policy.common.utils.services.OrderedServiceImpl; +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class PolicySessionFeatureApiConstants { /** * 'FeatureAPI.impl.getList()' returns an ordered list of objects @@ -31,8 +34,4 @@ public class PolicySessionFeatureApiConstants { @Getter private static final OrderedServiceImpl impl = new OrderedServiceImpl<>(PolicySessionFeatureApi.class); - - private PolicySessionFeatureApiConstants() { - // do nothing - } } diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmx.java b/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmx.java index e1a89752..0f4add77 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmx.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmx.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * policy-core * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -21,17 +21,16 @@ package org.onap.policy.drools.core.jmx; import java.util.concurrent.atomic.AtomicLong; +import lombok.Getter; public class PdpJmx implements PdpJmxMBean { + @Getter private static PdpJmx instance = new PdpJmx(); + private final AtomicLong updates = new AtomicLong(); private final AtomicLong actions = new AtomicLong(); - public static PdpJmx getInstance() { - return instance; - } - @Override public long getUpdates() { return updates.longValue(); diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmxListener.java b/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmxListener.java index 924f2b00..4a39173c 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmxListener.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/jmx/PdpJmxListener.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-core * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,25 +24,24 @@ import java.lang.management.ManagementFactory; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; -import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class PdpJmxListener { public static final Logger logger = LoggerFactory.getLogger(PdpJmxListener.class); - private PdpJmxListener() { - } - /** * Stop the listener. */ public static void stop() { - final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + final var server = ManagementFactory.getPlatformMBeanServer(); try { server.unregisterMBean(new ObjectName("PolicyEngine:type=PdpJmx")); } catch (MBeanRegistrationException | InstanceNotFoundException @@ -58,7 +57,7 @@ public class PdpJmxListener { * Start. */ public static void start() { - final MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + final var server = ManagementFactory.getPlatformMBeanServer(); try { server.registerMBean(PdpJmx.getInstance(), new ObjectName("PolicyEngine:type=PdpJmx")); } catch (InstanceAlreadyExistsException | MBeanRegistrationException diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/lock/AlwaysFailLock.java b/policy-core/src/main/java/org/onap/policy/drools/core/lock/AlwaysFailLock.java index 03024f52..dbb20883 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/lock/AlwaysFailLock.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/lock/AlwaysFailLock.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +20,15 @@ package org.onap.policy.drools.core.lock; +import lombok.NoArgsConstructor; /** * Lock implementation whose operations always fail. */ +@NoArgsConstructor public class AlwaysFailLock extends LockImpl { private static final long serialVersionUID = 1L; - /** - * Constructs the object. - */ - public AlwaysFailLock() { - super(); - } - /** * Constructs the object. * diff --git a/policy-core/src/main/java/org/onap/policy/drools/properties/DroolsPropertyConstants.java b/policy-core/src/main/java/org/onap/policy/drools/properties/DroolsPropertyConstants.java index 197c972d..110412a2 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/properties/DroolsPropertyConstants.java +++ b/policy-core/src/main/java/org/onap/policy/drools/properties/DroolsPropertyConstants.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-core * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,11 @@ package org.onap.policy.drools.properties; -public class DroolsPropertyConstants { +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DroolsPropertyConstants { /* Controller Properties */ @@ -36,8 +40,4 @@ public class DroolsPropertyConstants { public static final String RULES_GROUPID = "rules.groupId"; public static final String RULES_ARTIFACTID = "rules.artifactId"; public static final String RULES_VERSION = "rules.version"; - - private DroolsPropertyConstants() { - // do nothing - } } diff --git a/policy-core/src/main/java/org/onap/policy/drools/util/FeatureEnabledChecker.java b/policy-core/src/main/java/org/onap/policy/drools/util/FeatureEnabledChecker.java index aaafd0ec..755e7a18 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/util/FeatureEnabledChecker.java +++ b/policy-core/src/main/java/org/onap/policy/drools/util/FeatureEnabledChecker.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -21,22 +21,18 @@ package org.onap.policy.drools.util; import java.util.Properties; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; /** * Checks whether or not a feature is enabled. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class FeatureEnabledChecker { - /** - * Constructor. - */ - private FeatureEnabledChecker() { - super(); - } - /** * Determines if a feature is enabled. - * + * * @param props properties from which to extract the "enabled" flag * @param propName the name of the "enabled" property * @return {@code true} if the feature is enabled, or {@code false} if it is not diff --git a/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java index d56af118..043e2b10 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java +++ b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,8 @@ import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import lombok.NonNull; import org.apache.commons.io.IOUtils; import org.drools.compiler.kie.builder.impl.InternalKieModule; @@ -44,7 +46,6 @@ import org.kie.api.builder.KieBuilder; import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.Message; import org.kie.api.builder.ReleaseId; -import org.kie.api.builder.model.KieModuleModel; import org.kie.api.definition.KiePackage; import org.kie.api.definition.rule.Rule; import org.kie.api.runtime.KieContainer; @@ -56,7 +57,8 @@ import org.slf4j.LoggerFactory; /** * Kie related utilities. */ -public class KieUtils { +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class KieUtils { private static final Logger logger = LoggerFactory.getLogger(KieUtils.class); @@ -64,25 +66,21 @@ public class KieUtils { private static final String RESOURCE_PREFIX = "src/main/resources/drools"; private static final String RESOURCE_SUFFIX = ".drl"; - private KieUtils() { - // Utility class - } - /** * Installs a rules artifact in the local maven repository. */ public static ReleaseId installArtifact(File kmodule, File pom, String drlKJarPath, @NonNull List drls) throws IOException { - KieModuleModel kieModule = KieModuleModelImpl.fromXML(kmodule); + var kieModule = KieModuleModelImpl.fromXML(kmodule); - final KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem(); + final var kieFileSystem = KieServices.Factory.get().newKieFileSystem(); kieFileSystem.writeKModuleXML(kieModule.toXML()); kieFileSystem.writePomXML(new String(Files.readAllBytes(pom.toPath()))); for (File drl : drls) { kieFileSystem.write(drlKJarPath + drl.getName(), new String(Files.readAllBytes(drl.toPath()))); } - KieBuilder kieBuilder = build(kieFileSystem); + var kieBuilder = build(kieFileSystem); return getReleaseId(kieBuilder, pom); } @@ -95,7 +93,7 @@ public class KieUtils { } private static ReleaseId getReleaseId(KieBuilder kieBuilder, File pomFile) { - ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId(); + var releaseId = kieBuilder.getKieModule().getReleaseId(); KieMavenRepository .getKieMavenRepository() .installArtifact(releaseId, @@ -164,7 +162,7 @@ public class KieUtils { } private static KieBuilder build(KieFileSystem kieFileSystem) { - KieBuilder kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem); + var kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem); List messages = kieBuilder.buildAll().getResults().getMessages(); if (messages != null && !messages.isEmpty()) { throw new IllegalArgumentException(messages.toString()); @@ -198,11 +196,11 @@ public class KieUtils { } // generate a 'KieFileSystem' from these resources - KieServices kieServices = KieServices.Factory.get(); - KieFileSystem kfs = kieServices.newKieFileSystem(); - int index = 1; + var kieServices = KieServices.Factory.get(); + var kfs = kieServices.newKieFileSystem(); + var index = 1; while (resources.hasMoreElements()) { - URL url = resources.nextElement(); + var url = resources.nextElement(); try (InputStream is = url.openStream()) { // convert a resource to a byte array byte[] drl = IOUtils.toByteArray(is); @@ -216,7 +214,7 @@ public class KieUtils { } // do a build of the 'KieFileSystem' - KieBuilder builder = kieServices.newKieBuilder(kfs, classLoader); + var builder = kieServices.newKieBuilder(kfs, classLoader); builder.buildAll(); List results = builder.getResults().getMessages(); if (!results.isEmpty()) { -- cgit 1.2.3-korg