2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.guard;
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.util.XACMLPolicyScanner;
29 import java.io.ByteArrayInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.util.List;
37 import java.util.UUID;
38 import org.apache.commons.io.IOUtils;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardYamlCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.decisions.concepts.DecisionRequest;
43 import org.onap.policy.models.decisions.concepts.DecisionResponse;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
46 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CoordinationGuardTranslator.class);
54 public CoordinationGuardTranslator() {
59 public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
60 LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
62 // Policy name should be at the root
64 String type = toscaPolicy.getType();
65 String coordinationFunctionPath = "coordination/function";
66 Map<String, Object> policyProps = toscaPolicy.getProperties();
67 LOGGER.debug("path = {}", coordinationFunctionPath);
68 LOGGER.debug("props = {}", policyProps);
69 @SuppressWarnings("unchecked")
70 List<String> controlLoop = (List<String>) policyProps.get("controlLoop");
71 CoordinationDirective cd = new CoordinationDirective();
72 cd.setCoordinationFunction(type);
73 cd.setControlLoop(controlLoop);
74 LOGGER.debug("CoordinationDirective = {}", cd);
76 // Generate the xacml policy as a string
78 String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
79 LOGGER.debug("xacmlStr\n{}", xacmlStr);
81 // Scan the string and convert to PoilcyType
83 try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
84 return XACMLPolicyScanner.readPolicy(is);
85 } catch (IOException e) {
86 throw new ToscaPolicyConversionException("Failed to read policy", e);
91 * This function is not used for CLC instead
92 * the one in LegacyGuardTranslator is used.
95 public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
96 throw new ToscaPolicyConversionException("this convertRequest shouldn't be used");
100 * This function is not used for CLC instead
101 * the one in LegacyGuardTranslator is used.
104 public DecisionResponse convertResponse(Response xacmlResponse) {
105 LOGGER.info("this convertResponse shouldn't be used");
110 * Load YAML coordination directive.
112 * @param directiveFilename yaml directive file to load
113 * @return the CoordinationDirective
115 public static CoordinationDirective loadCoordinationDirectiveFromFile(
116 String directiveFilename) {
117 try (InputStream is = new FileInputStream(new File(directiveFilename))) {
118 String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
120 // Read the yaml into our Java Object
122 CoordinationDirective obj =
123 new StandardYamlCoder().decode(contents, CoordinationDirective.class);
124 LOGGER.debug(contents);
126 } catch (IOException | CoderException e) {
127 LOGGER.error("Error while loading YAML coordination directive", e);
133 * Generate Xacml rule implementing specified CoordinationDirective.
135 * @param cd the CoordinationDirective
136 * @param protoDir the directory containing Xacml implementation prototypes
137 * @return the generated Xacml policy
139 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
140 String protoDir) throws ToscaPolicyConversionException {
142 * Determine file names
144 String xacmlProtoFilename =
145 protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
146 LOGGER.debug("xacmlProtoFilename={}", xacmlProtoFilename);
148 * Values to be used for placeholders
150 final String uniqueId = UUID.randomUUID().toString();
151 final String cLOne = cd.getControlLoop(0);
152 final String cLTwo = cd.getControlLoop(1);
154 * Replace function placeholders with appropriate values
156 String policyXml = ResourceUtils.getResourceAsString(xacmlProtoFilename);
157 if (policyXml == null) {
158 throw new ToscaPolicyConversionException("Error while generating XACML policy for coordination directive");
160 policyXml = policyXml.replace("UNIQUE_ID", uniqueId);
161 policyXml = policyXml.replace("CONTROL_LOOP_ONE", cLOne);
162 policyXml = policyXml.replace("CONTROL_LOOP_TWO", cLTwo);