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;
28 import java.io.ByteArrayInputStream;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34 import java.util.List;
36 import java.util.UUID;
37 import org.apache.commons.io.IOUtils;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardYamlCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.decisions.concepts.DecisionRequest;
42 import org.onap.policy.models.decisions.concepts.DecisionResponse;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
45 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
51 private static final Logger LOGGER = LoggerFactory.getLogger(CoordinationGuardTranslator.class);
53 public CoordinationGuardTranslator() {
58 public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
59 LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
61 // Policy name should be at the root
63 String type = toscaPolicy.getType();
64 String coordinationFunctionPath = "coordination/function";
65 Map<String, Object> policyProps = toscaPolicy.getProperties();
66 LOGGER.debug("path = {}", coordinationFunctionPath);
67 LOGGER.debug("props = {}", policyProps);
68 @SuppressWarnings("unchecked")
69 List<String> controlLoop = (List<String>) policyProps.get("controlLoop");
70 CoordinationDirective cd = new CoordinationDirective();
71 cd.setCoordinationFunction(type);
72 cd.setControlLoop(controlLoop);
73 LOGGER.debug("CoordinationDirective = {}", cd);
75 // Generate the xacml policy as a string
77 String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
78 LOGGER.debug("xacmlStr\n{}", xacmlStr);
80 // Scan the string and convert to PoilcyType
82 try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
83 return XACMLPolicyScanner.readPolicy(is);
84 } catch (IOException e) {
85 throw new ToscaPolicyConversionException("Failed to read policy", e);
90 * This function is not used for CLC instead
91 * the one in LegacyGuardTranslator is used.
94 public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
95 throw new ToscaPolicyConversionException("this convertRequest shouldn't be used");
99 * This function is not used for CLC instead
100 * the one in LegacyGuardTranslator is used.
103 public DecisionResponse convertResponse(Response xacmlResponse) {
104 LOGGER.info("this convertResponse shouldn't be used");
109 * Load YAML coordination directive.
111 * @param directiveFilename yaml directive file to load
112 * @return the CoordinationDirective
114 public static CoordinationDirective loadCoordinationDirectiveFromFile(
115 String directiveFilename) {
116 if (directiveFilename == null) {
119 try (InputStream is = new FileInputStream(new File(directiveFilename))) {
120 String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
122 // Read the yaml into our Java Object
124 CoordinationDirective obj =
125 new StandardYamlCoder().decode(contents, CoordinationDirective.class);
126 LOGGER.debug(contents);
128 } catch (IOException | CoderException e) {
129 LOGGER.error("Error while loading YAML coordination directive", e);
135 * Generate Xacml rule implementing specified CoordinationDirective.
137 * @param cd the CoordinationDirective
138 * @param protoDir the directory containing Xacml implementation prototypes
139 * @return the generated Xacml policy
141 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
142 String protoDir) throws ToscaPolicyConversionException {
144 * Determine file names
146 String xacmlProtoFilename =
147 protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
148 LOGGER.info("xacmlProtoFilename={}", xacmlProtoFilename);
150 * Values to be substituted for placeholder's
152 final String uniqueId = UUID.randomUUID().toString();
153 final String cLOne = cd.getControlLoop(0);
154 final String cLTwo = cd.getControlLoop(1);
156 * Replace function placeholder's with appropriate values
158 String policyXml = ResourceUtils.getResourceAsString(xacmlProtoFilename);
159 if (policyXml == null) {
160 throw new ToscaPolicyConversionException("Unable to find prototype " + xacmlProtoFilename);
162 policyXml = policyXml.replace("UNIQUE_ID", uniqueId);
163 policyXml = policyXml.replace("CONTROL_LOOP_ONE", cLOne);
164 policyXml = policyXml.replace("CONTROL_LOOP_TWO", cLTwo);