//
// Policy name should be at the root
//
- String policyName = toscaPolicy.getMetadata().get("policy-id");
String type = toscaPolicy.getType();
String coordinationFunctionPath = "src/main/resources/coordination/function";
Map<String, Object> policyProps = toscaPolicy.getProperties();
cd.setCoordinationFunction(type);
cd.setControlLoop(controlLoop);
LOGGER.debug("CoordinationDirective = {}", cd);
-
+ //
+ // Generate the xacml policy as a string
+ //
String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
-
LOGGER.debug("xacmlStr\n{}", xacmlStr);
- PolicyType scannedPolicy = null;
+ //
+ // Scan the string and convert to PoilcyType
+ //
try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
- scannedPolicy = (PolicyType) XACMLPolicyScanner.readPolicy(is);
+ return (PolicyType) XACMLPolicyScanner.readPolicy(is);
} catch (IOException e) {
- LOGGER.error("Failed to read policy", e);
+ throw new ToscaPolicyConversionException("Failed to read policy", e);
}
- return scannedPolicy;
}
@Override
@Override
public DecisionResponse convertResponse(Response xacmlResponse) {
- LOGGER.info("this convertRequest shouldn't be used");
+ LOGGER.info("this convertResponse shouldn't be used");
return null;
}
//
Yaml yaml = new Yaml(new Constructor(CoordinationDirective.class));
Object obj = yaml.load(contents);
-
LOGGER.debug(contents);
-
return (CoordinationDirective) obj;
} catch (IOException e) {
LOGGER.error("Error while loading YAML coordination directive", e);
* @return the generated Xacml policy
*/
public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
- String protoDir) {
+ String protoDir) throws ToscaPolicyConversionException {
/*
* Determine file names
*/
final String cLOne = cd.getControlLoop(0);
final String cLTwo = cd.getControlLoop(1);
/*
- * Replace prototype placeholders with appropriate values
+ * Replace function placeholders with appropriate values
*/
- String xacmlPolicy = null;
try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
- xacmlPolicy = stream.map(s -> s.replaceAll("UNIQUE_ID", uniqueId))
+ return stream.map(s -> s.replaceAll("UNIQUE_ID", uniqueId))
.map(s -> s.replaceAll("CONTROL_LOOP_ONE", cLOne))
.map(s -> s.replaceAll("CONTROL_LOOP_TWO", cLTwo))
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
- LOGGER.error("Error while generating XACML policy for coordination directive", e);
+ throw new
+ ToscaPolicyConversionException("Error while generating XACML policy for coordination directive", e);
}
- return xacmlPolicy;
}
}