diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2019-03-27 11:03:57 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-03-27 11:03:57 +0000 |
commit | bca4180329b4d9cc587abd9d8471573f49994d29 (patch) | |
tree | c0d2f6e94cd5b88bd36191a26e5de479e5fe1998 /models-tosca/src/main/java/org | |
parent | df3fbb6f39b670a014ddf52067cf01624033c4f3 (diff) | |
parent | cee84a9344f35356f86384b0d48127f5a83e3776 (diff) |
Merge "Add Legacy Op Policy Persistence"
Diffstat (limited to 'models-tosca/src/main/java/org')
7 files changed, 414 insertions, 212 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java index 60a1e454d..1db4d6e20 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java @@ -36,6 +36,7 @@ public class LegacyOperationalPolicy { @SerializedName("policy-id") private String policyId; + @SerializedName("policy-version") private String policyVersion; private String content; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java index 2f87020be..65f477572 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java @@ -23,14 +23,19 @@ package org.onap.policy.models.tosca.legacy.mapping; import java.util.HashMap; import java.util.Map; +import javax.ws.rs.core.Response; + import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; 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 operational policy to and from a TOSCA service template. @@ -39,51 +44,72 @@ import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper; */ public class LegacyOperationalPolicyMapper implements ToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class); - // TODO: Do this correctly with an atomic integer - private static int nextVersion = 1; + private static final PfConceptKey LEGACY_OPERATIONAL_TYPE = + new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0"); @Override - public ToscaServiceTemplate toToscaServiceTemplate(LegacyOperationalPolicy legacyOperationalPolicy) { - PfConceptKey policyKey = - new PfConceptKey(legacyOperationalPolicy.getPolicyId(), getNextVersion()); + public ToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) { + String incomingVersion = legacyOperationalPolicy.getPolicyVersion(); + if (incomingVersion == null) { + incomingVersion = "1"; + } + + PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0"); - ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey); + final ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey); - // TODO: Find out how to parse the PolicyType from the content - // TODO: Check if this is the correct way to set the policy type version - toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.1")); + toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE); - Map<String, String> propertyMap = new HashMap<>(); + final Map<String, String> propertyMap = new HashMap<>(); toscaPolicy.setProperties(propertyMap); toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent()); - PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.2"); - ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey); + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0"); - PfReferenceKey topologyTemplateKey = new PfReferenceKey(serviceTemplateKey, "TopolocyTemplate"); - serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate(topologyTemplateKey)); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); - PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.3"); - serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies(policiesKey)); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy); return serviceTemplate; } @Override - public LegacyOperationalPolicy fromToscaServiceTemplate(ToscaServiceTemplate serviceTemplate) { - // TODO Auto-generated method stub - return null; - } + public LegacyOperationalPolicy fromToscaServiceTemplate(final ToscaServiceTemplate serviceTemplate) { + ToscaUtils.assertPoliciesExist(serviceTemplate); + + if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) { + String errorMessage = "more than one policy found in service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + // Get the policy + final ToscaPolicy toscaPolicy = + serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next(); + + final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy(); + legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName()); + legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion())); + + if (toscaPolicy.getProperties() == null) { + String errorMessage = "no properties defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + final String content = toscaPolicy.getProperties().get("Content"); + if (toscaPolicy.getProperties() == null) { + String errorMessage = "property \"Content\" not defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + legacyOperationalPolicy.setContent(content); - /** - * Get the next policy version. - * - * @return the next version - */ - private static String getNextVersion() { - return "1.0." + nextVersion++; + return legacyOperationalPolicy; } } 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 new file mode 100644 index 000000000..42343e1df --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java @@ -0,0 +1,268 @@ +/*- + * ============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.provider; + +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.Response; + +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.LegacyOperationalPolicy; +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; +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.provider.SimpleToscaProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class); + + private static final String FIRST_POLICY_VERSION = "1"; + + // Recurring constants + private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: "; + + /** + * Get legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the policies found + * @throws PfModelException on errors getting policies + */ + public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + throws PfModelException { + + 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 LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + } + + /** + * Create legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param legacyOperationalPolicy the definition of the policy to be created. + * @return the created policy + * @throws PfModelException on errors creating policies + */ + public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao, + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + + // We need to find the latest policy and update the major version, if there is no policy with this ID, then + // we set it to the first version + ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId()); + + if (newestPolicy == null) { + legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION); + } else { + legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1)); + } + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); + } + + /** + * Update legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param legacyOperationalPolicy the definition of the policy to be updated + * @return the updated policy + * @throws PfModelException on errors updating policies + */ + public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao, + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + + // We need to find the latest policy and use the major version, if there is no policy with this ID, then + // we have an error + ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId()); + + if (newestPolicy == null) { + String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId(); + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } else { + legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion())); + } + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); + } + + /** + * Delete legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the deleted policy + * @throws PfModelException on errors deleting policies + */ + public LegacyOperationalPolicy deleteOperationalPolicy(@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 LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + } + + /** + * Get legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the policies found + * @throws PfModelException on errors getting policies + */ + public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + throws PfModelException { + return null; + } + + /** + * Create legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param legacyGuardPolicy the definition of the policy to be created. + * @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; + } + + /** + * Update legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param legacyGuardPolicy the definition of the policy to be updated + * @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; + } + + + /** + * Delete legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @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; + } + + /** + * Get the latest policy for a policy ID. + * + * @param dao The DAO to read from + * @param policyId the ID of the policy + * @return the policy + */ + private ToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) { + // 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 + ToscaPolicy newestPolicy = null; + + for (ToscaPolicy policy : policyList) { + if (!policyId.equals(policy.getKey().getName())) { + continue; + } + + // We found a matching policy + if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) { + // First policy found + newestPolicy = policy; + } + } + return newestPolicy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java deleted file mode 100644 index da9d929df..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java +++ /dev/null @@ -1,139 +0,0 @@ -/*- - * ============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.provider; - -import lombok.NonNull; - -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; -import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; - -/** - * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class LegacyToscaProvider { - /** - * Get legacy operational policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - return null; - } - - /** - * Create legacy operational policy. - * - * @param dao the DAO to use to access the database - * @param legacyOperationalPolicy the definition of the policy to be created. - * @return the created policy - * @throws PfModelException on errors creating policies - */ - public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao, - @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { - return null; - } - - /** - * Update legacy operational policy. - * - * @param dao the DAO to use to access the database - * @param legacyOperationalPolicy the definition of the policy to be updated - * @return the updated policy - * @throws PfModelException on errors updating policies - */ - public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao, - @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { - return null; - } - - /** - * Delete legacy operational policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @return the deleted policy - * @throws PfModelException on errors deleting policies - */ - public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - return null; - } - - /** - * Get legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @return the policies found - * @throws PfModelException on errors getting policies - */ - public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - return null; - } - - /** - * Create legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param legacyGuardPolicy the definition of the policy to be created. - * @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; - } - - /** - * Update legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param legacyGuardPolicy the definition of the policy to be updated - * @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; - } - - /** - * Delete legacy guard policy. - * - * @param dao the DAO to use to access the database - * @param policyId ID of the policy. - * @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; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java index ee9c2eae5..f318bb6be 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java @@ -47,7 +47,7 @@ import org.onap.policy.models.base.PfConceptKey; public class ToscaPolicies extends PfConceptContainer<ToscaPolicy> { private static final long serialVersionUID = -7526648702327776101L; - public static final String DEFAULT_NAME = "ToscaPoliciessSimple"; + public static final String DEFAULT_NAME = "ToscaPoliciesSimple"; public static final String DEFAULT_VERSION = "1.0.0"; /** diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index 2240ef099..c7984c5ea 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -34,6 +34,7 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; +import org.onap.policy.models.tosca.utils.ToscaUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,7 +85,7 @@ public class SimpleToscaProvider { public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertPolicyTypesExist(serviceTemplate); + ToscaUtils.assertPolicyTypesExist(serviceTemplate); for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.create(policyType); @@ -114,7 +115,7 @@ public class SimpleToscaProvider { public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertPolicyTypesExist(serviceTemplate); + ToscaUtils.assertPolicyTypesExist(serviceTemplate); for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.update(policyType); @@ -192,7 +193,7 @@ public class SimpleToscaProvider { public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertPoliciesExist(serviceTemplate); + ToscaUtils.assertPoliciesExist(serviceTemplate); for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { dao.create(policy); @@ -222,7 +223,7 @@ public class SimpleToscaProvider { public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao, @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertPoliciesExist(serviceTemplate); + ToscaUtils.assertPoliciesExist(serviceTemplate); for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { dao.update(policy); @@ -258,44 +259,4 @@ public class SimpleToscaProvider { return serviceTemplate; } - - /** - * Check if policy types have been specified is initialized. - */ - private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) { - if (serviceTemplate.getPolicyTypes() == null) { - String errorMessage = "no policy types specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { - String errorMessage = "list of policy types specified on service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - } - - /** - * Check if policy types have been specified is initialized. - */ - private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) { - if (serviceTemplate.getTopologyTemplate() == null) { - String errorMessage = "topology template not specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { - String errorMessage = "no policies specified on topology template of service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { - String errorMessage = "list of policies specified on topology template of service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java new file mode 100644 index 000000000..a02bfa4b7 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -0,0 +1,85 @@ +/*- + * ============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.utils; + +import javax.ws.rs.core.Response; + +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility class for TOSCA concepts. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public final class ToscaUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class); + + /** + * Private constructor to prevent subclassing. + */ + private ToscaUtils() { + } + + /** + * Check if policy types have been specified is initialized. + */ + public static void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getPolicyTypes() == null) { + String errorMessage = "no policy types specified on service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { + String errorMessage = "list of policy types specified on service template is empty"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + } + + /** + * Check if policy types have been specified is initialized. + */ + public static void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getTopologyTemplate() == null) { + String errorMessage = "topology template not specified on service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { + String errorMessage = "no policies specified on topology template of service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { + String errorMessage = "list of policies specified on topology template of service template is empty"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + } + + +} |