diff options
Diffstat (limited to 'models-tosca')
9 files changed, 732 insertions, 213 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); + } + } + + +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java new file mode 100644 index 000000000..271e019d9 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java @@ -0,0 +1,317 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import com.google.gson.Gson; + +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; + +/** + * Test the {@link LegacyProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyProviderTest { + private Connection connection; + private PfDao pfDao; + private Gson gson; + + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database + // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance + connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); + + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + // Use the persistence unit ToscaConceptTest to test towards the h2 database + // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + gson = new Gson(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + connection.close(); + } + + @Test + public void testPoliciesGet() throws PfModelException { + try { + new LegacyProvider().getOperationalPolicy(null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().getOperationalPolicy(null, ""); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().getOperationalPolicy(pfDao, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist"); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); + } + + LegacyOperationalPolicy originalLop = + gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + LegacyOperationalPolicy.class); + + assertNotNull(originalLop); + + LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); + + assertEquals(originalLop, createdLop); + + LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + + assertEquals(gotLop, originalLop); + + String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); + String actualJsonOutput = gson.toJson(gotLop); + + assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + + LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); + LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + assertEquals(gotLopV2, createdLopV2); + } + + @Test + public void testPolicyCreate() throws PfModelException { + try { + new LegacyProvider().createOperationalPolicy(null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().createOperationalPolicy(pfDao, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); + } + + LegacyOperationalPolicy originalLop = + gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + LegacyOperationalPolicy.class); + + assertNotNull(originalLop); + + LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); + + assertEquals(originalLop, createdLop); + + LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + + assertEquals(gotLop, originalLop); + + String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); + String actualJsonOutput = gson.toJson(gotLop); + + assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + } + + + @Test + public void testPolicyUpdate() throws PfModelException { + try { + new LegacyProvider().updateOperationalPolicy(null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().updateOperationalPolicy(pfDao, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("no policy found for policy ID: null", exc.getMessage()); + } + + LegacyOperationalPolicy originalLop = + gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + LegacyOperationalPolicy.class); + + assertNotNull(originalLop); + + LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); + assertEquals(originalLop, createdLop); + + LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + assertEquals(gotLop, originalLop); + + originalLop.setContent("Some New Content"); + LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop); + assertEquals(originalLop, updatedLop); + + LegacyOperationalPolicy gotUpdatedLop = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + assertEquals(gotUpdatedLop, originalLop); + assertEquals("Some New Content", gotUpdatedLop.getContent()); + } + + + @Test + public void testPoliciesDelete() throws PfModelException { + try { + new LegacyProvider().deleteOperationalPolicy(null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().deleteOperationalPolicy(null, ""); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("dao is marked @NonNull but is null", exc.getMessage()); + } + + try { + new LegacyProvider().deleteOperationalPolicy(pfDao, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); + } + + + try { + new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist"); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); + } + + LegacyOperationalPolicy originalLop = + gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + LegacyOperationalPolicy.class); + + assertNotNull(originalLop); + + LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); + assertEquals(originalLop, createdLop); + + LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + + assertEquals(gotLop, originalLop); + + String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); + String actualJsonOutput = gson.toJson(gotLop); + + assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + + LegacyOperationalPolicy deletedLop = + new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId()); + assertEquals(deletedLop, originalLop); + + try { + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); + } + + LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy(); + otherLop.setPolicyId("another-policy"); + otherLop.setPolicyVersion("1"); + otherLop.setContent("content"); + + LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop); + assertEquals(otherLop, createdOtherLop); + + try { + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); + } + + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java index 3c9deb7df..5d1fa42ad 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java @@ -68,7 +68,7 @@ public class LegacyOperationalPolicySerializationTest { LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); - assertEquals("operational.restart:1.0.1", + assertEquals("operational.restart:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId()); } } |