/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
static {
- GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout",
+ GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
- GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
- new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
- GUARD_POLICY_TYPE_MAP.put("guard.blacklist",
+ GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
+ GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
}
@Override
public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
- PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
+ PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput);
if (guardPolicyType == null) {
String errorMessage =
"policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
toscaPolicy.setMetadata(metadata);
final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
- serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
+ serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
}
@Override
- public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
- final JpaToscaServiceTemplate serviceTemplate) {
+ public Map<String, LegacyGuardPolicyOutput>
+ fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
ToscaUtils.assertPoliciesExist(serviceTemplate);
final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
- legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
+ legacyGuardPolicyOutput.setVersion(toscaPolicy.getKey().getVersion());
if (toscaPolicy.getMetadata() == null) {
String errorMessage = "no metadata defined on TOSCA policy";
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
- final Map<String, Object> metadata = new LinkedHashMap<>();
- for (Entry<String, String> metadataEntry : toscaPolicy.getMetadata().entrySet()) {
- if (POLICY_VERSION.equals(metadataEntry.getKey())) {
- metadata.put(POLICY_VERSION, Integer.parseInt(metadataEntry.getValue()));
- } else {
- metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
- }
- }
+ final Map<String, Object> metadata = new LinkedHashMap<>(toscaPolicy.getMetadata());
+
+ // if version exists, convert it to int
+ metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString()));
+
legacyGuardPolicyOutput.setMetadata(metadata);
if (toscaPolicy.getProperties() == null) {
return legacyGuardPolicyOutputMap;
}
+
+ private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
+ final String policyId = legacyGuardPolicyInput.getPolicyId();
+ if (policyId == null) {
+ return null;
+ }
+
+ for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
+ if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
+ return guardPolicyTypeEntry.getValue();
+ }
+ }
+
+ return null;
+ }
}