* Adding in the statistics for decisions and errors.
* Cleaned up sonar issues and added code coverage.
* Sped up JUnit tests
* Fix JUnit issues with not finding application path
* Fix TestDecision not finding persistence.xml
* Fix for lingering statistics from previous runs. That
needs to be addressed at a later time.
* Changed persistence to use properties for configuration
of database rather than hard coding the persistence.xml
* Fix for Josh's comment to use else-if
* Changed to use apache Pair
* Added blacklist guard policy
Issue-ID: POLICY-1440
Change-Id: I56af8c3dcc82463f7381f1eaea7f1440b76200bd
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
package org.onap.policy.pdp.xacml.application.common;
+import com.att.research.xacml.api.Response;
+
import java.nio.file.Path;
import java.util.List;
+import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
* @param request Incoming DecisionRequest object
* @return response Responding DecisionResponse object
*/
- DecisionResponse makeDecision(DecisionRequest request);
+ Pair<DecisionResponse, Response> makeDecision(DecisionRequest request);
}
//
// In case there are any overloaded properties for the JPA
//
- Properties emProperties = new Properties(properties);
+ Properties emProperties = new Properties();
+ emProperties.putAll(properties);
//
// Create the entity manager factory
//
import java.util.Collection;
import java.util.Properties;
-import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
//
// In case there are any overloaded properties for the JPA
//
- Properties emProperties = new Properties(properties);
+ Properties emProperties = new Properties();
+ emProperties.putAll(properties);
//
// Create the entity manager factory
//
try {
jsonPolicy = coder.encode(toscaPolicy);
} catch (CoderException e) {
- LOGGER.error("Failed to encode policy to json", e);
- throw new ToscaPolicyConversionException(e);
+ throw new ToscaPolicyConversionException("Failed to encode policy to json", e);
}
addObligation(rule, jsonPolicy);
//
//
// Add in the Policy Version
//
- policy.setVersion(map.get("policy-version").toString());
+ policy.setVersion(map.get("policy-version"));
}
return policy;
}
//
AttributeValueType value = new AttributeValueType();
value.setDataType(ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_DATATYPE.stringValue());
- value.getContent().add(jsonPolicy.toString());
+ value.getContent().add(jsonPolicy);
//
// Create our AttributeAssignmentExpression where we will
// store the contents of the policy in JSON format.
import com.att.research.xacml.std.pip.engines.StdConfigurableEngine;
import java.math.BigInteger;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
}
@Override
- public DecisionResponse makeDecision(DecisionRequest request) {
+ public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request) {
//
// Convert to a XacmlRequest
//
//
// Convert to a DecisionResponse
//
- return this.getTranslator().convertResponse(xacmlResponse);
+ return Pair.of(this.getTranslator().convertResponse(xacmlResponse), xacmlResponse);
}
protected abstract ToscaPolicyTranslator getTranslator(String type);
<persistence-unit name="OperationsHistoryPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>org.onap.policy.pdp.xacml.application.common.OnapOperationsHistoryDbao</class>
+
+ <class>org.onap.policy.pdp.xacml.application.common.operationshistory.Dbao</class>
+
<properties>
- <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
- <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://policydb:3306/policy" />
- <property name="javax.persistence.jdbc.user" value="policy_user" />
- <property name="javax.persistence.jdbc.password" value="policy_user" />
<property name="javax.persistence.schema-generation.database.action" value="create" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.logging.level" value="FINE" />
- <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
- <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE" />
- <property name="javax.persistence.jdbc.user" value="policy" />
- <property name="javax.persistence.jdbc.password" value="P01icY" />
+ <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
</properties>
#
count-recent-operations.persistenceunit=PipEngineTest
get-operation-outcome.persistenceunit=PipEngineTest
+
+#
+# JPA Properties
+#
+javax.persistence.jdbc.driver=org.h2.Driver
+javax.persistence.jdbc.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE
+javax.persistence.jdbc.user=policy
+javax.persistence.jdbc.password=P01icY
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
*
*/
public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
-
private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
private static final String STRING_VERSION100 = "1.0.0";
private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
private CoordinationGuardTranslator coordinationTranslator = new CoordinationGuardTranslator();
- /** Constructor.
+ /**
+ * Constructor.
*
*/
public GuardPdpApplication() {
this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.MinMax",
STRING_VERSION100));
+ this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
+ "onap.policies.controlloop.guard.Blacklist",
+ STRING_VERSION100));
this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.coordination.FirstBlocksSecond",
STRING_VERSION100));
import lombok.ToString;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
-import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
@Getter
@Setter
public class LegacyGuardPolicyRequest {
private static final String STR_GUARD = "guard";
- private static final String URN_ONAP = ToscaDictionary.URN_ONAP;
@XACMLSubject(includeInResults = true)
private String onapName;
private static final String FIELD_GUARD_ACTIVE_START = "guardActiveStart";
private static final String FIELD_GUARD_ACTIVE_END = "guardActiveEnd";
+ private static final String FIELD_TARGET = "targets";
public LegacyGuardTranslator() {
super();
//
newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_DENY_UNLESS_PERMIT.stringValue());
//
- // Generate the TargetType
+ // Generate the TargetType - add true if not blacklist
//
- newPolicyType.setTarget(this.generateTargetType(toscaPolicy.getProperties()));
+ newPolicyType.setTarget(this.generateTargetType(toscaPolicy.getProperties(),
+ ! "onap.policies.controlloop.guard.Blacklist".equals(toscaPolicy.getType())));
//
// Now create the Permit Rule
//
return policy;
}
- protected TargetType generateTargetType(Map<String, Object> properties) {
+ protected TargetType generateTargetType(Map<String, Object> properties, boolean addTargets) {
//
// Go through potential properties
//
if (properties.containsKey("recipe")) {
addMatch(allOf, properties.get("recipe"), ToscaDictionary.ID_RESOURCE_GUARD_RECIPE);
}
- if (properties.containsKey("targets")) {
- addMatch(allOf, properties.get("targets"), ToscaDictionary.ID_RESOURCE_GUARD_TARGETID);
+ if (addTargets) {
+ if (properties.containsKey("targets")) {
+ addMatch(allOf, properties.get("targets"), ToscaDictionary.ID_RESOURCE_GUARD_TARGETID);
+ }
}
if (properties.containsKey("clname")) {
addMatch(allOf, properties.get("clname"), ToscaDictionary.ID_RESOURCE_GUARD_CLNAME);
}
- if (properties.containsKey("targets")) {
- addMatch(allOf, properties.get("targets"), ToscaDictionary.ID_RESOURCE_GUARD_TARGETID);
- }
//
// Create target
//
return generateFrequencyPermit(policyName, properties);
} else if ("onap.policies.controlloop.guard.MinMax".equals(policyType)) {
return generateMinMaxPermit(policyName, properties);
+ } else if ("onap.policies.controlloop.guard.Blacklist".equals(policyType)) {
+ return generateBlacklistPermit(policyName, properties);
}
LOGGER.error("Missing policy type in the policy");
return null;
return permit;
}
+ private static RuleType generateBlacklistPermit(String policyName, Map<String, Object> properties) {
+ //
+ // Generate target
+ //
+ if (! properties.containsKey(FIELD_TARGET)) {
+ LOGGER.error("Missing target for blacklist policy");
+ return null;
+ }
+ final ApplyType targetApply = generateTargetApply(properties.get(FIELD_TARGET));
+ //
+ // Get the properties that are common among guards
+ //
+ String guardActiveStart = null;
+ if (properties.containsKey(FIELD_GUARD_ACTIVE_START)) {
+ guardActiveStart = properties.get(FIELD_GUARD_ACTIVE_START).toString();
+ }
+ String guardActiveEnd = null;
+ if (properties.containsKey(FIELD_GUARD_ACTIVE_END)) {
+ guardActiveEnd = properties.get(FIELD_GUARD_ACTIVE_END).toString();
+ }
+ //
+ // Generate the time in range
+ //
+ final ApplyType timeRange = generateTimeInRange(guardActiveStart, guardActiveEnd);
+ //
+ // Create our rule
+ //
+ RuleType permit = new RuleType();
+ permit.setDescription("Default is to PERMIT if the policy matches.");
+ permit.setRuleId(policyName + ":rule");
+ permit.setEffect(EffectType.PERMIT);
+ permit.setTarget(new TargetType());
+ //
+ // Create our condition
+ //
+ ObjectFactory factory = new ObjectFactory();
+ ApplyType innerApply;
+ if (timeRange != null) {
+ ApplyType applyAnd = new ApplyType();
+ applyAnd.setDescription("Combine the timeRange with target to create AND");
+ applyAnd.setFunctionId(XACML3.ID_FUNCTION_AND.stringValue());
+ applyAnd.getExpression().add(factory.createApply(timeRange));
+ applyAnd.getExpression().add(factory.createApply(targetApply));
+ //
+ // Now we need to NOT this so the permit happens
+ //
+ ApplyType applyNot = new ApplyType();
+ applyNot.setDescription("This should be false for a permit.");
+ applyNot.setFunctionId(XACML3.ID_FUNCTION_NOT.stringValue());
+ applyNot.getExpression().add(factory.createApply(applyAnd));
+ innerApply = applyNot;
+ } else {
+ //
+ // Just the target is needed
+ //
+ ApplyType applyNot = new ApplyType();
+ applyNot.setDescription("This should be false for a permit.");
+ applyNot.setFunctionId(XACML3.ID_FUNCTION_NOT.stringValue());
+ applyNot.getExpression().add(factory.createApply(targetApply));
+ innerApply = applyNot;
+ }
+ //
+ // Create our condition
+ //
+ final ConditionType condition = new ConditionType();
+ //
+ // Add into the condition
+ //
+ condition.setExpression(factory.createApply(innerApply));
+ //
+ // Add the condition
+ //
+ permit.setCondition(condition);
+ return permit;
+ }
+
private static ApplyType generateTimeInRange(String start, String end) {
if (start == null || end == null) {
LOGGER.warn("Missing time range start {} end {}", start, end);
designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
designator.setDataType(XACML3.ID_DATATYPE_INTEGER.stringValue());
//
- // TODO Add this back in when the operational database PIP is configured.
- // The issuer indicates that the PIP will be providing this attribute during
- // the decision making.
- //
- // Right now I am faking the count value by re-using the request-id field
+ // Setup issuer
//
String issuer = ToscaDictionary.GUARD_ISSUER_PREFIX
+ CountRecentOperationsPip.ISSUER_NAME
return applyLessThanEqual;
}
+ @SuppressWarnings("unchecked")
+ private static ApplyType generateTargetApply(Object targetObject) {
+ ObjectFactory factory = new ObjectFactory();
+ //
+ // Create a bag of values
+ //
+ ApplyType applyStringBag = new ApplyType();
+ applyStringBag.setDescription("Bag the target values");
+ applyStringBag.setFunctionId(XACML3.ID_FUNCTION_STRING_BAG.stringValue());
+ if (targetObject instanceof Collection) {
+ for (Object target : ((Collection<Object>) targetObject)) {
+ if (! (target instanceof String)) {
+ LOGGER.error("Collection of unsupported objects {}", target.getClass());
+ return null;
+ }
+ AttributeValueType value = new AttributeValueType();
+ value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
+ value.getContent().add(target.toString());
+ applyStringBag.getExpression().add(factory.createAttributeValue(value));
+ }
+ } else if (targetObject instanceof String) {
+ AttributeValueType value = new AttributeValueType();
+ value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
+ value.getContent().add(targetObject.toString());
+ applyStringBag.getExpression().add(factory.createAttributeValue(value));
+ } else {
+ LOGGER.warn("Unsupported object for target {}", targetObject.getClass());
+ return null;
+ }
+ //
+ // Create our designator
+ //
+ AttributeDesignatorType designator = new AttributeDesignatorType();
+ designator.setAttributeId(ToscaDictionary.ID_RESOURCE_GUARD_TARGETID.stringValue());
+ designator.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
+ designator.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
+ //
+ // Create apply for our AnyOf
+ //
+ ApplyType applyAnyOf = new ApplyType();
+ applyAnyOf.setDescription("Find designator as anyof the possible values");
+ applyAnyOf.setFunctionId(XACML3.ID_FUNCTION_ANY_OF.stringValue());
+ applyAnyOf.getExpression().add(factory.createAttributeDesignator(designator));
+ applyAnyOf.getExpression().add(factory.createApply(applyStringBag));
+ return applyAnyOf;
+ }
+
private static Integer parseInteger(String strInteger) {
Integer theInt = null;
try {
import static org.assertj.core.api.Assertions.assertThat;
+import com.att.research.xacml.api.Response;
+
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
+import org.apache.commons.lang3.tuple.Pair;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(request);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(request);
//
// Check decision
//
- checkDecision(expected, response);
+ checkDecision(expected, decision.getKey());
}
@Test
import static org.assertj.core.api.Assertions.assertThat;
+import com.att.research.xacml.api.Response;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
+import org.apache.commons.lang3.tuple.Pair;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(request);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(request);
//
// Check decision
//
- checkDecision(expected, response);
+ checkDecision(expected, decision.getKey());
}
@Test
// can support the correct policy types.
//
assertThat(service.supportedPolicyTypes()).isNotEmpty();
- assertThat(service.supportedPolicyTypes().size()).isEqualTo(3);
+ assertThat(service.supportedPolicyTypes().size()).isEqualTo(4);
assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0"))).isTrue();
assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.MinMax", "1.0.0"))).isTrue();
assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.MinMax", "1.0.1"))).isFalse();
+ assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ "onap.policies.controlloop.guard.Blacklist", "1.0.0"))).isTrue();
+ assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
+ "onap.policies.controlloop.guard.Blacklist", "1.0.1"))).isFalse();
assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
"onap.policies.controlloop.guard.coordination.FirstBlocksSecond", "1.0.0"))).isTrue();
assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
//
// Ask for a decision - should get permit
//
- DecisionResponse response = service.makeDecision(request);
- LOGGER.info("Looking for Permit Decision {}", response);
- assertThat(response).isNotNull();
- assertThat(response.getStatus()).isNotNull();
- assertThat(response.getStatus()).isEqualTo("Permit");
+ Pair<DecisionResponse, Response> decision = service.makeDecision(request);
+ LOGGER.info("Looking for Permit Decision {}", decision.getKey());
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getStatus()).isNotNull();
+ assertThat(decision.getKey().getStatus()).isEqualTo("Permit");
//
// Try a deny
//
guard.put("vfCount", "10");
resource.put("guard", guard);
request.setResource(resource);
- response = service.makeDecision(request);
- LOGGER.info("Looking for Deny Decision {}", response);
- assertThat(response).isNotNull();
- assertThat(response.getStatus()).isNotNull();
- assertThat(response.getStatus()).isEqualTo("Deny");
+ decision = service.makeDecision(request);
+ LOGGER.info("Looking for Deny Decision {}", decision.getKey());
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getStatus()).isNotNull();
+ assertThat(decision.getKey().getStatus()).isEqualTo("Deny");
+ }
+
+ @Test
+ public void test6Blacklist() throws CoderException, XacmlApplicationException {
+ LOGGER.info("**************** Running test4 ****************");
+ //
+ // Setup requestVfCount1 to point to another target for this test
+ //
+ ((Map<String, Object>)requestVfCount3.getResource().get("guard")).put("targets", "vLoadBalancer-01");
+ //
+ // vfcount=1 above min of 2: should get a permit
+ //
+ requestAndCheckDecision(requestVfCount3, PERMIT);
+ //
+ // Now load the vDNS blacklist policy
+ //
+ TestUtils.loadPolicies("src/test/resources/vDNS.policy.guard.blacklist.output.tosca.yaml", service);
+ //
+ // vfcount=1 above min of 2: should get a permit
+ //
+ requestAndCheckDecision(requestVfCount3, DENY);
}
@SuppressWarnings("unchecked")
+++ /dev/null
-#
-# Create the operations history table
-#
-CREATE TABLE `operationshistory`
- (
- `id` bigint not null,
- `closedLoopName` varchar(255) not null,
- `requestId` varchar(50) not null,
- `subrequestId` varchar(50) not null,
- `actor` varchar(50) not null,
- `operation` varchar(50) not null,
- `target` varchar(50) not null,
- `starttime` timestamp not null,
- `outcome` varchar(50) not null,
- `message` varchar(255) not null,
- `endtime` timestamp not null);
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.logging.level" value="FINE" />
- <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
- <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE" />
- <property name="javax.persistence.jdbc.user" value="policy" />
- <property name="javax.persistence.jdbc.password" value="P01icY" />
+ <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
- <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/createtest.sql"/>
</properties>
</persistence-unit>
--- /dev/null
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+topology_template:
+ policies:
+ -
+ guard.blacklist.scaleout:
+ type: onap.policies.controlloop.guard.Blacklist
+ version: 1.0.0
+ metadata:
+ policy-id: guard.frequency.scaleout
+ policy-version: 1
+ properties:
+ actor: SO
+ recipe: VF Module Create
+ targets: vLoadBalancer-01
+ clname: ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3
+ limit: 2
+ guardActiveStart: 00:00:00-05:00
+ guardActiveEnd: 23:59:59-05:00
#
# Make pips available to finder
#
-xacml.pip.engines=count-recent-operations,get-operation-outcome
\ No newline at end of file
+xacml.pip.engines=count-recent-operations,get-operation-outcome
+
+#
+# JPA Properties
+#
+javax.persistence.jdbc.driver=org.h2.Driver
+javax.persistence.jdbc.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE
+javax.persistence.jdbc.user=policy
+javax.persistence.jdbc.password=P01icY
import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
import org.onap.policy.pdp.xacml.application.common.std.StdCombinedPolicyResultsTranslator;
import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* This is the engine class that manages the instance of the XACML PDP engine.
*/
public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider {
- private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplication.class);
private static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring";
private static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring";
import static org.assertj.core.api.Assertions.assertThat;
+import com.att.research.xacml.api.Response;
+
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
+import org.apache.commons.lang3.tuple.Pair;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(requestSinglePolicy);
- LOGGER.info("Decision {}", response);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
+ LOGGER.info("Decision {}", decision);
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(0);
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
}
@Test
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(requestSinglePolicy);
- LOGGER.info("Decision {}", response);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
+ LOGGER.info("Decision {}", decision);
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(1);
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
//
// Dump it out as Json
//
- LOGGER.info(gson.encode(response));
+ LOGGER.info(gson.encode(decision.getKey()));
LOGGER.info("Now testing unloading of policy");
//
// Now unload it
//
// Ask for a decision
//
- response = service.makeDecision(requestSinglePolicy);
- LOGGER.info("Decision {}", response);
-
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(0);
- }
-
- @Test
- public void test4BadPolicies() {
- /*
- *
- * THESE TEST SHOULD BE MOVED INTO THE API PROJECT
- *
- //
- // No need for service, just test some of the methods
- // for bad policies
- //
- MonitoringPdpApplication onapPdpEngine = new MonitoringPdpApplication();
-
- assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
- try (InputStream is =
- new FileInputStream("src/test/resources/test.monitoring.policy.missingmetadata.yaml")) {
- onapPdpEngine.convertPolicies(is);
- }
- }).withMessageContaining("missing metadata section");
-
- assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
- try (InputStream is =
- new FileInputStream("src/test/resources/test.monitoring.policy.missingtype.yaml")) {
- onapPdpEngine.convertPolicies(is);
- }
- }).withMessageContaining("missing type value");
-
- assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
- try (InputStream is =
- new FileInputStream("src/test/resources/test.monitoring.policy.missingversion.yaml")) {
- onapPdpEngine.convertPolicies(is);
- }
- }).withMessageContaining("missing version value");
-
- assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
- try (InputStream is =
- new FileInputStream("src/test/resources/test.monitoring.policy.badmetadata.1.yaml")) {
- onapPdpEngine.convertPolicies(is);
- }
- }).withMessageContaining("missing metadata policy-version");
-
- assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
- try (InputStream is =
- new FileInputStream("src/test/resources/test.monitoring.policy.badmetadata.2.yaml")) {
- onapPdpEngine.convertPolicies(is);
- }
- }).withMessageContaining("missing metadata policy-id");
+ decision = service.makeDecision(requestSinglePolicy);
+ LOGGER.info("Decision {}", decision.getKey());
- */
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
}
}
+++ /dev/null
-tosca_definitions_version: tosca_simple_yaml_1_0_0
-policies:
- -
- test.monitoring.policy.badmetadata.1:
- type: onap.policies.monitoring.cdap.tca.hi.lo.app
- version: 1.0.0
- metadata:
- policy-id: test.monitoring.policy.badmetadata.1
- properties:
- domain: measurementsForVfScaling
+++ /dev/null
-tosca_definitions_version: tosca_simple_yaml_1_0_0
-policies:
- -
- onap.scaleout.tca:
- type: onap.policies.monitoring.cdap.tca.hi.lo.app
- version: 1.0.0
- metadata:
- policy-version: 1
- properties:
- domain: measurementsForVfScaling
+++ /dev/null
-tosca_definitions_version: tosca_simple_yaml_1_0_0
-policies:
- -
- onap.scaleout.tca:
- type: onap.policies.monitoring.test
- description: I am a test policy
- version: 1.0.0
- properties:
- domain: measurementsForVfScaling
+++ /dev/null
-tosca_definitions_version: tosca_simple_yaml_1_0_0
-policies:
- -
- onap.scaleout.tca:
- description: I am a test policy
- version: 1.0.0
- metadata:
- policy-id: onap.scaleout.tca
- policy-version: 10
- properties:
- domain: measurementsForVfScaling
+++ /dev/null
-tosca_definitions_version: tosca_simple_yaml_1_0_0
-policies:
- -
- onap.scaleout.tca:
- type: onap.policies.monitoring.test
- description: I am a test policy
- metadata:
- policy-id: onap.scaleout.tca
- policy-version: 10
- properties:
- domain: measurementsForVfScaling
//
for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
if (policyTypeId.equals(supported)) {
+ LOGGER.info("optimization can support {}", supported);
return true;
}
}
import static org.assertj.core.api.Assertions.assertThat;
+import com.att.research.xacml.api.Response;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.ServiceLoader;
+import org.apache.commons.lang3.tuple.Pair;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(requestAffinity);
- LOGGER.info("Decision {}", response);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(requestAffinity);
+ LOGGER.info("Decision {}", decision.getKey());
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(0);
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
}
@Test
//
// Ask for a decision
//
- DecisionResponse response = service.makeDecision(requestAffinity);
- LOGGER.info("Decision {}", response);
+ Pair<DecisionResponse, Response> decision = service.makeDecision(requestAffinity);
+ LOGGER.info("Decision {}", decision.getKey());
- assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(1);
+ assertThat(decision.getKey()).isNotNull();
+ assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
//
// Dump it out as Json
//
- LOGGER.info(gson.encode(response));
+ LOGGER.info(gson.encode(decision.getKey()));
}
}
public void pdpRegistration(PdpStatus status) throws TopicSinkClientException {
try {
if (!client.send(status)) {
- LOGGER.error("Failed to send to topic sink " + client.getTopic());
- return;
+ LOGGER.error("Failed to send to topic sink {}", client.getTopic());
}
} catch (IllegalStateException e) {
- LOGGER.error("Failed ot send to topic sink " + client.getTopic(), e);
- return;
+ LOGGER.error("Failed ot send to topic sink {}", client.getTopic(), e);
}
}
}
// Send State Change Status to PAP
if (!client.send(newStatus)) {
- LOGGER.error("failed to send to topic sink " + client.getTopic());
+ LOGGER.error("failed to send to topic sink {}", client.getTopic());
throw new TopicSinkClientException("failed to send to topic sink " + client.getTopic());
}
private static ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
private static Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
private static List<ToscaPolicyTypeIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
- private static List<ToscaPolicyIdentifier> toscaPolicyIdents = new ArrayList<>();
- private static List<ToscaPolicy> toscaPolicies = new ArrayList<>();
private static Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
* One time to initialize the applications upon startup.
*/
public static void initializeApplications(Path applicationPath) {
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Initialization applications {}", applicationPath.toAbsolutePath());
+ }
//
// If we have already done this
//
if (! needsInit) {
- LOGGER.error("Already initialized the applications");
- return;
+ LOGGER.warn("Already initialized the applications {}", providerActionMap);
+ //
+ // I had to remove this because the JUnits kept failing - although I probably can
+ // add it back. The main() is hanging around during JUnits and initialization will
+ // fail.
+ //
+ // return
}
//
// Load service
// Iterate through the applications for actions and supported policy types
//
for (XacmlApplicationServiceProvider application : applicationLoader) {
- LOGGER.info("Application {} supports {}", application.applicationName(),
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Application {} supports {}", application.applicationName(),
application.supportedPolicyTypes());
+ }
//
// We are not going to make this available unless the application can
// install correctly.
// we have initialized
//
needsInit = false;
+ LOGGER.info("Finished applications initialization {}", providerActionMap);
+
}
public static XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
for (XacmlApplicationServiceProvider application : applicationLoader) {
try {
if (application.unloadPolicy(policy)) {
- LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Unloaded ToscaPolicy {} from application {}", policy.getMetadata(),
application.applicationName());
+ }
if (mapLoadedPolicies.remove(policy) == null) {
LOGGER.error("Failed to remove unloaded policy {} from map size {}", policy.getMetadata(),
mapLoadedPolicies.size());
//
if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
if (application.loadPolicy(policy)) {
- LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
application.applicationName());
+ }
mapLoadedPolicies.put(policy, application);
}
return;
// they can result in a valid directory being created.
//
Path path = Paths.get(basePath.toAbsolutePath().toString(), application.applicationName());
- LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("initializeApplicationPath {} at this path {}", application.applicationName(), path);
+ }
//
// Create that the directory if it does not exist. Ideally
// this is only for testing, but could be used for production
//
Files.createDirectory(path);
} catch (IOException e) {
- LOGGER.error("Failed to create application directory", e);
+ LOGGER.error("Failed to create application directory {}", path.toAbsolutePath().toString(), e);
}
}
//
return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
.entity(new DecisionProvider().fetchDecision(body)).build();
} catch (DecisionException e) {
+ XacmlPdpStatisticsManager.updateErrorCount();
return addLoggingHeaders(
addVersionControlHeaders(Response.status((e.getErrorResponse().getResponseCode()))), requestId)
.entity(e.getErrorResponse()).build();
@Override
public boolean start() {
try {
+ LOGGER.info("Starting XacmlPdpRestServer...");
//
// Initialize the applications - SEND PROPERTIES
//
}
server.start();
}
+ LOGGER.info("servers are started");
} catch (final Exception exp) {
LOGGER.error("Failed to start xacml pdp http server", exp);
return false;
private static long totalPolicyTypesCount;
private static long totalPoliciesCount;
+ private static long errorsCount;
private static long permitDecisionsCount;
private static long denyDecisionsCount;
private static long indeterminantDecisionsCount;
return ++totalPoliciesCount;
}
+ /**
+ * Method to update the number of error decisions.
+ *
+ * @return the errorDecisionsCount
+ */
+ public static long updateErrorCount() {
+ return ++errorsCount;
+ }
+
/**
* Method to update the number of permit decisions.
*
return totalPoliciesCount;
}
+ /**
+ * Returns the current value of errorDecisionsCount.
+
+ * @return the permitDecisionsCount
+ */
+ public static long getErrorCount() {
+ return errorsCount;
+ }
+
/**
* Returns the current value of permitDecisionsCount.
* Reset all the statistics counts to 0.
*/
public static void resetAllStatistics() {
+ totalPolicyTypesCount = 0L;
totalPoliciesCount = 0L;
+ errorsCount = 0L;
permitDecisionsCount = 0L;
denyDecisionsCount = 0L;
indeterminantDecisionsCount = 0L;
package org.onap.policy.pdpx.main.rest.model;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
/**
* Class to represent statistics report of xacmlPdp service.
*
*/
+@Getter
+@Setter
+@ToString
public class StatisticsReport {
private int code;
private long totalPolicyTypesCount;
private long totalPoliciesCount;
+ private long totalErrorCount;
private long permitDecisionsCount;
private long denyDecisionsCount;
private long indeterminantDecisionsCount;
private long notApplicableDecisionsCount;
-
-
- /**
- * Returns the code of this {@link StatisticsReport} instance.
- *
- * @return the code
- */
- public int getCode() {
- return code;
- }
-
- /**
- * Set code in this {@link StatisticsReport} instance.
- *
- * @param code the code to set
- */
- public void setCode(final int code) {
- this.code = code;
- }
-
- /**
- * Returns the totalPolicyTypesCount of this {@link StatisticsReport} instance.
- *
- * @return the totalPolicyTypesCount
- */
- public long getTotalPolicyTypesCount() {
- return totalPolicyTypesCount;
- }
-
- /**
- * Set totalPolicyTypesCount in this {@link StatisticsReport} instance.
- *
- * @param totalPolicyTypesCount the totalPolicyTypesCount to set
- */
- public void setTotalPolicyTypesCount(long totalPolicyTypesCount) {
- this.totalPolicyTypesCount = totalPolicyTypesCount;
- }
-
- /**
- * Returns the totalPoliciesCount of this {@link StatisticsReport} instance.
- *
- * @return the totalPoliciesCount
- */
- public long getTotalPoliciesCount() {
- return totalPoliciesCount;
- }
-
- /**
- * Set totalPoliciesCount in this {@link StatisticsReport} instance.
- *
- * @param totalPoliciesCount the totalPoliciesCount to set
- */
- public void setTotalPoliciesCount(long totalPoliciesCount) {
- this.totalPoliciesCount = totalPoliciesCount;
- }
-
- /**
- * Returns the permitDecisionsCount of this {@link StatisticsReport} instance.
- *
- * @return the permitDecisionsCount
- */
- public long getPermitDecisionsCount() {
- return permitDecisionsCount;
- }
-
- /**
- * Set permitDecisionsCount in this {@link StatisticsReport} instance.
- *
- * @param permitDecisionsCount the permitDecisionsCount to set
- */
- public void setPermitDecisionsCount(long permitDecisionsCount) {
- this.permitDecisionsCount = permitDecisionsCount;
- }
-
- /**
- * Returns the denyDecisionsCount of this {@link StatisticsReport} instance.
- *
- * @return the denyDecisionsCount
- */
- public long getDenyDecisionsCount() {
- return denyDecisionsCount;
- }
-
- /**
- * Set denyDecisionsCount in this {@link StatisticsReport} instance.
- *
- * @param denyDecisionsCount the denyDecisionsCount to set
- */
- public void setDenyDecisionsCount(long denyDecisionsCount) {
- this.denyDecisionsCount = denyDecisionsCount;
- }
-
- /**
- * Returns the indeterminantDecisionsCount of this {@link StatisticsReport} instance.
- *
- * @return the indeterminantDecisionsCount
- */
- public long getIndeterminantDecisionsCount() {
- return indeterminantDecisionsCount;
- }
-
- /**
- * Set indeterminantDecisionsCount in this {@link StatisticsReport} instance.
- *
- * @param indeterminantDecisionsCount the indeterminantDecisionsCount to set
- */
- public void setIndeterminantDecisionsCount(long indeterminantDecisionsCount) {
- this.indeterminantDecisionsCount = indeterminantDecisionsCount;
- }
-
- /**
- * Returns the notApplicableDecisionsCount of this {@link StatisticsReport} instance.
- *
- * @return the notApplicableDecisionsCount
- */
- public long getNotApplicableDecisionsCount() {
- return notApplicableDecisionsCount;
- }
-
- /**
- * Set notApplicableDecisionsCount in this {@link StatisticsReport} instance.
- *
- * @param notApplicableDecisionsCount the notApplicableDecisionsCount to set
- */
- public void setNotApplicableDecisionsCount(long notApplicableDecisionsCount) {
- this.notApplicableDecisionsCount = notApplicableDecisionsCount;
- }
-
- /**
- * {@inheritDoc}.
- */
- @Override
- public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append("StatisticsReport [code=");
- builder.append(getCode());
- builder.append(", totalPolicyTypesCount=");
- builder.append(getTotalPolicyTypesCount());
- builder.append(", totalPoliciesCount=");
- builder.append(getTotalPoliciesCount());
- builder.append(", permitDecisionsCount=");
- builder.append(getPermitDecisionsCount());
- builder.append(", denyDecisionsCount=");
- builder.append(getDenyDecisionsCount());
- builder.append(", indeterminantDecisionsCount=");
- builder.append(getIndeterminantDecisionsCount());
- builder.append(", notApplicableDecisionsCount=");
- builder.append(getNotApplicableDecisionsCount());
- builder.append("]");
- return builder.toString();
- }
}
package org.onap.policy.pdpx.main.rest.provider;
-import javax.ws.rs.core.Response.Status;
+import com.att.research.xacml.api.Response;
+import com.att.research.xacml.api.Result;
+import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.decisions.concepts.DecisionException;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
+import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//
XacmlApplicationServiceProvider application = findApplication(request);
//
- // Cannot find application for action
+ // Found application for action
//
- return application.makeDecision(request);
+ Pair<DecisionResponse, Response> decision;
+ try {
+ decision = application.makeDecision(request);
+ } catch (Exception e) {
+ LOGGER.error("makeDecision failed", e);
+ throw e;
+ }
+ //
+ // Calculate statistics
+ //
+ this.calculateStatistic(decision.getValue());
+ //
+ // Return the decision
+ //
+ return decision.getKey();
}
private XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
if (application != null) {
return application;
}
- throw new DecisionException(Status.BAD_REQUEST, "No application for action " + request.getAction());
+ throw new DecisionException(javax.ws.rs.core.Response.Status.BAD_REQUEST,
+ "No application for action " + request.getAction());
+ }
+
+ private void calculateStatistic(Response xacmlResponse) {
+
+ for (Result result : xacmlResponse.getResults()) {
+ switch (result.getDecision()) {
+ case PERMIT:
+ XacmlPdpStatisticsManager.updatePermitDecisionsCount();
+ break;
+
+ case DENY:
+ XacmlPdpStatisticsManager.updateDenyDecisionsCount();
+ break;
+
+ case INDETERMINATE:
+ case INDETERMINATE_DENY:
+ case INDETERMINATE_DENYPERMIT:
+ case INDETERMINATE_PERMIT:
+ XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
+ break;
+
+ case NOTAPPLICABLE:
+ XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
+ break;
+
+ default:
+ break;
+
+ }
+ }
}
}
report.setCode(XacmlPdpActivator.getCurrent().isAlive() ? 200 : 500);
report.setTotalPolicyTypesCount(XacmlPdpStatisticsManager.getTotalPolicyTypesCount());
report.setTotalPoliciesCount(XacmlPdpStatisticsManager.getTotalPoliciesCount());
+ report.setTotalErrorCount(XacmlPdpStatisticsManager.getErrorCount());
report.setPermitDecisionsCount(XacmlPdpStatisticsManager.getPermitDecisionsCount());
report.setDenyDecisionsCount(XacmlPdpStatisticsManager.getDenyDecisionsCount());
report.setIndeterminantDecisionsCount(XacmlPdpStatisticsManager.getIndeterminantDecisionsCount());
* @param topicProperties properties used to configure the topics
*/
public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup, Properties topicProperties) {
+ LOGGER.info("Activator initializing using {} and {}", xacmlPdpParameterGroup, topicProperties);
+
TopicEndpoint.manager.addTopicSinks(topicProperties);
TopicEndpoint.manager.addTopicSources(topicProperties);
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============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=========================================================
+ -->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
+
+ <persistence-unit name="OperationsHistoryPU" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+ <class>org.onap.policy.pdp.xacml.application.common.operationshistory.Dbao</class>
+
+ <properties>
+ <property name="javax.persistence.schema-generation.database.action" value="create" />
+ <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
+ <property name="eclipselink.ddl-generation.output-mode" value="database" />
+ <property name="eclipselink.logging.level" value="INFO" />
+ </properties>
+ </persistence-unit>
+
+</persistence>
+
\ No newline at end of file
--- /dev/null
+/*-
+ * ============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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdpx.main;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.common.utils.test.ExceptionsTester;
+
+public class PolicyXacmlPdpExceptionTest {
+
+ @Test
+ public void test() {
+ assertEquals(2, new ExceptionsTester().test(PolicyXacmlPdpException.class));
+ }
+
+}
--- /dev/null
+/*-
+ * ============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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdpx.main;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.onap.policy.common.utils.test.ExceptionsTester;
+
+public class PolicyXacmlPdpRuntimeExceptionTest {
+
+ @Test
+ public void test() {
+ assertEquals(2, new ExceptionsTester().test(PolicyXacmlPdpRuntimeException.class));
+ }
+
+
+}
import java.util.HashMap;
import java.util.Map;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
-import org.glassfish.jersey.client.ClientConfig;
-import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
private static Main main;
+ private static HttpClient client;
@ClassRule
public static final TemporaryFolder appsFolder = new TemporaryFolder();
/**
* BeforeClass setup environment.
* @throws IOException Cannot create temp apps folder
+ * @throws Exception exception if service does not start
*/
@BeforeClass
- public static void beforeClass() throws IOException {
+ public static void beforeClass() throws Exception {
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
//
// Copy test directory over of the application directories
//
- Path src = Paths.get("../packages/policy-xacmlpdp-tarball/src/main/resources/apps");
+ Path src = Paths.get("src/test/resources/apps");
File apps = appsFolder.newFolder("apps");
Files.walk(src).forEach(source -> {
copy(source, apps.toPath().resolve(src.relativize(source)));
// Start the service
//
main = startXacmlPdpService(fileParams);
+ //
+ // Make sure it is running
+ //
+ if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) {
+ throw new IllegalStateException("Cannot connect to port 6969");
+ }
+ //
+ // Create a client
+ //
+ client = getNoAuthHttpClient();
}
@AfterClass
}
@Test
- public void testDecision_Guard() throws InterruptedException, IOException {
+ public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException,
+ ClassNotFoundException {
+
LOGGER.info("Running test testDecision_Guard");
DecisionRequest request = new DecisionRequest();
main.shutdown();
}
- private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException {
- final ClientConfig clientConfig = new ClientConfig();
+ private DecisionResponse getDecision(DecisionRequest request) {
- final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
- clientConfig.register(feature);
-
- final Client client = ClientBuilder.newClient(clientConfig);
- final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision");
+ Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
+ Response response = client.post("", entityRequest, Collections.emptyMap());
- final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
+ assertEquals(200, response.getStatus());
- if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
- throw new IllegalStateException("Cannot connect to port 6969");
- }
-
- return invocationBuilder.post(Entity.json(request), DecisionResponse.class);
+ return HttpClient.getBody(response, DecisionResponse.class);
}
- private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException,
- NoSuchAlgorithmException, ClassNotFoundException {
-
- HttpClient client = getNoAuthHttpClient();
+ private ErrorResponse getErrorDecision(DecisionRequest request) {
Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
Response response = client.post("", entityRequest, Collections.emptyMap());
return HttpClient.getBody(response, ErrorResponse.class);
}
- private HttpClient getNoAuthHttpClient()
+ private static HttpClient getNoAuthHttpClient()
throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
return HttpClient.factory.build(BusTopicParams.builder()
.clientName("testDecisionClient")
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.ClassRule;
+import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.junit.runners.MethodSorters;
import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
import org.onap.policy.common.endpoints.report.HealthCheckReport;
import org.onap.policy.common.utils.network.NetworkUtil;
* Class to perform unit test of {@link XacmlPdpRestServer}.
*
*/
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestXacmlPdpRestServer {
private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
if (main != null) {
stopXacmlPdpService(main);
+ main = null;
}
if (restServer != null) {
restServer.stop();
+ restServer = null;
}
}
} catch (IOException | PolicyXacmlPdpException e) {
}
@Test
- public void testHealthCheckSuccess() throws IOException, InterruptedException, TopicSinkClientException {
+ public void test1HealthCheckSuccess() throws IOException, InterruptedException, TopicSinkClientException {
+ LOGGER.info("***************************** Running test1HealthCheckSuccess *****************************");
main = startXacmlPdpService(true);
final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
+ LOGGER.info("test1HealthCheckSuccess health report {}", report);
validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
}
@Test
- public void testHealthCheckFailure() throws InterruptedException, IOException {
+ public void test7HealthCheckFailure() throws InterruptedException, IOException {
+ LOGGER.info("***************************** Running test7HealthCheckFailure *****************************");
final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath());
restServer.start();
final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
+ LOGGER.info("test7HealthCheckFailure health report {}", report);
validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
assertTrue(restServer.isAlive());
assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
}
@Test
- public void testHttpsHealthCheckSuccess() throws Exception {
+ public void test2HttpsHealthCheckSuccess() throws Exception {
+ LOGGER.info("**************************** Running test2HttpsHealthCheckSuccess ****************************");
main = startXacmlPdpService(false);
final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
+ LOGGER.info("test2HttpsHealthCheckSuccess health report {}", report);
validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
}
@Test
- public void testStatistics_200() throws IOException, InterruptedException, TopicSinkClientException {
+ public void test4Statistics_200() throws IOException, InterruptedException, TopicSinkClientException {
+ LOGGER.info("***************************** Running test4Statistics_200 *****************************");
+ XacmlPdpStatisticsManager.resetAllStatistics();
main = startXacmlPdpService(true);
Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
+ LOGGER.info("test4Statistics_200 health report {}", report);
validateStatisticsReport(report, 0, 200);
updateXacmlPdpStatistics();
invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
report = invocationBuilder.get(StatisticsReport.class);
+ LOGGER.info("test4Statistics_200 health report {}", report);
validateStatisticsReport(report, 1, 200);
XacmlPdpStatisticsManager.resetAllStatistics();
}
@Test
- public void testStatistics_500() throws IOException, InterruptedException {
+ public void test5Statistics_500() throws IOException, InterruptedException {
+ LOGGER.info("***************************** Running test5Statistics_500 *****************************");
final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath());
restServer.start();
final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
+ LOGGER.info("test5Statistics_500 health report {}", report);
validateStatisticsReport(report, 0, 500);
XacmlPdpStatisticsManager.resetAllStatistics();
}
@Test
- public void testHttpsStatistic() throws Exception {
+ public void test6HttpsStatistic() throws Exception {
+ LOGGER.info("***************************** Running test6HttpsStatistic *****************************");
main = startXacmlPdpService(false);
final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
+ LOGGER.info("test6HttpsStatistic health report {}", report);
validateStatisticsReport(report, 0, 200);
}
@Test
- public void testStatisticsConstructorIsPrivate() {
+ public void test3StatisticsConstructorIsPrivate() {
+ LOGGER.info("************************* Running test3StatisticsConstructorIsPrivate *************************");
try {
final Constructor<XacmlPdpStatisticsManager> constructor =
XacmlPdpStatisticsManager.class.getDeclaredConstructor();
final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
- if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
- throw new IllegalStateException("cannot connect to port 6969");
+ if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) {
+ throw new IllegalStateException("Cannot connect to port 6969");
}
+
return invocationBuilder;
}
@Test
public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
try {
+ LOGGER.info("*************************** Running testXacmlPdpStatistics_200 ***************************");
final Main main = startXacmlPdpService();
StatisticsReport report = getXacmlPdpStatistics();
validateReport(report, 0, 200);
@Test
public void testXacmlPdpStatistics_500() throws InterruptedException {
+ LOGGER.info("***************************** Running testXacmlPdpStatistics_500 *****************************");
final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams,
final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
- if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
+ if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) {
throw new IllegalStateException("Cannot connect to port 6969");
}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============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=========================================================
+ -->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
+
+ <persistence-unit name="PipEngineTest" transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+ <class>org.onap.policy.pdp.xacml.application.common.operationshistory.Dbao</class>
+
+ <properties>
+ <property name="eclipselink.ddl-generation" value="create-tables" />
+ <property name="eclipselink.logging.level" value="FINE" />
+ <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
+ <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
+ <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
+ </properties>
+ </persistence-unit>
+
+</persistence>
--- /dev/null
+#
+# Properties that the embedded PDP engine uses to configure and load
+#
+# Standard API Factories
+#
+xacml.dataTypeFactory=com.att.research.xacml.std.StdDataTypeFactory
+xacml.pdpEngineFactory=com.att.research.xacmlatt.pdp.ATTPDPEngineFactory
+xacml.pepEngineFactory=com.att.research.xacml.std.pep.StdEngineFactory
+xacml.pipFinderFactory=com.att.research.xacml.std.pip.StdPIPFinderFactory
+xacml.traceEngineFactory=com.att.research.xacml.std.trace.LoggingTraceEngineFactory
+#
+# AT&T PDP Implementation Factories
+#
+xacml.att.evaluationContextFactory=com.att.research.xacmlatt.pdp.std.StdEvaluationContextFactory
+xacml.att.combiningAlgorithmFactory=com.att.research.xacmlatt.pdp.std.StdCombiningAlgorithmFactory
+xacml.att.functionDefinitionFactory=com.att.research.xacmlatt.pdp.std.StdFunctionDefinitionFactory
+#
+# ONAP PDP Implementation Factories
+#
+xacml.att.policyFinderFactory=org.onap.policy.pdp.xacml.application.common.OnapPolicyFinderFactory
+
+#
+# Use a root combining algorithm
+#
+xacml.att.policyFinderFactory.combineRootPolicies=urn:oasis:names:tc:xacml:3.0:policy-combining-algorithm:deny-overrides
+
+#
+# PIP Engine Definitions
+#
+count-recent-operations.classname=org.onap.policy.pdp.xacml.application.common.operationshistory.CountRecentOperationsPip
+count-recent-operations.issuer=urn:org:onap:xacml:guard:count-recent-operations
+count-recent-operations.name=CountRecentOperations
+count-recent-operations.description=Returns operation counts based on time window
+count-recent-operations.persistenceunit=OperationsHistoryPUTest
+
+get-operation-outcome.classname=org.onap.policy.pdp.xacml.application.common.operationshistory.GetOperationOutcomePip
+get-operation-outcome.issuer=urn:org:onap:xacml:guard:get-operation-outcome
+get-operation-outcome.name=GetOperationOutcome
+get-operation-outcome.description=Returns operation outcome
+get-operation-outcome.persistenceunit=OperationsHistoryPUTest
+
+#
+# Make pips available to finder
+#
+xacml.pip.engines=count-recent-operations,get-operation-outcome
+
+#
+# JPA Properties
+#
+javax.persistence.jdbc.driver=org.h2.Driver
+javax.persistence.jdbc.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE
+javax.persistence.jdbc.user=policy
+javax.persistence.jdbc.password=P01icY
--- /dev/null
+#
+# Properties that the embedded PDP engine uses to configure and load
+#
+# Standard API Factories
+#
+xacml.dataTypeFactory=com.att.research.xacml.std.StdDataTypeFactory
+xacml.pdpEngineFactory=com.att.research.xacmlatt.pdp.ATTPDPEngineFactory
+xacml.pepEngineFactory=com.att.research.xacml.std.pep.StdEngineFactory
+xacml.pipFinderFactory=com.att.research.xacml.std.pip.StdPIPFinderFactory
+xacml.traceEngineFactory=com.att.research.xacml.std.trace.LoggingTraceEngineFactory
+#
+# AT&T PDP Implementation Factories
+#
+xacml.att.evaluationContextFactory=com.att.research.xacmlatt.pdp.std.StdEvaluationContextFactory
+xacml.att.combiningAlgorithmFactory=com.att.research.xacmlatt.pdp.std.StdCombiningAlgorithmFactory
+xacml.att.functionDefinitionFactory=com.att.research.xacmlatt.pdp.std.StdFunctionDefinitionFactory
+#
+# ONAP PDP Implementation Factories
+#
+xacml.att.policyFinderFactory=org.onap.policy.pdp.xacml.application.common.OnapPolicyFinderFactory
+
+#
+# Use a root combining algorithm
+#
+xacml.att.policyFinderFactory.combineRootPolicies=urn:com:att:xacml:3.0:policy-combining-algorithm:combined-permit-overrides
+
+#
+# Policies to load
+#
+xacml.rootPolicies=
+xacml.referencedPolicies=
\ No newline at end of file
--- /dev/null
+#
+# Properties that the embedded PDP engine uses to configure and load
+#
+# Standard API Factories
+#
+xacml.dataTypeFactory=com.att.research.xacml.std.StdDataTypeFactory
+xacml.pdpEngineFactory=com.att.research.xacmlatt.pdp.ATTPDPEngineFactory
+xacml.pepEngineFactory=com.att.research.xacml.std.pep.StdEngineFactory
+xacml.pipFinderFactory=com.att.research.xacml.std.pip.StdPIPFinderFactory
+xacml.traceEngineFactory=com.att.research.xacml.std.trace.LoggingTraceEngineFactory
+#
+# AT&T PDP Implementation Factories
+#
+xacml.att.evaluationContextFactory=com.att.research.xacmlatt.pdp.std.StdEvaluationContextFactory
+xacml.att.combiningAlgorithmFactory=com.att.research.xacmlatt.pdp.std.StdCombiningAlgorithmFactory
+xacml.att.functionDefinitionFactory=com.att.research.xacmlatt.pdp.std.StdFunctionDefinitionFactory
+#
+# ONAP PDP Implementation Factories
+#
+xacml.att.policyFinderFactory=org.onap.policy.pdp.xacml.application.common.OnapPolicyFinderFactory
+
+#
+# Use a root combining algorithm
+#
+xacml.att.policyFinderFactory.combineRootPolicies=urn:com:att:xacml:3.0:policy-combining-algorithm:combined-permit-overrides
+
+#
+# Policies to load
+#
+xacml.rootPolicies=
+xacml.referencedPolicies=
\ No newline at end of file
"userName": "healthcheck",
"password": "zb!XztG34"
},
- "applicationPath": "apps.test"
+ "applicationPath": "src/test/resources/apps"
}
"password":"zb!XztG34",
"https":true
},
- "applicationPath": "apps.test"
+ "applicationPath": "src/test/resources/apps"
}
\ No newline at end of file
#
# Use a root combining algorithm
#
-xacml.att.policyFinderFactory.combineRootPolicies=urn:oasis:names:tc:xacml:3.0:policy-combining-algorithm:permit-unless-deny
-
-xacml.pip.engines=historydb
+xacml.att.policyFinderFactory.combineRootPolicies=urn:oasis:names:tc:xacml:3.0:policy-combining-algorithm:deny-overrides
#
-# PIP Engine Definition
+# PIP Engine Definitions
#
-historydb.classname=org.onap.policy.pdp.xacml.application.common.OnapOperationsHistoryPipEngine
-historydb.issuer=urn:org:onap:xacml:guard:historydb
-historydb.name=operationHistoryDB
-historydb.description=Returns operation counts based on time window
+count-recent-operations.classname=org.onap.policy.pdp.xacml.application.common.operationshistory.CountRecentOperationsPip
+count-recent-operations.issuer=urn:org:onap:xacml:guard:count-recent-operations
+count-recent-operations.name=CountRecentOperations
+count-recent-operations.description=Returns operation counts based on time window
+count-recent-operations.persistenceunit=OperationsHistoryPU
+
+get-operation-outcome.classname=org.onap.policy.pdp.xacml.application.common.operationshistory.GetOperationOutcomePip
+get-operation-outcome.issuer=urn:org:onap:xacml:guard:get-operation-outcome
+get-operation-outcome.name=GetOperationOutcome
+get-operation-outcome.description=Returns operation outcome
+get-operation-outcome.persistenceunit=OperationsHistoryPU
#
-# Database persistence for PIP
+# Make pips available to finder
#
-historydb.persistenceunit=OperationsHistoryPU
+xacml.pip.engines=count-recent-operations,get-operation-outcome
-# Policies to load
#
-#xacml.rootPolicies=guard
-#guard.file=src/main/resources/RootGuardPolicy.xml
-
+# JPA Properties
+#
+javax.persistence.jdbc.driver=org.mariadb.jdbc.Driver
+javax.persistence.jdbc.url=jdbc:mariadb://mariadb:3306/operationshistory
+javax.persistence.jdbc.user=policy_user
+javax.persistence.jdbc.password=cG9saWN5X3VzZXI=