diff options
Diffstat (limited to 'applications/common')
7 files changed, 73 insertions, 71 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java index 0d2b1ddd..d00df3b7 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPip.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +34,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Collection; +import java.util.Objects; import org.onap.policy.pdp.xacml.application.common.ToscaDictionary; import org.onap.policy.pdp.xacml.application.common.std.StdOnapPip; import org.slf4j.Logger; @@ -41,7 +43,7 @@ import org.slf4j.LoggerFactory; public class CountRecentOperationsPip extends StdOnapPip { public static final String ISSUER_NAME = "count-recent-operations"; - private static Logger logger = LoggerFactory.getLogger(CountRecentOperationsPip.class); + private static final Logger logger = LoggerFactory.getLogger(CountRecentOperationsPip.class); public CountRecentOperationsPip() { super(); @@ -57,7 +59,7 @@ public class CountRecentOperationsPip extends StdOnapPip { * getAttributes. * * @param pipRequest the request - * @param pipFinder the pip finder + * @param pipFinder the pip finder * @return PIPResponse */ @Override @@ -66,21 +68,21 @@ public class CountRecentOperationsPip extends StdOnapPip { throw new PIPException("Engine is shutdown"); } logger.debug("getAttributes requesting attribute {} of type {} for issuer {}", - pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer()); + pipRequest.getAttributeId(), pipRequest.getDataTypeId(), pipRequest.getIssuer()); // // Determine if the issuer is correct // if (Strings.isNullOrEmpty(pipRequest.getIssuer())) { logger.debug("issuer is null - returning empty response"); // - // We only respond to ourself as the issuer + // We only respond to ourselves as the issuer // return StdPIPResponse.PIP_RESPONSE_EMPTY; } - if (! pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) { + if (!pipRequest.getIssuer().startsWith(ToscaDictionary.GUARD_ISSUER_PREFIX)) { logger.debug("Issuer does not start with guard"); // - // We only respond to ourself as the issuer + // We only respond to ourselves as the issuer // return StdPIPResponse.PIP_RESPONSE_EMPTY; } @@ -100,7 +102,7 @@ public class CountRecentOperationsPip extends StdOnapPip { String target = getAttribute(pipFinder, PIP_REQUEST_TARGET); String timeWindow = timeWindowVal + " " + timeWindowScale; logger.info("Going to query DB about: actor {} operation {} target {} time window {}", - actor, operation, target, timeWindow); + actor, operation, target, timeWindow); // // Sanity check // @@ -120,17 +122,17 @@ public class CountRecentOperationsPip extends StdOnapPip { // var pipResponse = new StdMutablePIPResponse(); this.addLongAttribute(pipResponse, - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONCOUNT, - operationCount, - pipRequest); + XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONCOUNT, + operationCount, + pipRequest); return new StdPIPResponse(pipResponse); } private long doDatabaseQuery(String actor, String operation, String target, int timeWindowVal, - String timeWindowScale) { + String timeWindowScale) { logger.info("Querying operations history for {} {} {} {} {}", - actor, operation, target, timeWindowVal, timeWindowScale); + actor, operation, target, timeWindowVal, timeWindowScale); // // Only can query if we have an EntityManager // @@ -146,19 +148,18 @@ public class CountRecentOperationsPip extends StdOnapPip { // We are expecting a single result // return em.createQuery("select count(e) from OperationsHistory e" - + " where e.outcome<>'Failure_Guard'" - + " and e.actor= ?1" - + " and e.operation= ?2" - + " and e.target= ?3" - + " and e.endtime between" - + " ?4 and CURRENT_TIMESTAMP", - Long.class) + + " where e.outcome<>'Failure_Guard'" + + " and e.actor= ?1" + + " and e.operation= ?2" + + " and e.target= ?3" + + " and e.endtime between" + + " ?4 and CURRENT_TIMESTAMP", + Long.class) .setParameter(1, actor) .setParameter(2, operation) .setParameter(3, target) .setParameter(4, Timestamp.from(Instant.now() - .minus(timeWindowVal, - stringToChronoUnit(timeWindowScale)))) + .minus(timeWindowVal, Objects.requireNonNull(stringToChronoUnit(timeWindowScale))))) .getSingleResult(); } catch (Exception e) { logger.error("Typed query failed ", e); diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java index e0705f80..9dff0599 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdOnapPip.java @@ -37,6 +37,7 @@ import com.att.research.xacml.std.pip.StdMutablePIPResponse; import com.att.research.xacml.std.pip.StdPIPRequest; import com.att.research.xacml.std.pip.engines.StdConfigurableEngine; import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; import java.math.BigInteger; import java.util.Collection; @@ -53,22 +54,23 @@ import org.slf4j.LoggerFactory; public abstract class StdOnapPip extends StdConfigurableEngine { protected static Logger logger = LoggerFactory.getLogger(StdOnapPip.class); - protected static final PIPRequest PIP_REQUEST_ACTOR = new StdPIPRequest( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - ToscaDictionary.ID_RESOURCE_GUARD_ACTOR, - XACML3.ID_DATATYPE_STRING); + protected static final PIPRequest PIP_REQUEST_ACTOR = new StdPIPRequest( + XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + ToscaDictionary.ID_RESOURCE_GUARD_ACTOR, + XACML3.ID_DATATYPE_STRING); - protected static final PIPRequest PIP_REQUEST_RECIPE = new StdPIPRequest( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - ToscaDictionary.ID_RESOURCE_GUARD_RECIPE, - XACML3.ID_DATATYPE_STRING); + protected static final PIPRequest PIP_REQUEST_RECIPE = new StdPIPRequest( + XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + ToscaDictionary.ID_RESOURCE_GUARD_RECIPE, + XACML3.ID_DATATYPE_STRING); - protected static final PIPRequest PIP_REQUEST_TARGET = new StdPIPRequest( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - ToscaDictionary.ID_RESOURCE_GUARD_TARGETID, - XACML3.ID_DATATYPE_STRING); + protected static final PIPRequest PIP_REQUEST_TARGET = new StdPIPRequest( + XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + ToscaDictionary.ID_RESOURCE_GUARD_TARGETID, + XACML3.ID_DATATYPE_STRING); protected Properties properties; + protected EntityManagerFactory emf; protected EntityManager em; protected String issuer; protected boolean shutdown = false; @@ -104,9 +106,9 @@ public abstract class StdOnapPip extends StdConfigurableEngine { // // Create the entity manager factory // - em = Persistence.createEntityManagerFactory( - properties.getProperty(this.issuer + ".persistenceunit"), - emProperties).createEntityManager(); + emf = Persistence.createEntityManagerFactory( + properties.getProperty(this.issuer + ".persistenceunit"), emProperties); + em = emf.createEntityManager(); } catch (Exception e) { logger.error("Persistence failed {} operations history db", e.getLocalizedMessage(), e); } @@ -142,7 +144,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { pipResponse = pipFinder.getMatchingAttributes(pipRequest, this); if (pipResponse.getStatus() != null && !pipResponse.getStatus().isOk()) { logger.info("get attribute error retrieving {}: {}", pipRequest.getAttributeId(), - pipResponse.getStatus()); + pipResponse.getStatus()); pipResponse = null; } if (pipResponse != null && pipResponse.getAttributes().isEmpty()) { @@ -156,10 +158,10 @@ public abstract class StdOnapPip extends StdConfigurableEngine { } protected String findFirstAttributeValue(PIPResponse pipResponse) { - for (Attribute attribute: pipResponse.getAttributes()) { - Iterator<AttributeValue<String>> iterAttributeValues = attribute.findValues(DataTypes.DT_STRING); + for (Attribute attribute : pipResponse.getAttributes()) { + Iterator<AttributeValue<String>> iterAttributeValues = attribute.findValues(DataTypes.DT_STRING); while (iterAttributeValues.hasNext()) { - String value = iterAttributeValues.next().getValue(); + String value = iterAttributeValues.next().getValue(); if (value != null) { return value; } @@ -169,35 +171,35 @@ public abstract class StdOnapPip extends StdConfigurableEngine { } protected void addIntegerAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, - Identifier attributeId, int value, PIPRequest pipRequest) { - AttributeValue<BigInteger> attributeValue = null; + Identifier attributeId, int value, PIPRequest pipRequest) { + AttributeValue<BigInteger> attributeValue = null; try { - attributeValue = makeInteger(value); + attributeValue = makeInteger(value); } catch (Exception e) { logger.error("Failed to convert {} to integer", value, e); } if (attributeValue != null) { stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, - pipRequest.getIssuer(), false)); + pipRequest.getIssuer(), false)); } } protected void addLongAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, - Identifier attributeId, long value, PIPRequest pipRequest) { - AttributeValue<BigInteger> attributeValue = null; + Identifier attributeId, long value, PIPRequest pipRequest) { + AttributeValue<BigInteger> attributeValue = null; try { - attributeValue = makeLong(value); + attributeValue = makeLong(value); } catch (Exception e) { logger.error("Failed to convert {} to long", value, e); } if (attributeValue != null) { stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, - pipRequest.getIssuer(), false)); + pipRequest.getIssuer(), false)); } } protected void addStringAttribute(StdMutablePIPResponse stdPipResponse, Identifier category, Identifier attributeId, - String value, PIPRequest pipRequest) { + String value, PIPRequest pipRequest) { AttributeValue<String> attributeValue = null; try { attributeValue = makeString(value); @@ -206,7 +208,7 @@ public abstract class StdOnapPip extends StdConfigurableEngine { } if (attributeValue != null) { stdPipResponse.addAttribute(new StdMutableAttribute(category, attributeId, attributeValue, - pipRequest.getIssuer(), false)); + pipRequest.getIssuer(), false)); } } diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java index 0231c058..bd42933c 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021,2023 Nordix Foundation. + * Modifications Copyright (C) 2021, 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -102,7 +102,6 @@ public class PolicyApiCallerTest { ApiRestController.class.getName()); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false"); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, "false"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, GsonMessageBodyHandler.class.getName()); diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java index e564cd96..c38ab716 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/operationshistory/CountRecentOperationsPipTest.java @@ -26,13 +26,13 @@ import static org.mockito.Mockito.when; import com.att.research.xacml.api.Attribute; import com.att.research.xacml.api.AttributeValue; -import com.att.research.xacml.api.Status; import com.att.research.xacml.api.pip.PIPException; import com.att.research.xacml.api.pip.PIPFinder; import com.att.research.xacml.api.pip.PIPRequest; import com.att.research.xacml.api.pip.PIPResponse; import com.att.research.xacml.std.pip.StdPIPResponse; import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; import jakarta.persistence.Query; import java.io.FileInputStream; @@ -44,6 +44,7 @@ import java.util.LinkedList; import java.util.Properties; import java.util.Queue; import java.util.UUID; +import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -64,6 +65,7 @@ public class CountRecentOperationsPipTest { private static final String TARGET = "my-target"; private static final String TEST_PROPERTIES = "src/test/resources/test.properties"; + private static EntityManagerFactory emf; private static EntityManager em; @Mock @@ -81,9 +83,6 @@ public class CountRecentOperationsPipTest { @Mock private PIPResponse resp3; - @Mock - private Status okStatus; - private Properties properties; private Queue<PIPResponse> responses; private Queue<String> attributes; @@ -110,11 +109,12 @@ public class CountRecentOperationsPipTest { // String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit"; LOGGER.info("persistenceunit {}", persistenceUnit); - em = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2).createEntityManager(); + emf = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2); + em = emf.createEntityManager(); // // // - LOGGER.info("Configured own entity manager", em.toString()); + LOGGER.info("Configured own entity manager {}", em.toString()); } /** @@ -153,7 +153,7 @@ public class CountRecentOperationsPipTest { } @Test - public void testConfigure_DbException() throws Exception { + public void testConfigure_DbException() { properties.put("jakarta.persistence.jdbc.url", "invalid"); assertThatCode(() -> pipEngine.configure("issuer", properties) @@ -208,7 +208,7 @@ public class CountRecentOperationsPipTest { // // create entry // - OperationsHistory newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS"); + OperationsHistory newEntry = createEntry(); // // No entries yet // @@ -231,9 +231,9 @@ public class CountRecentOperationsPipTest { } @Test - public void testStringToChronoUnit() throws PIPException { + public void testStringToChronosUnit() throws PIPException { // not configured yet - OperationsHistory newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS"); + OperationsHistory newEntry = createEntry(); assertEquals(-1, getCount(newEntry)); // now configure it @@ -271,14 +271,14 @@ public class CountRecentOperationsPipTest { return ((Number) value.getValue()).longValue(); } - private OperationsHistory createEntry(String cl, String target, String outcome) { + private OperationsHistory createEntry() { // // Create entry // OperationsHistory newEntry = new OperationsHistory(); - newEntry.setClosedLoopName(cl); - newEntry.setTarget(target); - newEntry.setOutcome(outcome); + newEntry.setClosedLoopName("cl-foobar-1"); + newEntry.setTarget("vnf-1"); + newEntry.setOutcome("SUCCESS"); newEntry.setActor("Controller"); newEntry.setOperation("operationA"); newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000))); diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java index 2edaf0e5..95880efe 100644 --- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java +++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java @@ -128,7 +128,6 @@ public class StdMatchableTranslatorTest { ApiRestController.class.getName()); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false"); - props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, "false"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, GsonMessageBodyHandler.class.getName()); diff --git a/applications/common/src/test/resources/META-INF/persistence.xml b/applications/common/src/test/resources/META-INF/persistence.xml index 87896a60..53597b91 100644 --- a/applications/common/src/test/resources/META-INF/persistence.xml +++ b/applications/common/src/test/resources/META-INF/persistence.xml @@ -20,16 +20,18 @@ ============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 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.hibernate.jpa.HibernatePersistenceProvider</provider> <class>org.onap.policy.guard.OperationsHistory</class> <properties> <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="jakarta.persistence.schema-generation.create-source" value="metadata"/> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> - <property name="hibernate.show_sql" value="false" /> + <property name="hibernate.show_sql" value="false"/> </properties> </persistence-unit> diff --git a/applications/common/src/test/resources/test.properties b/applications/common/src/test/resources/test.properties index 77939c35..58aa5b16 100644 --- a/applications/common/src/test/resources/test.properties +++ b/applications/common/src/test/resources/test.properties @@ -40,7 +40,6 @@ get-operation-outcome.persistenceunit=PipEngineTest # # JPA Properties # -eclipselink.target-database=Auto jakarta.persistence.jdbc.driver=org.h2.Driver jakarta.persistence.jdbc.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=FALSE jakarta.persistence.jdbc.user=policy |