diff options
author | liamfallon <liam.fallon@est.tech> | 2020-12-17 12:10:21 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-12-23 11:11:05 +0000 |
commit | b87e0242ce1a957740ee988bec3b82e3628adbed (patch) | |
tree | 0d75dce2189f7daf719293620a01af7afc83bc15 | |
parent | 12fce55a66848bcc7f71430324b3a9051b8ce0d4 (diff) |
Add Service Template TOSCA handling
Today we can only handle a single service template in the database. We
should be able to handle multiple service templates and assign arbitrary
policy types and policies to maned and versioned service templates.
This review brings in the Java API in models provider for handling
service templates in this way and uses a simplistic single-teplate
implementation in models-tosca, that will work but still only handles a
single service template under the hood.
Issue-ID: POLICY-2900
Change-Id: Ia02dea8abe44b7f407e685090a4b8e0360889653
Signed-off-by: liamfallon <liam.fallon@est.tech>
18 files changed, 793 insertions, 112 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java index 4c2cf0c4f..d2015eb7e 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java @@ -198,9 +198,9 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex } incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(), - incomingConceptEntry.getValue(), PfNameVersion::getName)); + incomingConceptEntry.getValue(), PfNameVersion::getDefinedName)); incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(), - incomingConceptEntry.getValue(), PfNameVersion::getVersion)); + incomingConceptEntry.getValue(), PfNameVersion::getDefinedVersion)); C jpaConcept = getConceptNewInstance(); // This cast allows us to call the fromAuthorative method diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java index f9fde53e5..4e4177004 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java @@ -38,6 +38,24 @@ public interface PfNameVersion { public void setVersion(final String version); /** + * Get the defined name for a concept, return null if no name is defined. + * + * @return the defined name + */ + public default String getDefinedName() { + return getName(); + } + + /** + * Get the defined version for a concept, return null if no version is defined. + * + * @return the defined version + */ + public default String getDefinedVersion() { + return getVersion(); + } + + /** * Compare two name version implementation objects. * * @param left the left name/version implementation diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java index f7e29f1b4..342fbab73 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java @@ -56,8 +56,7 @@ public interface PfObjectFilter<T> { } /** - * Gets a predicate used to filter an item in a list by exactly matching an extracted value - * with some text. + * Gets a predicate used to filter an item in a list by exactly matching an extracted value with some text. * * @param text the desired text to check against, or {@code null} if to accept everything * @param extractor function to extract the value, to be matched, from a list item @@ -73,11 +72,9 @@ public interface PfObjectFilter<T> { } /** - * Gets a predicate used to filter an item in a list by comparing the start of an - * extracted value with a prefix. + * Gets a predicate used to filter an item in a list by comparing the start of an extracted value with a prefix. * - * @param prefix the desired prefix to check against, or {@code null} if to accept - * everything + * @param prefix the desired prefix to check against, or {@code null} if to accept everything * @param extractor function to extract the value, to be matched, from a list item * @return a predicate to match a prefix with a value from a list item */ @@ -94,8 +91,7 @@ public interface PfObjectFilter<T> { } /** - * Gets a predicate used to filter an item in a list by matching an extracted value - * with a regular expression. + * Gets a predicate used to filter an item in a list by matching an extracted value with a regular expression. * * @param pattern regular expression to match, or {@code null} if to accept everything * @param extractor function to extract the value, to be matched, from a list item @@ -140,7 +136,8 @@ public interface PfObjectFilter<T> { * The list is sorted so if the last element name is the same as the current element name, the current * element should be removed. */ - if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) { + if (PfUtils.compareObjects(((PfNameVersion) curElement).getName(), + ((PfNameVersion) lastElement).getName()) != 0) { // have a new name - done comparing with the old "current" ++icur; } diff --git a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/ModifyNssiTest.java b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/ModifyNssiTest.java index 2d290d68a..2ef9caac9 100644 --- a/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/ModifyNssiTest.java +++ b/models-interactions/model-actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/ModifyNssiTest.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2020 Wipro Limited. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,14 +29,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.List; -import java.util.Map; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.controlloop.actorserviceprovider.OperationProperties; import org.onap.policy.controlloop.actorserviceprovider.OperationResult; @@ -47,7 +45,6 @@ public class ModifyNssiTest extends BasicSoOperation { private ModifyNssi oper; - private static StandardCoder coder = new StandardCoder(); public ModifyNssiTest() { super(DEFAULT_ACTOR, ModifyNssi.NAME); @@ -63,6 +60,7 @@ public class ModifyNssiTest extends BasicSoOperation { destroyAfterClass(); } + @Override @Before public void setUp() throws Exception { super.setUp(); @@ -71,9 +69,8 @@ public class ModifyNssiTest extends BasicSoOperation { @Test public void testSuccess() throws Exception { - HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT) - .path("3gppservices/v7/modify").pollPath("orchestrationRequests/v5/") - .maxPolls(2).build(); + HttpPollingParams opParams = HttpPollingParams.builder().clientName(MY_CLIENT).path("3gppservices/v7/modify") + .pollPath("orchestrationRequests/v5/").maxPolls(2).build(); config = new HttpPollingConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory()); params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build(); @@ -98,14 +95,11 @@ public class ModifyNssiTest extends BasicSoOperation { @Test public void testGetPropertyNames() { - assertThat(oper.getPropertyNames()).isEqualTo( - List.of( - OperationProperties.EVENT_PAYLOAD)); + assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.EVENT_PAYLOAD)); } private String getPayload() { - return ResourceUtils - .getResourceAsString("src/test/resources/ModifyNSSI.json"); + return ResourceUtils.getResourceAsString("src/test/resources/ModifyNSSI.json"); } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java index f2f7318e8..1e8fd24ff 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java @@ -35,6 +35,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter; /** * This interface describes the operations that are provided to users and components for reading objects from and @@ -54,6 +55,58 @@ public interface PolicyModelsProvider extends AutoCloseable { public void close() throws PfModelException; /** + * Get service templates. + * + * @param name the name of the topology template to get, set to null to get all service templates + * @param version the version of the service template to get, set to null to get all service templates + * @return the topology templates found + * @throws PfModelException on errors getting service templates + */ + public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) + throws PfModelException; + + /** + * Get filtered service templates. + * + * @param filter the filter for the service templates to get + * @return the service templates found + * @throws PfModelException on errors getting service templates + */ + public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull final ToscaServiceTemplateFilter filter) + throws PfModelException; + + /** + * Create service template. + * + * @param serviceTemplate the service template to be created + * @return the created service template + * @throws PfModelException on errors creating the service template + */ + public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException; + + /** + * Update service template. + * + * @param serviceTemplate the service template to be updated + * @return the updated service template + * @throws PfModelException on errors updating the service template + */ + public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException; + + /** + * Delete service template. + * + * @param name the name of the service template to delete. + * @param version the version of the service template to delete. + * @return the TOSCA service template that was deleted + * @throws PfModelException on errors deleting policy types + */ + public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version) + throws PfModelException; + + /** * Get policy types. * * @param name the name of the policy type to get, set to null to get all policy types @@ -104,7 +157,7 @@ public interface PolicyModelsProvider extends AutoCloseable { throws PfModelException; /** - * Create policy types. + * Update policy types. * * @param serviceTemplate the service template containing the definition of the policy types to be modified * @return the TOSCA service template containing the modified policy types @@ -119,7 +172,7 @@ public interface PolicyModelsProvider extends AutoCloseable { * @param name the name of the policy type to delete. * @param version the version of the policy type to delete. * @return the TOSCA service template containing the policy type that was deleted - * @throws PfModelException on errors deleting policy types + * @throws PfModelException on errors deleting the policy type */ public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version) throws PfModelException; diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index e27b9a43c..29b2d70b0 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -50,6 +50,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter; import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -131,6 +132,43 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } @Override + public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version) + throws PfModelException { + assertInitialized(); + return new AuthorativeToscaProvider().getServiceTemplateList(pfDao, name, version); + } + + + @Override + public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter) + throws PfModelException { + assertInitialized(); + return new AuthorativeToscaProvider().getFilteredServiceTemplateList(pfDao, filter); + } + + @Override + public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { + assertInitialized(); + return new AuthorativeToscaProvider().createServiceTemplate(pfDao, serviceTemplate); + } + + @Override + public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { + assertInitialized(); + return new AuthorativeToscaProvider().updateServiceTemplate(pfDao, serviceTemplate); + } + + @Override + public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version) + throws PfModelException { + assertInitialized(); + + return new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, name, version); + } + + @Override public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException { assertInitialized(); return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version); diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index 16f5a2b74..1d892272c 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.ws.rs.core.Response; +import lombok.NonNull; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; @@ -42,6 +43,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter; /** * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework. @@ -69,6 +71,36 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { // Not required on the dummy provider } + + @Override + public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException { + return new ArrayList<>(); + } + + @Override + public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter) + throws PfModelException { + return new ArrayList<>(); + } + + @Override + public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return serviceTemplate; + } + + @Override + public ToscaServiceTemplate updateServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return serviceTemplate; + } + + @Override + public ToscaServiceTemplate deleteServiceTemplate(@NonNull String name, @NonNull String version) + throws PfModelException { + return getDummyResponse("dummyimpl/DummyToscaServiceTemplateDeleteResponse.json"); + } + @Override public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException { return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json"); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index 453f86633..1158307b4 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -41,6 +41,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter; /** * Dummy implementation of {@link PolicyModelsProvider} with bad constructor. @@ -63,6 +64,30 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override + public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter) + throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate updateServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate deleteServiceTemplate(@NonNull String name, @NonNull String version) + throws PfModelException { + return null; + } + + @Override public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException { return null; } @@ -203,4 +228,10 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { // Not implemented return null; } + + @Override + public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException { + // TODO Auto-generated method stub + return null; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java index 10d3b7a14..5c1167c7d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java @@ -32,6 +32,7 @@ import javax.ws.rs.core.Response; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; +import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfNameVersion; @@ -43,8 +44,8 @@ import org.onap.policy.models.base.PfNameVersion; @Data @NoArgsConstructor public class ToscaEntity implements PfNameVersion { - private String name; - private String version; + private String name = PfKey.NULL_KEY_NAME; + private String version = PfKey.NULL_KEY_VERSION; @ApiModelProperty(name = "derived_from") @SerializedName("derived_from") @@ -81,6 +82,16 @@ public class ToscaEntity implements PfNameVersion { return new ToscaEntityKey(name, version); } + @Override + public String getDefinedName() { + return (PfKey.NULL_KEY_NAME.equals(name) ? null : name); + } + + @Override + public String getDefinedVersion() { + return (PfKey.NULL_KEY_VERSION.equals(version) ? null : version); + } + /** * Convert a list of maps of TOSCA entities into a regular map. * @@ -129,5 +140,4 @@ public class ToscaEntity implements PfNameVersion { return entityMap; } - } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateComparator.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateComparator.java new file mode 100644 index 000000000..015c10bdd --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateComparator.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.authorative.concepts; + +import java.util.Comparator; + +/** + * Compare two ToscaServiceTemplate objects. + */ +public class ToscaServiceTemplateComparator implements Comparator<ToscaServiceTemplate> { + + @Override + public int compare(final ToscaServiceTemplate left, final ToscaServiceTemplate right) { + return left.compareNameVersion(left, right); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilter.java new file mode 100644 index 000000000..75b13969d --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilter.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.authorative.concepts; + +import java.util.List; +import java.util.stream.Collectors; +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; +import org.onap.policy.models.base.PfObjectFilter; + +/** + * Filter class for searches for {@link ToscaServiceTemplate} instances. If any fields are null, they are ignored. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Builder +@Data +public class ToscaServiceTemplateFilter implements PfObjectFilter<ToscaServiceTemplate> { + public static final String LATEST_VERSION = "LATEST"; + + // Regular expression + private String name; + + // Regular Expression, set to LATEST_VERRSION to get the latest version + private String version; + + @Override + public List<ToscaServiceTemplate> filter(@NonNull final List<ToscaServiceTemplate> originalList) { + + // @formatter:off + List<ToscaServiceTemplate> returnList = originalList.stream() + .filter(p -> filterString(p.getName(), name)) + .filter(p -> LATEST_VERSION.equals(version) + || filterString(p.getVersion(), version)) + .collect(Collectors.toList()); + // @formatter:off + + if (LATEST_VERSION.equals(version)) { + return this.latestVersionFilter(returnList, new ToscaServiceTemplateComparator()); + } else { + return returnList; + } + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplates.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplates.java new file mode 100644 index 000000000..912cfd580 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplates.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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.authorative.concepts; + +import com.google.gson.annotations.SerializedName; +import io.swagger.annotations.ApiModelProperty; +import java.util.List; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * Class to represent a map of TOSCA service templates. + */ +@Data +@EqualsAndHashCode +public class ToscaServiceTemplates { + @ApiModelProperty(name = "service_templates") + @SerializedName("service_templates") + private List<ToscaServiceTemplate> serviceTemplates; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index e8f99f742..d20f5d080 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -39,6 +39,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils; @@ -57,29 +58,59 @@ public class AuthorativeToscaProvider { private static final Object providerLockObject = "providerLockObject"; /** - * Get a service template. + * Get service templates. * * @param dao the DAO to use to access the database - * @param name the name of the control loop to get. - * @param version the version of the control loop to get. - * @return the control loops found - * @throws PfModelException on errors getting control loops + * @param name the name of the service template to get. + * @param version the version of the service template to get. + * @return the service templates found + * @throws PfModelException on errors getting service templates */ - public ToscaServiceTemplate getServiceTemplate(@NonNull final PfDao dao, final String name, final String version) - throws PfModelException { + public List<ToscaServiceTemplate> getServiceTemplateList(PfDao dao, String name, String version) + throws PfModelException { synchronized (providerLockObject) { - LOGGER.debug("->getServiceTemplate: name={}, version={}", name, version); + LOGGER.debug("->getServiceTemplateList: name={}, version={}", name, version); - ToscaServiceTemplate gotServiceTemplate = new SimpleToscaProvider().getServiceTemplate(dao).toAuthorative(); + List<ToscaServiceTemplate> serviceTemplateList = new ArrayList<>(); - LOGGER.debug("<-getServiceTemplate: name={}, version={}, gotServiceTemplate={}", name, version, - gotServiceTemplate); - return gotServiceTemplate; + try { + ToscaServiceTemplate serviceTemplate = + new SimpleToscaProvider().getServiceTemplate(dao).toAuthorative(); + serviceTemplateList.add(serviceTemplate); + } catch (PfModelRuntimeException pfme) { + return handlePfModelRuntimeException(pfme); + } + + LOGGER.debug("<-getServiceTemplateList: name={}, version={}, serviceTemplateList={}", name, version, + serviceTemplateList); + return serviceTemplateList; } } /** + * Get filtered service templates. + * + * @param pfDao the DAO to use to access the database + * @param filter the filter for the service templates to get + * @return the service templates found + * @throws PfModelException on errors getting service templates + */ + public List<ToscaServiceTemplate> getFilteredServiceTemplateList(PfDao pfDao, + @NonNull ToscaServiceTemplateFilter filter) throws PfModelException { + + LOGGER.debug("->getFilteredServiceTemplateList: filter={}", filter); + + List<ToscaServiceTemplate> filteredServiceTemplateList = + filter.filter(getServiceTemplateList(pfDao, null, null)); + + LOGGER.debug("<-getFilteredServiceTemplateList: filter={}, filteredServiceTemplateList={}", filter, + filteredServiceTemplateList); + + return filteredServiceTemplateList; + } + + /** * Create a service template. * * @param dao the DAO to use to access the database @@ -88,13 +119,13 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors creating the service template */ public ToscaServiceTemplate createServiceTemplate(@NonNull final PfDao dao, - @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->createServiceTemplate: serviceTemplate={}", serviceTemplate); ToscaServiceTemplate createdServiceTemplate = new SimpleToscaProvider() - .appendToServiceTemplate(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + .appendToServiceTemplate(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); LOGGER.debug("<-createServiceTemplate: createdServiceTemplate={}", createdServiceTemplate); return createdServiceTemplate; @@ -102,6 +133,28 @@ public class AuthorativeToscaProvider { } /** + * Update a service template. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template to be updated. + * @return the TOSCA service template that was updated + * @throws PfModelException on errors updating the service template + */ + public ToscaServiceTemplate updateServiceTemplate(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + + synchronized (providerLockObject) { + LOGGER.debug("->updateServiceTemplate: serviceTemplate={}", serviceTemplate); + + ToscaServiceTemplate updatedServiceTemplate = new SimpleToscaProvider() + .appendToServiceTemplate(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + + LOGGER.debug("<-updateServiceTemplate: updatedServiceTemplate={}", updatedServiceTemplate); + return updatedServiceTemplate; + } + } + + /** * Delete a service template. * * @param dao the DAO to use to access the database @@ -111,16 +164,16 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors deleting the control loop */ public ToscaServiceTemplate deleteServiceTemplate(@NonNull final PfDao dao, @NonNull final String name, - @NonNull final String version) throws PfModelException { + @NonNull final String version) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->deleteServiceTemplate: name={}, version={}", name, version); ToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deleteServiceTemplate(dao).toAuthorative(); + new SimpleToscaProvider().deleteServiceTemplate(dao).toAuthorative(); LOGGER.debug("<-deleteServiceTemplate: name={}, version={}, deletedServiceTemplate={}", name, version, - deletedServiceTemplate); + deletedServiceTemplate); return deletedServiceTemplate; } } @@ -135,7 +188,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policy types */ public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version) - throws PfModelException { + throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version); @@ -159,7 +212,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policy types */ public List<ToscaPolicyType> getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version) - throws PfModelException { + throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version); @@ -168,7 +221,7 @@ public class AuthorativeToscaProvider { try { policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version) - .toAuthorative().getPolicyTypes().values()); + .toAuthorative().getPolicyTypes().values()); } catch (PfModelRuntimeException pfme) { return handlePfModelRuntimeException(pfme); } @@ -187,7 +240,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policy types */ public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao, - @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { + @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter); @@ -200,17 +253,17 @@ public class AuthorativeToscaProvider { if (CollectionUtils.isEmpty(filteredPolicyTypes)) { throw new PfModelRuntimeException(Response.Status.NOT_FOUND, - "policy types for filter " + filter.toString() + " do not exist"); + "policy types for filter " + filter.toString() + " do not exist"); } JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); for (ToscaPolicyType policyType : filteredPolicyTypes) { JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider - .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion()); + .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion()); filteredServiceTemplate = - ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); + ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); } ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); @@ -229,14 +282,14 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policy types */ public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull final PfDao dao, - @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { + @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { LOGGER.debug("->getFilteredPolicyTypeList: filter={}", filter); List<ToscaPolicyType> filteredPolicyTypeList = filter.filter(getPolicyTypeList(dao, null, null)); LOGGER.debug("<-getFilteredPolicyTypeList: filter={}, filteredPolicyTypeList={}", filter, - filteredPolicyTypeList); + filteredPolicyTypeList); return filteredPolicyTypeList; } @@ -250,13 +303,13 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors creating policy types */ public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao, - @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->createPolicyTypes: serviceTemplate={}", serviceTemplate); ToscaServiceTemplate createdServiceTemplate = new SimpleToscaProvider() - .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + .createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); LOGGER.debug("<-createPolicyTypes: createdServiceTemplate={}", createdServiceTemplate); return createdServiceTemplate; @@ -272,13 +325,13 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors updating policy types */ public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao, - @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->updatePolicyTypes: serviceTemplate={}", serviceTemplate); ToscaServiceTemplate updatedServiceTemplate = new SimpleToscaProvider() - .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + .updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); LOGGER.debug("<-updatePolicyTypes: updatedServiceTemplate={}", updatedServiceTemplate); return updatedServiceTemplate; @@ -295,16 +348,16 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors deleting policy types */ public ToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final String name, - @NonNull final String version) throws PfModelException { + @NonNull final String version) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->deletePolicyType: name={}, version={}", name, version); ToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative(); + new SimpleToscaProvider().deletePolicyType(dao, new PfConceptKey(name, version)).toAuthorative(); LOGGER.debug("<-deletePolicyType: name={}, version={}, deletedServiceTemplate={}", name, version, - deletedServiceTemplate); + deletedServiceTemplate); return deletedServiceTemplate; } } @@ -319,16 +372,16 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policies */ public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version) - throws PfModelException { + throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getPolicies: name={}, version={}", name, version); ToscaServiceTemplate gotServiceTemplate = - new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative(); + new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative(); LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTemplate={}", name, version, - gotServiceTemplate); + gotServiceTemplate); return gotServiceTemplate; } } @@ -343,7 +396,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policies */ public List<ToscaPolicy> getPolicyList(@NonNull final PfDao dao, final String name, final String version) - throws PfModelException { + throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getPolicyList: name={}, version={}", name, version); @@ -352,7 +405,7 @@ public class AuthorativeToscaProvider { try { policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative() - .getToscaTopologyTemplate().getPolicies()); + .getToscaTopologyTemplate().getPolicies()); } catch (PfModelRuntimeException pfme) { return handlePfModelRuntimeException(pfme); } @@ -371,7 +424,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policies */ public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) - throws PfModelException { + throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->getFilteredPolicies: filter={}", filter); @@ -379,25 +432,25 @@ public class AuthorativeToscaProvider { SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider(); final JpaToscaServiceTemplate dbServiceTemplate = - simpleToscaProvider.getPolicies(dao, filter.getName(), version); + simpleToscaProvider.getPolicies(dao, filter.getName(), version); List<ToscaPolicy> filteredPolicies = - dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList(); + dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList(); filteredPolicies = filter.filter(filteredPolicies); if (CollectionUtils.isEmpty(filteredPolicies)) { throw new PfModelRuntimeException(Response.Status.NOT_FOUND, - "policies for filter " + filter.toString() + " do not exist"); + "policies for filter " + filter.toString() + " do not exist"); } JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); for (ToscaPolicy policy : filteredPolicies) { - JpaToscaServiceTemplate cascadedServiceTemplate = - simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion()); + JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider + .getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion()); filteredServiceTemplate = - ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); + ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); } ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); @@ -416,7 +469,7 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors getting policies */ public List<ToscaPolicy> getFilteredPolicyList(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) - throws PfModelException { + throws PfModelException { LOGGER.debug("->getFilteredPolicyList: filter={}", filter); String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion(); @@ -436,13 +489,13 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors creating policies */ public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao, - @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->createPolicies: serviceTemplate={}", serviceTemplate); ToscaServiceTemplate createdServiceTemplate = new SimpleToscaProvider() - .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + .createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); LOGGER.debug("<-createPolicies: createdServiceTemplate={}", createdServiceTemplate); return createdServiceTemplate; @@ -458,13 +511,13 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors updating policies */ public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao, - @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->updatePolicies: serviceTemplate={}", serviceTemplate); ToscaServiceTemplate updatedServiceTemplate = new SimpleToscaProvider() - .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); + .updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)).toAuthorative(); LOGGER.debug("<-updatePolicies: updatedServiceTemplate={}", updatedServiceTemplate); return updatedServiceTemplate; @@ -481,16 +534,16 @@ public class AuthorativeToscaProvider { * @throws PfModelException on errors deleting policies */ public ToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final String name, - @NonNull final String version) throws PfModelException { + @NonNull final String version) throws PfModelException { synchronized (providerLockObject) { LOGGER.debug("->deletePolicy: name={}, version={}", name, version); ToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative(); + new SimpleToscaProvider().deletePolicy(dao, new PfConceptKey(name, version)).toAuthorative(); LOGGER.debug("<-deletePolicy: name={}, version={}, deletedServiceTemplate={}", name, version, - deletedServiceTemplate); + deletedServiceTemplate); return deletedServiceTemplate; } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java index 439ec6282..031fceb9a 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java @@ -47,13 +47,12 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class ToscaPolicyTypeFilterTest { - private static final String VERSION_100 = "1.0.0"; - - private static final String VERSION_000 = "0.0.0"; - // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class); + private static final String VERSION_100 = "1.0.0"; + private static final String VERSION_000 = "0.0.0"; + // @formatter:off private static final String[] policyTypeResourceNames = { "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml", @@ -151,11 +150,11 @@ public class ToscaPolicyTypeFilterTest { // got changed - perhaps we change this test to find a specific name // to test for vs an index which never remains consistent? // - //assertEquals("2.0.0", filteredList.get(18).getVersion()); + // assertEquals("2.0.0", filteredList.get(18).getVersion()); // // And now this index changes again?? // - //assertEquals(VERSION_100, filteredList.get(17).getVersion()); + // assertEquals(VERSION_100, filteredList.get(17).getVersion()); typeList.get(12).setVersion(VERSION_100); filteredList = filter.filter(typeList); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java new file mode 100644 index 000000000..ba72e621b --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.models.base.PfKey; + +/** + * Test of the {@link ToscaServiceTemplateFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaServiceTemplateFilterTest { + @Test + public void testNullList() { + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); + } + + @Test + public void testFilterNothing() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().build(); + + List<ToscaServiceTemplate> filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertTrue(filteredList.containsAll(serviceTemplates.getServiceTemplates())); + } + + @Test + public void testFilterLatestVersion() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = + ToscaServiceTemplateFilter.builder().version(ToscaServiceTemplateFilter.LATEST_VERSION).build(); + + List<ToscaServiceTemplate> filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(4, filteredList.size()); + assertEquals("0.0.0", filteredList.get(0).getVersion()); + assertEquals("1.2.8", filteredList.get(1).getVersion()); + assertEquals("1.2.3", filteredList.get(2).getVersion()); + assertEquals("1.8.3", filteredList.get(3).getVersion()); + + filter = ToscaServiceTemplateFilter.builder().version("1.2.3").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(3, filteredList.size()); + filter = ToscaServiceTemplateFilter.builder().version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(6, filteredList.size()); + + serviceTemplates.getServiceTemplates().get(12).setVersion("0.0.0"); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(7, filteredList.size()); + assertEquals(PfKey.NULL_KEY_VERSION, filteredList.get(0).getVersion()); + assertEquals(PfKey.NULL_KEY_VERSION, filteredList.get(6).getVersion()); + } + + @Test + public void testFilterNameVersion() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().name("name0").build(); + List<ToscaServiceTemplate> filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(13, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("not.found").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(0, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name(PfKey.NULL_KEY_NAME).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(2, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(6, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name(PfKey.NULL_KEY_NAME).version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(2, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("name2").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(1, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("name2").version("1.8.3").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(1, filteredList.size()); + assertEquals("name2", filteredList.get(0).getName()); + assertEquals("1.8.3", filteredList.get(0).getVersion()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java index 53e8311ee..dc0115486 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java @@ -114,14 +114,14 @@ public class AuthorativeToscaProviderGenericTest { @Test public void testCreateGetDelete() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getServiceTemplate(null, null, null); + new AuthorativeToscaProvider().getServiceTemplateList(null, null, null); }).hasMessageMatching(DAO_IS_NULL); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -130,14 +130,15 @@ public class AuthorativeToscaProviderGenericTest { assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getServiceTemplate(pfDao, null, null); + List<ToscaServiceTemplate> gotServiceTemplateList = + new AuthorativeToscaProvider().getServiceTemplateList(pfDao, null, null); - ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType gotPolicyType = gotServiceTemplateList.get(0).getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); List<ToscaPolicyType> gotPolicyTypeList = - new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001); + new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001); assertEquals(2, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); @@ -153,50 +154,49 @@ public class AuthorativeToscaProviderGenericTest { assertEquals(2, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - assertThatThrownBy( - () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001)) - .hasMessageContaining("Policy Framework DAO has not been initialized"); + assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, + VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized"); assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty()); ToscaServiceTemplate deletedServiceTemplate = - new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1"); + new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1"); assertEquals(2, deletedServiceTemplate.getPolicyTypes().size()); } @Test public void testNullParameters() throws Exception { - assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplate(null, null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplateList(null, null, null)) + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(pfDao, null)) - .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); + .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, new ToscaServiceTemplate())) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, "0.0.1")) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", "0.0.1")) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, null)) - .hasMessageMatching("^name is marked .*on.*ull but is null$"); + .hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, "0.0.1")) - .hasMessageMatching("^name is marked .*on.*ull but is null$"); + .hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", null)) - .hasMessageMatching("^version is marked .*on.*ull but is null$"); + .hasMessageMatching("^version is marked .*on.*ull but is null$"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index 7d900ab3a..f66a8e8a5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -43,6 +43,7 @@ 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.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -410,8 +411,8 @@ public class AuthorativeToscaProviderPolicyTest { @Test public void testEntityMaps() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = @@ -449,11 +450,19 @@ public class AuthorativeToscaProviderPolicyTest { assertThatThrownBy(() -> { createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap(); }).hasMessageContaining("list of map of entities contains more than one entity with key"); + + + ToscaDataType duplDataType = toscaServiceTemplatePolicyType.getDataTypes().values().iterator().next(); + toscaServiceTemplatePolicyType.getDataTypes().put("DuplicateDataType", duplDataType); + + assertThatThrownBy(() -> { + toscaServiceTemplatePolicyType.getDataTypesAsMap(); + }).hasMessageContaining("list of map of entities contains more than one entity with key"); } private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = diff --git a/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml b/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml new file mode 100644 index 000000000..d2aea5819 --- /dev/null +++ b/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml @@ -0,0 +1,185 @@ +service_templates: +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + policy_types: + onap.policies.Policytype0: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype0 + description: a policy type + properties: + policytype0.property0: + type: onap.datatypes.datatype0 + description: a property + required: true + data_types: + onap.datatypes.datatype0: + derived_from: tosca.datatypes.Root + properties: + domain0: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + policy_types: + onap.policies.Policytype1: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype1 + description: a policy type + properties: + policytype1.property1: + type: onap.datatypes.datatype1 + description: a property + required: true + data_types: + onap.datatypes.datatype1: + derived_from: tosca.datatypes.Root + properties: + domain1: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype2: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype2 + description: a policy type + properties: + policytype2.property2: + type: onap.datatypes.datatype2 + description: a property + required: true + data_types: + onap.datatypes.datatype2: + derived_from: tosca.datatypes.Root + properties: + domain2: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype3: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype3 + description: a policy type + properties: + policytype3.property3: + type: onap.datatypes.datatype3 + description: a property + required: true + data_types: + onap.datatypes.datatype3: + derived_from: tosca.datatypes.Root + properties: + domain3: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype4: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype4 + description: a policy type + properties: + policytype4.property4: + type: onap.datatypes.datatype4 + description: a property + required: true + data_types: + onap.datatypes.datatype4: + derived_from: tosca.datatypes.Root + properties: + domain4: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + vesion: 1.2.3 + policy_types: + onap.policies.Policytype5: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype5 + description: a policy type + properties: + policytype5.property5: + type: onap.datatypes.datatype5 + description: a property + required: true + data_types: + onap.datatypes.datatype5: + derived_from: tosca.datatypes.Root + properties: + domain5: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.3 + policy_types: + onap.policies.Policytype6: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype6 + description: a policy type + properties: + policytype6.property6: + type: onap.datatypes.datatype6 + description: a property + required: true + data_types: + onap.datatypes.datatype6: + derived_from: tosca.datatypes.Root + properties: + domain6: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.3 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.4 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.5 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.6 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.7 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name1 + version: 1.2.3 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name2 + version: 1.8.3 |