diff options
Diffstat (limited to 'models-tosca/src/main/java')
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java | 80 | ||||
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java (renamed from models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java) | 9 | ||||
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java | 38 | ||||
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java | 151 | ||||
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java | 84 |
5 files changed, 346 insertions, 16 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java new file mode 100644 index 000000000..b46737d2b --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import lombok.Data; + +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Content object of a Legacy Guard Policy. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +public class LegacyGuardPolicyContent { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); + + private String actor; + private String recipe; + private String targets; + private String clname; + private String limit; + private String timeWindow; + private String timeUnits; + private String min; + private String max; + private String guardActiveStart; + private String guardActiveEnd; + + /** + * Get contents as a map. + * + * @return the contents as a map. + */ + public Map<String, String> getAsPropertyMap() { + final Map<String, String> propertyMap = new HashMap<>(); + + try { + for (Field field : this.getClass().getDeclaredFields()) { + if (field.get(this) != null && field.getType().equals(String.class)) { + propertyMap.put(field.getName(), (String)field.get(this)); + } + } + } catch (Exception exc) { + String errorMessage = "could not convert content to a property map"; + LOGGER.warn(errorMessage, exc); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc); + + } + + return propertyMap; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java index 59715e4f9..18853c100 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java @@ -21,7 +21,7 @@ package org.onap.policy.models.tosca.legacy.concepts; -import java.util.Map; +import com.google.gson.annotations.SerializedName; import lombok.Data; @@ -29,15 +29,16 @@ import lombok.Data; * Definition of a legacy guard policy stored as a TOSCA policy. * * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) */ @Data -public class LegacyGuardPolicy { +public class LegacyGuardPolicyInput { + @SerializedName("policy-id") private String policyId; + @SerializedName("policy-version") private String policyVersion; - private Map<String, String> content; + private LegacyGuardPolicyContent content; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java new file mode 100644 index 000000000..2fe3d88e4 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts; + +import java.util.Map; + +import lombok.Data; + +/** + * Body of a legacy guard policy output. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +public class LegacyGuardPolicyOutput { + private String type; + private String version; + private Map<String, Object> metadata; + private Map<String, LegacyGuardPolicyContent> properties; +} 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 new file mode 100644 index 000000000..141b4e783 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.mapping; + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; +import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper; +import org.onap.policy.models.tosca.utils.ToscaUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class maps a legacy guard policy to and from a TOSCA service template. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyGuardPolicyMapper + implements ToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); + + private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); + + static { + GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout", + 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", + new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); + } + + @Override + public ToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) { + PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId()); + if (guardPolicyType == null) { + String errorMessage = + "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + String version = legacyGuardPolicyInput.getPolicyVersion(); + if (version != null) { + version = version + ".0.0"; + } else { + version = guardPolicyType.getVersion(); + } + + PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version); + + final ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey); + toscaPolicy.setType(guardPolicyType); + toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap()); + + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0"); + + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy); + + return serviceTemplate; + } + + @Override + public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(final ToscaServiceTemplate serviceTemplate) { + ToscaUtils.assertPoliciesExist(serviceTemplate); + + final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>(); + + for (ToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) { + + final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput(); + legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName()); + legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion()); + + final Map<String, Object> metadata = new LinkedHashMap<>(); + metadata.put("policy-id", toscaPolicy.getKey().getName()); + metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion()); + legacyGuardPolicyOutput.setMetadata(metadata); + + if (toscaPolicy.getProperties() == null) { + String errorMessage = "no properties defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + 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 + + final Map<String, LegacyGuardPolicyContent> 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; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java index 42343e1df..04a010f08 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.legacy.provider; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.ws.rs.core.Response; @@ -30,8 +31,10 @@ import lombok.NonNull; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; +import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; @@ -193,9 +196,24 @@ public class LegacyProvider { * @return the policies found * @throws PfModelException on errors getting policies */ - public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) throws PfModelException { - return null; + + ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId); + + if (newestPolicy == null) { + String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + // Create the structure of the TOSCA service template to contain the policy type + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); } /** @@ -206,9 +224,15 @@ public class LegacyProvider { * @return the created policy * @throws PfModelException on errors creating policies */ - public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao, + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } /** @@ -219,9 +243,15 @@ public class LegacyProvider { * @return the updated policy * @throws PfModelException on errors updating policies */ - public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao, + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } @@ -233,9 +263,39 @@ public class LegacyProvider { * @return the deleted policy * @throws PfModelException on errors deleting policies */ - public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - return null; + public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao, + @NonNull final String policyId) throws PfModelException { + + // Get all the policies in the database and check the policy ID against the policies returned + List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class); + + // Find the latest policy that matches the ID + List<ToscaPolicy> policyDeleteList = new ArrayList<>(); + + for (ToscaPolicy policy : policyList) { + if (policyId.equals(policy.getKey().getName())) { + policyDeleteList.add(policy); + } + } + + if (policyDeleteList.isEmpty()) { + String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + // Create the structure of the TOSCA service template to contain the policy type + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + + for (ToscaPolicy deletePolicy : policyDeleteList) { + dao.delete(deletePolicy); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), + deletePolicy); + } + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); } /** |