summaryrefslogtreecommitdiffstats
path: root/applications/native/src/main/java/org
diff options
context:
space:
mode:
authorChenfei Gao <cgao@research.att.com>2020-02-28 14:45:48 -0500
committerChenfei Gao <cgao@research.att.com>2020-02-28 14:45:55 -0500
commitbdef9f5dfd8c95eff86e710ef94c1fab4e652854 (patch)
treebcc7327976e890e3fec352a4dbf8d1f150e98bda /applications/native/src/main/java/org
parentfcd767926048397607d9e0f0288f2a0982f6bbcc (diff)
Build XACML PDP support for native xacml policy type
Added a new native application to the service loader framework Added a new translator for the native application Added custom serialization providers for xacml+json and xacml+xml Added a new endpoint for native xacml decision api Added a new api provider function to handle the native xacml api calls Added corresponding junit tests Issue-ID: POLICY-2182 Change-Id: I30fa4637612c324d543f9952386cf1a27a52d76c Signed-off-by: Chenfei Gao <cgao@research.att.com>
Diffstat (limited to 'applications/native/src/main/java/org')
-rw-r--r--applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplication.java78
-rw-r--r--applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java112
2 files changed, 190 insertions, 0 deletions
diff --git a/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplication.java b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplication.java
new file mode 100644
index 00000000..0d862d11
--- /dev/null
+++ b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplication.java
@@ -0,0 +1,78 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.xacml.pdp.application.nativ;
+
+import com.att.research.xacml.api.Request;
+import com.att.research.xacml.api.Response;
+import java.util.Arrays;
+import java.util.List;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
+import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
+
+/**
+ * This class implements an application that handles onap.policies.native.Xacml policies.
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ *
+ */
+public class NativePdpApplication extends StdXacmlApplicationServiceProvider {
+
+ private static final ToscaPolicyTypeIdentifier supportedPolicyType = new ToscaPolicyTypeIdentifier(
+ "onap.policies.native.Xacml", "1.0.0");
+ private NativePdpApplicationTranslator translator = new NativePdpApplicationTranslator();
+
+ @Override
+ public String applicationName() {
+ return "native";
+ }
+
+ @Override
+ public List<String> actionDecisionsSupported() {
+ return Arrays.asList("native");
+ }
+
+ @Override
+ public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
+ return Arrays.asList(supportedPolicyType);
+ }
+
+ @Override
+ public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
+ return supportedPolicyType.equals(policyTypeId);
+ }
+
+ @Override
+ protected ToscaPolicyTranslator getTranslator(String type) {
+ return translator;
+ }
+
+ /**
+ * Makes decision for the incoming native xacml request.
+ * @param request the native xacml request
+ * @return the native xacml response
+ */
+ public Response makeNativeDecision(Request request) {
+ return this.xacmlDecision(request);
+ }
+}
diff --git a/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java
new file mode 100644
index 00000000..98a1c657
--- /dev/null
+++ b/applications/native/src/main/java/org/onap/policy/xacml/pdp/application/nativ/NativePdpApplicationTranslator.java
@@ -0,0 +1,112 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.xacml.pdp.application.nativ;
+
+import com.att.research.xacml.api.Request;
+import com.att.research.xacml.api.Response;
+import com.att.research.xacml.util.XACMLPolicyScanner;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.Map;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+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;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class implements one translator that interprets TOSCA policy and decision API request/response payload.
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ *
+ */
+public class NativePdpApplicationTranslator implements ToscaPolicyTranslator {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTranslator.class);
+ private static final String POLICY = "policy";
+
+ public NativePdpApplicationTranslator() {
+ super();
+ }
+
+ @Override
+ public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ //
+ // Extract the Base64 encoded policy xml string and decode it
+ //
+ String encodedXacmlPolicy = getNativeXacmlPolicy(toscaPolicy);
+ String decodedXacmlPolicy;
+ try {
+ decodedXacmlPolicy = new String(Base64.getDecoder().decode(encodedXacmlPolicy), StandardCharsets.UTF_8);
+ } catch (IllegalArgumentException exc) {
+ throw new ToscaPolicyConversionException("error on Base64 decoding the native policy", exc);
+ }
+ LOGGER.debug("Decoded xacml policy {}",decodedXacmlPolicy);
+ //
+ // Scan the string and convert to xacml PolicyType
+ //
+ try (InputStream is = new ByteArrayInputStream(decodedXacmlPolicy.getBytes(StandardCharsets.UTF_8))) {
+ //
+ // Here we assume it is PolicyType, not PolicySetType
+ // PolicySetType will be addressed later
+ //
+ return (PolicyType) XACMLPolicyScanner.readPolicy(is);
+ } catch (IOException exc) {
+ throw new ToscaPolicyConversionException("Failed to read policy", exc);
+ }
+ }
+
+ private String getNativeXacmlPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+
+ Map<String, Object> propertyMap = toscaPolicy.getProperties();
+ if (propertyMap.isEmpty() || !propertyMap.containsKey(POLICY)) {
+ throw new ToscaPolicyConversionException("no xacml native policy found in the tosca policy");
+ }
+
+ String nativePolicyString = propertyMap.get(POLICY).toString();
+ LOGGER.debug("Base64 encoded native xacml policy {}", nativePolicyString);
+ return nativePolicyString;
+ }
+
+ @Override
+ public Request convertRequest(DecisionRequest request) {
+ //
+ // We do nothing to DecisionRequest for native xacml application
+ //
+ return null;
+ }
+
+ @Override
+ public DecisionResponse convertResponse(Response xacmlResponse) {
+ //
+ // We do nothing to DecisionResponse for native xacml application
+ //
+ return null;
+ }
+} \ No newline at end of file