X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Flegacy%2Fmapping%2FLegacyGuardPolicyMapper.java;h=b7ebdcec33cac48b54d0bd924d1b9ab010788ea8;hb=2358a161a4bbd0f0c97696b61fd725c33f7267e6;hp=cc37338e44a1a219273ccb67473b914ca7ce5fd8;hpb=cc5b96bfd33cd7d91fe6994d348e8d6a0ebb54fa;p=policy%2Fmodels.git diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java index cc37338e4..b7ebdcec3 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -1,6 +1,6 @@ /*- * ============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. @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.legacy.mapping; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import javax.ws.rs.core.Response; @@ -48,22 +49,23 @@ public class LegacyGuardPolicyMapper implements JpaToscaServiceTemplateMapper> { private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); + // Tag for metadata fields + private static final String POLICY_ID = "policy-id"; + private static final String POLICY_VERSION = "policy-version"; + private static final Map 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.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"; @@ -84,8 +86,13 @@ public class LegacyGuardPolicyMapper toscaPolicy.setType(guardPolicyType); toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap()); + final Map metadata = new LinkedHashMap<>(); + metadata.put(POLICY_ID, toscaPolicy.getKey().getName()); + metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion())); + toscaPolicy.setMetadata(metadata); + final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0"); + serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0"); serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); @@ -96,8 +103,8 @@ public class LegacyGuardPolicyMapper } @Override - public Map fromToscaServiceTemplate( - final JpaToscaServiceTemplate serviceTemplate) { + public Map + fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) { ToscaUtils.assertPoliciesExist(serviceTemplate); final Map legacyGuardPolicyOutputMap = new LinkedHashMap<>(); @@ -107,11 +114,19 @@ public class LegacyGuardPolicyMapper 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"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + final Map metadata = new LinkedHashMap<>(toscaPolicy.getMetadata()); + + // if version exists, convert it to int + metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString())); - final Map metadata = new LinkedHashMap<>(); - metadata.put("policy-id", toscaPolicy.getKey().getName()); - metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion()); legacyGuardPolicyOutput.setMetadata(metadata); if (toscaPolicy.getProperties() == null) { @@ -121,33 +136,30 @@ public class LegacyGuardPolicyMapper } final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); - // @formatter:off - content.setActor( toscaPolicy.getProperties().get("actor")); - content.setClname( toscaPolicy.getProperties().get("clname")); - content.setGuardActiveEnd( toscaPolicy.getProperties().get("guardActiveEnd")); - content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart")); - content.setLimit( toscaPolicy.getProperties().get("limit")); - content.setMax( toscaPolicy.getProperties().get("max")); - content.setMin( toscaPolicy.getProperties().get("min")); - content.setRecipe( toscaPolicy.getProperties().get("recipe")); - content.setTargets( toscaPolicy.getProperties().get("targets")); - content.setTimeUnits( toscaPolicy.getProperties().get("timeUnits")); - content.setTimeWindow( toscaPolicy.getProperties().get("timeWindow")); - // @formatter:on + content.setContent(toscaPolicy.getProperties()); final Map propertiesMap = new LinkedHashMap<>(); propertiesMap.put("content", content); legacyGuardPolicyOutput.setProperties(propertiesMap); - if (toscaPolicy.getProperties() == null) { - String errorMessage = "property \"Content\" not defined on TOSCA policy"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput); } return legacyGuardPolicyOutputMap; } + + private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) { + final String policyId = legacyGuardPolicyInput.getPolicyId(); + if (policyId == null) { + return null; + } + + for (Entry guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) { + if (policyId.startsWith(guardPolicyTypeEntry.getKey())) { + return guardPolicyTypeEntry.getValue(); + } + } + + return null; + } }