summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-05 14:17:01 -0400
committerJim Hahn <jrh3@att.com>2019-08-13 09:24:52 -0400
commit9e8023a455633c7daf0dd291c6268986e39352d7 (patch)
treedc25ae63c4a042a7f16c53d8c50ebcdb4493f5cb
parent177b67a70f99e0125fec7732f39c18840409f968 (diff)
Fix sonar issues in drools-applications
Addressed sonar issue, "Move constants to a class or enum.", by moving the "manager" object from the ControlLoopMetrics interface into a utility class, ControlLoopMetricsManager. Addressed sonar issue, "duplicated blocks of code must be removed.", by refactoring PolicyGuardYamlToXacml, extracing common functions. Addressed sonar issue, "Remove this unused import", in RestControlLoopManager. Addressed sonar issue, "Refactor this method to throw at most one checked exception", in event manager. Fixed likely new sonar issue with assigning to a parameter. Moved logging line to more appropriate place. Addressed reviewer comment: Use "replace" instead of "replaceAll", thus avoiding escaping Change-Id: I47db957c83c1b3e2bd2330474e261987c6f0aac6 Issue-ID: POLICY-1967 Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r--controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java3
-rw-r--r--controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java108
-rw-r--r--controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java3
-rw-r--r--controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetrics.java5
-rw-r--r--controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeature.java2
-rw-r--r--controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsManager.java39
-rw-r--r--controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java82
-rw-r--r--controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java132
8 files changed, 194 insertions, 180 deletions
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java
index 7d42f8f76..298513619 100644
--- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java
+++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java
@@ -399,9 +399,8 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
*
* @return a ControlLoopOperationManager
* @throws ControlLoopException if an error occurs
- * @throws AaiException if an error occurs retrieving information from A&AI
*/
- public ControlLoopOperationManager processControlLoop() throws ControlLoopException, AaiException {
+ public ControlLoopOperationManager processControlLoop() throws ControlLoopException {
validateFinalControlLoop();
//
// Is there a current operation?
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java
index 006899efe..cac1b8ee3 100644
--- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java
+++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java
@@ -98,33 +98,39 @@ public class ControlLoopOperationManager implements Serializable {
* @param policy the policy
* @param em the event manager
* @throws ControlLoopException if an error occurs
- * @throws AaiException if an error occurs retrieving information from A&AI
*/
public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em)
- throws ControlLoopException, AaiException {
+ throws ControlLoopException {
+
this.onset = onset;
this.policy = policy;
this.guardApprovalStatus = "NONE";
this.eventManager = em;
this.targetEntity = getTarget(policy);
- //
- // Let's make a sanity check
- //
- switch (policy.getActor()) {
- case "APPC":
- initAppc(onset, policy);
- break;
- case "SO":
- break;
- case "SDNR":
- break;
- case "VFC":
- break;
- case "SDNC":
- break;
- default:
- throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
+ try {
+
+ //
+ // Let's make a sanity check
+ //
+ switch (policy.getActor()) {
+ case "APPC":
+ initAppc(onset, policy);
+ break;
+ case "SO":
+ break;
+ case "SDNR":
+ break;
+ case "VFC":
+ break;
+ case "SDNC":
+ break;
+ default:
+ throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
+ }
+
+ } catch (AaiException e) {
+ throw new ControlLoopException(e.getMessage(), e);
}
}
@@ -210,9 +216,8 @@ public class ControlLoopOperationManager implements Serializable {
* @param policy the policy
* @return the target
* @throws ControlLoopException if an error occurs
- * @throws AaiException if an error occurs retrieving information from A&AI
*/
- public String getTarget(Policy policy) throws ControlLoopException, AaiException {
+ public String getTarget(Policy policy) throws ControlLoopException {
if (policy.getTarget() == null) {
throw new ControlLoopException("The target is null");
}
@@ -235,7 +240,7 @@ public class ControlLoopOperationManager implements Serializable {
}
- private String getVfModuleTarget() throws AaiException, ControlLoopException {
+ private String getVfModuleTarget() throws ControlLoopException {
VirtualControlLoopEvent virtualOnsetEvent = (VirtualControlLoopEvent) this.onset;
if (this.onset.getTarget().equalsIgnoreCase(VSERVER_VSERVER_NAME)) {
return virtualOnsetEvent.getAai().get(VSERVER_VSERVER_NAME);
@@ -253,17 +258,22 @@ public class ControlLoopOperationManager implements Serializable {
* If the vnf-name was retrieved from the onset then the vnf-id must be obtained from the event
* manager's A&AI GET query
*/
- String vnfId;
- if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
- vnfId = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset).getDefaultGenericVnf()
- .getVnfId();
- } else {
- vnfId = this.eventManager.getVnfResponse().getVnfId();
- }
- if (vnfId == null) {
- throw new AaiException("No vnf-id found");
+ try {
+ String vnfId;
+ if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
+ vnfId = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset).getDefaultGenericVnf()
+ .getVnfId();
+ } else {
+ vnfId = this.eventManager.getVnfResponse().getVnfId();
+ }
+ if (vnfId == null) {
+ throw new AaiException("No vnf-id found");
+ }
+ return vnfId;
+
+ } catch (AaiException e) {
+ throw new ControlLoopException(e.getMessage(), e);
}
- return vnfId;
}
throw new ControlLoopException("Target does not match target type");
}
@@ -274,10 +284,9 @@ public class ControlLoopOperationManager implements Serializable {
* @param onset the onset event
* @return the operation request
* @throws ControlLoopException if an error occurs
- * @throws AaiException if error occurs
*/
public Object startOperation(/* VirtualControlLoopEvent */ControlLoopEvent onset)
- throws ControlLoopException, AaiException {
+ throws ControlLoopException {
verifyOperatonCanRun();
//
@@ -293,19 +302,24 @@ public class ControlLoopOperationManager implements Serializable {
//
// Now determine which actor we need to construct a request for
//
- switch (policy.getActor()) {
- case "APPC":
- return startAppcOperation(onset, operation);
- case "SO":
- return startSoOperation(onset, operation);
- case "VFC":
- return startVfcOperation(onset, operation);
- case "SDNR":
- return startSdnrOperation(onset, operation);
- case "SDNC":
- return startSdncOperation(onset, operation);
- default:
- throw new ControlLoopException("invalid actor " + policy.getActor() + " on policy");
+ try {
+ switch (policy.getActor()) {
+ case "APPC":
+ return startAppcOperation(onset, operation);
+ case "SO":
+ return startSoOperation(onset, operation);
+ case "VFC":
+ return startVfcOperation(onset, operation);
+ case "SDNR":
+ return startSdnrOperation(onset, operation);
+ case "SDNC":
+ return startSdncOperation(onset, operation);
+ default:
+ throw new ControlLoopException("invalid actor " + policy.getActor() + " on policy");
+ }
+
+ } catch (AaiException e) {
+ throw new ControlLoopException(e.getMessage(), e);
}
}
diff --git a/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java b/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java
index 22e0451e3..4b62e489a 100644
--- a/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java
+++ b/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
@@ -41,7 +41,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.onap.policy.aai.AaiManager;
-import org.onap.policy.aai.util.AaiException;
import org.onap.policy.controlloop.ControlLoopException;
import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
import org.onap.policy.controlloop.params.ControlLoopParams;
diff --git a/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetrics.java b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetrics.java
index a7c8f11df..3cc02446a 100644
--- a/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetrics.java
+++ b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetrics.java
@@ -31,11 +31,6 @@ import org.onap.policy.drools.system.PolicyController;
public interface ControlLoopMetrics {
/**
- * Singleton manager object.
- */
- ControlLoopMetrics manager = new CacheBasedControlLoopMetricsManager();
-
- /**
* Gets all transaction identifiers being monitored.
*
* @return transaction id list
diff --git a/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeature.java b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeature.java
index b4057dfae..26627eb1c 100644
--- a/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeature.java
+++ b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeature.java
@@ -70,7 +70,7 @@ public class ControlLoopMetricsFeature implements PolicyControllerFeatureApi {
@Override
public boolean beforeDeliver(PolicyController controller, CommInfrastructure protocol, String topic, Object event) {
if (event instanceof VirtualControlLoopNotification) {
- ControlLoopMetrics.manager.transactionEvent(controller, (VirtualControlLoopNotification) event);
+ ControlLoopMetricsManager.getManager().transactionEvent(controller, (VirtualControlLoopNotification) event);
}
/* do not take ownership */
diff --git a/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsManager.java b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsManager.java
new file mode 100644
index 000000000..c4ae11bdf
--- /dev/null
+++ b/controlloop/common/feature-controlloop-trans/src/main/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsManager.java
@@ -0,0 +1,39 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.apps.controlloop.feature.trans;
+
+import lombok.Getter;
+
+/**
+ * Control Loop Metrics Tracker.
+ */
+public class ControlLoopMetricsManager {
+
+ /**
+ * Singleton manager object.
+ */
+ @Getter
+ private static final ControlLoopMetrics manager = new CacheBasedControlLoopMetricsManager();
+
+ private ControlLoopMetricsManager() {
+ // do nothing
+ }
+}
diff --git a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java
index abc5db8e6..bbbcc132f 100644
--- a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java
+++ b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java
@@ -66,9 +66,9 @@ public class ControlLoopMetricsFeatureTest {
@Test
public void cacheDefaults() {
- assertEquals(3, ControlLoopMetrics.manager.getCacheSize());
- assertEquals(2, ControlLoopMetrics.manager.getTransactionTimeout());
- assertEquals(0, ControlLoopMetrics.manager.getCacheOccupancy());
+ assertEquals(3, ControlLoopMetricsManager.getManager().getCacheSize());
+ assertEquals(2, ControlLoopMetricsManager.getManager().getTransactionTimeout());
+ assertEquals(0, ControlLoopMetricsManager.getManager().getCacheOccupancy());
}
@Test
@@ -82,7 +82,7 @@ public class ControlLoopMetricsFeatureTest {
notification.setRequestId(requestId);
feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
- assertNull(ControlLoopMetrics.manager.getTransaction(requestId));
+ assertNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
this.cacheDefaults();
}
@@ -95,14 +95,15 @@ public class ControlLoopMetricsFeatureTest {
notification.setNotification(ControlLoopNotificationType.ACTIVE);
feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
- assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId));
- assertTrue(ControlLoopMetrics.manager.getTransaction(requestId).getFrom().contains(testController.getName()));
- assertNotNull(ControlLoopMetrics.manager.getTransaction(requestId).getNotificationTime());
- assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == 1);
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
+ assertTrue(ControlLoopMetricsManager.getManager().getTransaction(requestId).getFrom()
+ .contains(testController.getName()));
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId).getNotificationTime());
+ assertTrue(ControlLoopMetricsManager.getManager().getCacheOccupancy() == 1);
/* wait for the entries to expire */
- await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
- .until(() -> ControlLoopMetrics.manager.getTransaction(requestId) == null);
+ await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
+ .until(() -> ControlLoopMetricsManager.getManager().getTransaction(requestId) == null);
this.cacheDefaults();
}
@@ -113,56 +114,61 @@ public class ControlLoopMetricsFeatureTest {
new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT,
notification);
- assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
- ControlLoopMetrics.manager.resetCache(ControlLoopMetrics.manager.getCacheSize(),
- ControlLoopMetrics.manager.getTransactionTimeout());
- assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
+ ControlLoopMetricsManager.getManager().resetCache(ControlLoopMetricsManager.getManager().getCacheSize(),
+ ControlLoopMetricsManager.getManager().getTransactionTimeout());
+ assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
this.cacheDefaults();
}
@Test
public void removeTransaction() {
VirtualControlLoopNotification notification = this.generateNotification();
- assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
- ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
+ assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
+ ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
- ControlLoopMetrics.manager.transactionEvent(testController, notification);
- assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
- ControlLoopMetrics.manager.removeTransaction(notification.getRequestId());
- assertNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
+ ControlLoopMetricsManager.getManager().transactionEvent(testController, notification);
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
+ ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
+ assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
}
@Test
public void eviction() throws InterruptedException {
ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
- for (int i = 0; i < ControlLoopMetrics.manager.getCacheSize(); i++) {
+ for (int i = 0; i < ControlLoopMetricsManager.getManager().getCacheSize(); i++) {
VirtualControlLoopNotification notification = generateNotification();
feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, notification);
- assertNotNull(ControlLoopMetrics.manager.getTransaction(notification.getRequestId()));
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
}
- assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
+ assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
+ ControlLoopMetricsManager.getManager().getCacheOccupancy());
VirtualControlLoopNotification overflowNotification = generateNotification();
feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, overflowNotification);
- assertEquals(ControlLoopMetrics.manager.getCacheOccupancy(), ControlLoopMetrics.manager.getCacheOccupancy());
- assertNotNull(ControlLoopMetrics.manager.getTransaction(overflowNotification.getRequestId()));
- assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager.getCacheSize());
- assertTrue(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
- assertFalse(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
- assertFalse(ControlLoopMetrics.manager.getTransactions().isEmpty());
+ assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
+ ControlLoopMetricsManager.getManager().getCacheOccupancy());
+ assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(overflowNotification.getRequestId()));
+ assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().size() == ControlLoopMetricsManager
+ .getManager().getCacheSize());
+ assertTrue(ControlLoopMetricsManager.getManager().getCacheOccupancy() == ControlLoopMetricsManager.getManager()
+ .getCacheSize());
+ assertFalse(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
+ assertFalse(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
/* wait for the entries to expire */
- await().atMost(ControlLoopMetrics.manager.getTransactionTimeout() + 1, TimeUnit.SECONDS)
- .until(() -> ControlLoopMetrics.manager.getTransactions().isEmpty());
-
- ControlLoopMetrics.manager.refresh();
- assertTrue(ControlLoopMetrics.manager.getTransactionIds().size() == ControlLoopMetrics.manager
- .getCacheOccupancy());
- assertFalse(ControlLoopMetrics.manager.getCacheOccupancy() == ControlLoopMetrics.manager.getCacheSize());
- assertTrue(ControlLoopMetrics.manager.getTransactionIds().isEmpty());
- assertTrue(ControlLoopMetrics.manager.getTransactions().isEmpty());
+ await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
+ .until(() -> ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
+
+ ControlLoopMetricsManager.getManager().refresh();
+ assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().size() == ControlLoopMetricsManager
+ .getManager().getCacheOccupancy());
+ assertFalse(ControlLoopMetricsManager.getManager().getCacheOccupancy() == ControlLoopMetricsManager.getManager()
+ .getCacheSize());
+ assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
+ assertTrue(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
this.cacheDefaults();
}
diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
index a64abcad4..057c3f116 100644
--- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
+++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* guard
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017, 2019 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.
@@ -25,9 +25,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
+import java.util.function.Consumer;
import org.onap.policy.controlloop.policy.guard.Constraint;
import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
import org.onap.policy.controlloop.policy.guard.GuardPolicy;
@@ -44,7 +42,7 @@ public class PolicyGuardYamlToXacml {
/**
* Convert from Yaml to Xacml.
- *
+ *
* @param yamlFile the Yaml file
* @param xacmlTemplate the Xacml template
* @param xacmlPolicyOutput the Xacml output
@@ -78,7 +76,7 @@ public class PolicyGuardYamlToXacml {
/**
* Generate a Xacml guard.
- *
+ *
* @param xacmlTemplateContent the Xacml template content
* @param matchParameters the paremeters to use
* @param constraint the constraint to use
@@ -86,29 +84,9 @@ public class PolicyGuardYamlToXacml {
*/
private static String generateXacmlGuard(String xacmlTemplateContent, MatchParameters matchParameters,
Constraint constraint) {
- Pattern pattern = Pattern.compile("\\$\\{clname\\}");
- Matcher matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getControlLoopName())) {
- matchParameters.setControlLoopName(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{actor\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getActor())) {
- matchParameters.setActor(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getActor());
+ xacmlTemplateContent = doCommonReplacements(xacmlTemplateContent, matchParameters, constraint);
- pattern = Pattern.compile("\\$\\{recipe\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getRecipe())) {
- matchParameters.setRecipe(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getRecipe());
-
- pattern = Pattern.compile("\\$\\{targets\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
String targetsRegex = "";
if (isNullOrEmptyList(matchParameters.getTargets())) {
targetsRegex = ".*";
@@ -125,33 +103,47 @@ public class PolicyGuardYamlToXacml {
}
targetsRegex = targetsRegexSb.toString();
}
- xacmlTemplateContent = matcher.replaceAll(targetsRegex);
+ xacmlTemplateContent = xacmlTemplateContent.replace("${targets}", targetsRegex);
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${limit}",
+ constraint.getFreq_limit_per_target().toString());
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${twValue}", constraint.getTime_window().get("value"));
- pattern = Pattern.compile("\\$\\{limit\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getFreq_limit_per_target().toString());
+ xacmlTemplateContent = xacmlTemplateContent.replace("${twUnits}", constraint.getTime_window().get("units"));
- pattern = Pattern.compile("\\$\\{twValue\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getTime_window().get("value"));
+ logger.debug(xacmlTemplateContent);
- pattern = Pattern.compile("\\$\\{twUnits\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getTime_window().get("units"));
+ return xacmlTemplateContent;
+ }
+ private static String doCommonReplacements(String xacmlTemplateContent, MatchParameters matchParameters,
+ Constraint constraint) {
- pattern = Pattern.compile("\\$\\{guardActiveStart\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("start"));
+ replaceNullOrEmpty(matchParameters.getControlLoopName(), matchParameters::setControlLoopName, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${clname}", matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{guardActiveEnd\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("end"));
- logger.debug(xacmlTemplateContent);
+ replaceNullOrEmpty(matchParameters.getActor(), matchParameters::setActor, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${actor}", matchParameters.getActor());
+
+ replaceNullOrEmpty(matchParameters.getRecipe(), matchParameters::setRecipe, ".*");
+ xacmlTemplateContent = xacmlTemplateContent.replace("${recipe}", matchParameters.getRecipe());
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${guardActiveStart}",
+ constraint.getActive_time_range().get("start"));
+
+ xacmlTemplateContent = xacmlTemplateContent.replace("${guardActiveEnd}",
+ constraint.getActive_time_range().get("end"));
return xacmlTemplateContent;
}
+ private static void replaceNullOrEmpty(String text, Consumer<String> replacer, String newValue) {
+ if (isNullOrEmpty(text)) {
+ replacer.accept(newValue);
+ }
+ }
+
public static boolean isNullOrEmpty(String string) {
return string == null || string.trim().isEmpty();
}
@@ -162,7 +154,7 @@ public class PolicyGuardYamlToXacml {
/**
* Convert from Yaml to Xacml blacklist.
- *
+ *
* @param yamlFile the Yaml file
* @param xacmlTemplate the Xacml template
* @param xacmlPolicyOutput the Xacml output
@@ -185,6 +177,8 @@ public class PolicyGuardYamlToXacml {
String xacmlPolicyContent = generateXacmlGuardBlacklist(xacmlTemplateContent,
guardPolicy.getMatch_parameters(), constraint);
+ logger.debug("{}", xacmlPolicyContent);
+
Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
} catch (IOException e) {
@@ -194,49 +188,17 @@ public class PolicyGuardYamlToXacml {
private static String generateXacmlGuardBlacklist(String xacmlTemplateContent, MatchParameters matchParameters,
Constraint constraint) {
- Pattern pattern = Pattern.compile("\\$\\{clname\\}");
- Matcher matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getControlLoopName())) {
- matchParameters.setControlLoopName(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getControlLoopName());
- pattern = Pattern.compile("\\$\\{actor\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getActor())) {
- matchParameters.setActor(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getActor());
-
- pattern = Pattern.compile("\\$\\{recipe\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- if (isNullOrEmpty(matchParameters.getRecipe())) {
- matchParameters.setRecipe(".*");
- }
- xacmlTemplateContent = matcher.replaceAll(matchParameters.getRecipe());
-
- pattern = Pattern.compile("\\$\\{guardActiveStart\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("start"));
-
- pattern = Pattern.compile("\\$\\{guardActiveEnd\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll(constraint.getActive_time_range().get("end"));
- logger.debug(xacmlTemplateContent);
+ String result = doCommonReplacements(xacmlTemplateContent, matchParameters, constraint);
for (String target : constraint.getBlacklist()) {
- pattern = Pattern.compile("\\$\\{blackListElement\\}");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent =
- matcher.replaceAll("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + target
- + "</AttributeValue>" + "\n\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
+ result = result.replace("${blackListElement}",
+ "<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + target
+ + "</AttributeValue>" + "\n\t\t\t\t\t\t\\${blackListElement}\n");
}
- pattern = Pattern.compile("\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
- matcher = pattern.matcher(xacmlTemplateContent);
- xacmlTemplateContent = matcher.replaceAll("");
-
+ result = result.replace("\t\t\t\t\t\t\\${blackListElement}\n", "");
- return xacmlTemplateContent;
+ return result;
}
}