From 825612628f130b394f9ee1aa1ad2cca41b67bc7f Mon Sep 17 00:00:00 2001 From: nrpandya Date: Thu, 21 Nov 2019 11:51:18 -0600 Subject: Add template and tosca model entities and repositories Add Control loop template, tosca model and model policy properties hibernate entity classes and crud repositories Issue-ID: CLAMP-555 Change-Id: Ib7f07aca5ad2ddf5caff7c98ea9341bdc147e817 Signed-off-by: nrpandya --- src/main/java/org/onap/clamp/clds/Application.java | 8 +- .../onap/clamp/clds/ClampInUserAuditorAware.java | 46 +++ .../onap/clamp/clds/filter/ClampCadiFilter.java | 2 +- .../org/onap/clamp/clds/model/CldsDictionary.java | 159 -------- .../onap/clamp/clds/model/CldsDictionaryItem.java | 214 ----------- .../clamp/clds/tosca/ToscaYamlToJsonConvertor.java | 57 --- src/main/java/org/onap/clamp/loop/Loop.java | 25 +- src/main/java/org/onap/clamp/loop/LoopService.java | 28 +- .../java/org/onap/clamp/loop/LoopsRepository.java | 5 +- .../org/onap/clamp/loop/common/AuditEntity.java | 108 ++++++ src/main/java/org/onap/clamp/loop/log/LoopLog.java | 2 - .../org/onap/clamp/loop/log/LoopLogRepository.java | 4 +- .../java/org/onap/clamp/loop/service/Service.java | 18 +- .../clamp/loop/service/ServicesRepository.java | 31 ++ .../org/onap/clamp/loop/template/LoopTemplate.java | 268 ++++++++++++++ .../loop/template/LoopTemplatesRepository.java | 37 ++ .../clamp/loop/template/MicroServiceModel.java | 217 +++++++++++ .../template/MicroServiceModelsRepository.java | 31 ++ .../org/onap/clamp/loop/template/PolicyModel.java | 239 ++++++++++++ .../onap/clamp/loop/template/PolicyModelId.java | 93 +++++ .../loop/template/PolicyModelsRepository.java | 38 ++ .../clamp/loop/template/PolicyModelsService.java | 59 +++ .../loop/template/TemplateMicroServiceModel.java | 194 ++++++++++ .../loop/template/TemplateMicroServiceModelId.java | 102 +++++ .../policy/microservice/MicroServicePolicy.java | 64 +++- .../microservice/MicroServicePolicyRepository.java | 4 +- .../microservice/MicroServicePolicyService.java | 82 ++++ .../microservice/MicroservicePolicyService.java | 82 ---- .../policy/operational/OperationalPolicy.java | 35 ++ .../operational/OperationalPolicyRepository.java | 4 +- src/main/java/org/onap/clamp/tosca/Dictionary.java | 174 +++++++++ .../org/onap/clamp/tosca/DictionaryElement.java | 249 +++++++++++++ .../clamp/tosca/DictionaryElementsRepository.java | 32 ++ .../org/onap/clamp/tosca/DictionaryRepository.java | 38 ++ .../org/onap/clamp/util/SemanticVersioning.java | 76 ++++ src/main/resources/META-INF/resources/swagger.html | 411 ++++++++++++++++++--- .../org/onap/clamp/loop/CsarInstallerItCase.java | 6 +- .../onap/clamp/loop/LoopControllerTestItCase.java | 30 +- .../onap/clamp/loop/LoopLogServiceTestItCase.java | 4 +- .../onap/clamp/loop/LoopRepositoriesItCase.java | 137 ++++++- .../org/onap/clamp/loop/LoopServiceTestItCase.java | 84 ++--- .../java/org/onap/clamp/loop/LoopToJsonTest.java | 37 +- .../onap/clamp/loop/PolicyModelServiceItCase.java | 161 ++++++++ .../onap/clamp/util/SemanticVersioningTest.java | 71 ++++ 44 files changed, 3076 insertions(+), 690 deletions(-) create mode 100644 src/main/java/org/onap/clamp/clds/ClampInUserAuditorAware.java delete mode 100644 src/main/java/org/onap/clamp/clds/model/CldsDictionary.java delete mode 100644 src/main/java/org/onap/clamp/clds/model/CldsDictionaryItem.java create mode 100644 src/main/java/org/onap/clamp/loop/common/AuditEntity.java create mode 100644 src/main/java/org/onap/clamp/loop/service/ServicesRepository.java create mode 100644 src/main/java/org/onap/clamp/loop/template/LoopTemplate.java create mode 100644 src/main/java/org/onap/clamp/loop/template/LoopTemplatesRepository.java create mode 100644 src/main/java/org/onap/clamp/loop/template/MicroServiceModel.java create mode 100644 src/main/java/org/onap/clamp/loop/template/MicroServiceModelsRepository.java create mode 100644 src/main/java/org/onap/clamp/loop/template/PolicyModel.java create mode 100644 src/main/java/org/onap/clamp/loop/template/PolicyModelId.java create mode 100644 src/main/java/org/onap/clamp/loop/template/PolicyModelsRepository.java create mode 100644 src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java create mode 100644 src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModel.java create mode 100644 src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModelId.java create mode 100644 src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java delete mode 100644 src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java create mode 100644 src/main/java/org/onap/clamp/tosca/Dictionary.java create mode 100644 src/main/java/org/onap/clamp/tosca/DictionaryElement.java create mode 100644 src/main/java/org/onap/clamp/tosca/DictionaryElementsRepository.java create mode 100644 src/main/java/org/onap/clamp/tosca/DictionaryRepository.java create mode 100644 src/main/java/org/onap/clamp/util/SemanticVersioning.java create mode 100644 src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java create mode 100644 src/test/java/org/onap/clamp/util/SemanticVersioningTest.java (limited to 'src') diff --git a/src/main/java/org/onap/clamp/clds/Application.java b/src/main/java/org/onap/clamp/clds/Application.java index e41140f5c..63320d2fe 100644 --- a/src/main/java/org/onap/clamp/clds/Application.java +++ b/src/main/java/org/onap/clamp/clds/Application.java @@ -58,6 +58,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -65,13 +66,14 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @ComponentScan(basePackages = { "org.onap.clamp" }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class, - UserDetailsServiceAutoConfiguration.class }) + UserDetailsServiceAutoConfiguration.class }) @EnableJpaRepositories(basePackages = { "org.onap.clamp" }) @EntityScan(basePackages = { "org.onap.clamp" }) @EnableTransactionManagement @EnableConfigurationProperties @EnableAsync @EnableScheduling +@EnableJpaAuditing public class Application extends SpringBootServletInitializer { protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class); @@ -137,8 +139,6 @@ public class Application extends SpringBootServletInitializer { return tomcat; } - - private Connector createRedirectConnector(int redirectSecuredPort) { if (redirectSecuredPort <= 0) { eelfLogger.warn("HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1" @@ -159,7 +159,7 @@ public class Application extends SpringBootServletInitializer { if (env.getProperty("server.ssl.key-store") != null) { KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type")); - String password = PassDecoder.decode(env.getProperty("server.ssl.key-store-password"), + String password = PassDecoder.decode(env.getProperty("server.ssl.key-store-password"), env.getProperty("clamp.config.keyFile")); String keyStore = env.getProperty("server.ssl.key-store"); InputStream is = ResourceFileUtil.getResourceAsStream(keyStore.replaceAll("classpath:", "")); diff --git a/src/main/java/org/onap/clamp/clds/ClampInUserAuditorAware.java b/src/main/java/org/onap/clamp/clds/ClampInUserAuditorAware.java new file mode 100644 index 000000000..d18e7ebf3 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/ClampInUserAuditorAware.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds; + +import java.util.Optional; + +import org.springframework.data.domain.AuditorAware; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Component; + +@Component +public class ClampInUserAuditorAware implements AuditorAware { + + @Override + public Optional getCurrentAuditor() { + if (SecurityContextHolder.getContext().getAuthentication() != null + && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) { + return Optional.of(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()) + .getUsername()); + } + return Optional.of(""); + } + +} diff --git a/src/main/java/org/onap/clamp/clds/filter/ClampCadiFilter.java b/src/main/java/org/onap/clamp/clds/filter/ClampCadiFilter.java index 9e04bd084..3bbb8a0bc 100644 --- a/src/main/java/org/onap/clamp/clds/filter/ClampCadiFilter.java +++ b/src/main/java/org/onap/clamp/clds/filter/ClampCadiFilter.java @@ -153,7 +153,7 @@ public class ClampCadiFilter extends CadiFilter { URLDecoder.decode(certHeader, StandardCharsets.UTF_8.toString()).getBytes())); X509Certificate caCert = (X509Certificate) certificateFactory .generateCertificate(new ByteArrayInputStream( - ResourceFileUtil.getResourceAsString("clds/aaf/ssl/ca-certs.pem").getBytes())); + ResourceFileUtil.getResourceAsString("clds/aaf/ssl/ca-certs.pem").getBytes())); X509Certificate[] certifArray = ((X509Certificate[]) request .getAttribute("javax.servlet.request.X509Certificate")); diff --git a/src/main/java/org/onap/clamp/clds/model/CldsDictionary.java b/src/main/java/org/onap/clamp/clds/model/CldsDictionary.java deleted file mode 100644 index 35fbcec46..000000000 --- a/src/main/java/org/onap/clamp/clds/model/CldsDictionary.java +++ /dev/null @@ -1,159 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2018 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. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.clds.model; - -import com.google.gson.annotations.Expose; - -import java.util.ArrayList; -import java.util.List; - -/** - * Represents a CLDS Dictionary. - */ - -public class CldsDictionary { - - @Expose - private String dictionaryId; - @Expose - private String dictionaryName; - - @Expose - private String createdBy; - @Expose - private String updatedBy; - @Expose - private String lastUpdatedDate; - @Expose - private List cldsDictionaryItems = new ArrayList<>(); - - /** - * Get the dictionary ID. - * - * @return the dictionaryId - */ - public String getDictionaryId() { - return dictionaryId; - } - - /** - * Set the dictionary Id. - * - * @param dictionaryId the dictionaryId to set - */ - public void setDictionaryId(String dictionaryId) { - this.dictionaryId = dictionaryId; - } - - /** - * Get the dictionary name. - * - * @return the dictionaryName - */ - public String getDictionaryName() { - return dictionaryName; - } - - /** - * Set the dictionary name. - * - * @param dictionaryName the dictionaryName to set - */ - public void setDictionaryName(String dictionaryName) { - this.dictionaryName = dictionaryName; - } - - /** - * Get the createdBy info. - * - * @return the createdBy - */ - public String getCreatedBy() { - return createdBy; - } - - /** - * Set the createdBy info. - * - * @param createdBy the createdBy to set - */ - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - - /** - * Get the updatedBy info. - * - * @return the updatedBy - */ - public String getUpdatedBy() { - return updatedBy; - } - - /** - * Set the updatedBy info. - * - * @param updatedby the updatedBy to set - */ - public void setUpdatedBy(String updatedby) { - updatedBy = updatedby; - } - - /** - * Get the last updated date. - * - * @return the lastUpdatedDate - */ - public String getLastUpdatedDate() { - return lastUpdatedDate; - } - - /** - * Set the last updated date. - * - * @param lastUpdatedDate the lastUpdatedDate to set - */ - public void setLastUpdatedDate(String lastUpdatedDate) { - this.lastUpdatedDate = lastUpdatedDate; - } - - /** - * Get all the dictionary items. - * - * @return the cldsDictionaryItems - */ - public List getCldsDictionaryItems() { - return cldsDictionaryItems; - } - - /** - * Set the whole dictionary items. - * - * @param cldsDictionaryItems the cldsDictionaryItems to set - */ - public void setCldsDictionaryItems(List cldsDictionaryItems) { - this.cldsDictionaryItems = cldsDictionaryItems; - } - -} diff --git a/src/main/java/org/onap/clamp/clds/model/CldsDictionaryItem.java b/src/main/java/org/onap/clamp/clds/model/CldsDictionaryItem.java deleted file mode 100644 index 1b79bdfa9..000000000 --- a/src/main/java/org/onap/clamp/clds/model/CldsDictionaryItem.java +++ /dev/null @@ -1,214 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2018 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. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.clds.model; - -import com.google.gson.annotations.Expose; - -/** - * Represents a CLDS Dictionary Item. - */ -public class CldsDictionaryItem { - - @Expose - private String dictElementId; - @Expose - private String dictionaryId; - @Expose - private String dictElementName; - @Expose - private String dictElementShortName; - @Expose - private String dictElementDesc; - @Expose - private String dictElementType; - @Expose - private String createdBy; - @Expose - private String updatedBy; - @Expose - private String lastUpdatedDate; - - /** - * Get the dictionary element id. - * - * @return the dictElementId - */ - public String getDictElementId() { - return dictElementId; - } - - /** - * Set the dictionary element id. - * - * @param dictElementId the dictElementId to set - */ - public void setDictElementId(String dictElementId) { - this.dictElementId = dictElementId; - } - - /** - * Get the dictionary id. - * - * @return the dictionaryId - */ - public String getDictionaryId() { - return dictionaryId; - } - - /** - * Set the dictionary id. - * - * @param dictionaryId the dictionaryId to set - */ - public void setDictionaryId(String dictionaryId) { - this.dictionaryId = dictionaryId; - } - - /** - * Get the dictionary name. - * - * @return the dictElementName - */ - public String getDictElementName() { - return dictElementName; - } - - /** - * Set the dictionary name. - * - * @param dictElementName the dictElementName to set - */ - public void setDictElementName(String dictElementName) { - this.dictElementName = dictElementName; - } - - /** - * Get the dictionary element short name. - * - * @return the dictElementShortName - */ - public String getDictElementShortName() { - return dictElementShortName; - } - - /** - * Set the dictionary element short name. - * - * @param dictElementShortName the dictElementShortName to set - */ - public void setDictElementShortName(String dictElementShortName) { - this.dictElementShortName = dictElementShortName; - } - - /** - * Get the dictionary element description. - * - * @return the dictElementDesc - */ - public String getDictElementDesc() { - return dictElementDesc; - } - - /** - * Set the dictionary element description. - * - * @param dictElementDesc the dictElementDesc to set - */ - public void setDictElementDesc(String dictElementDesc) { - this.dictElementDesc = dictElementDesc; - } - - /** - * Get the dictionary element type. - * - * @return the dictElementType - */ - public String getDictElementType() { - return dictElementType; - } - - /** - * Set the dictionary element type. - * - * @param dictElementType the dictElementType to set - */ - public void setDictElementType(String dictElementType) { - this.dictElementType = dictElementType; - } - - /** - * Get the createdBy info. - * - * @return the createdBy - */ - public String getCreatedBy() { - return createdBy; - } - - /** - * Set the createdBy info. - * - * @param createdBy the createdBy to set - */ - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - - /** - * Get the updatedBy info. - * - * @return the updatedBy - */ - public String getUpdatedBy() { - return updatedBy; - } - - /** - * Set the updatedBy info. - * - * @param updatedby the updatedBy to set - */ - public void setUpdatedBy(String updatedby) { - updatedBy = updatedby; - } - - /** - * Get the last updated date. - * - * @return the lastUpdatedDate - */ - public String getLastUpdatedDate() { - return lastUpdatedDate; - } - - /** - * Set the last updated date. - * - * @param lastUpdatedDate the lastUpdatedDate to set - */ - public void setLastUpdatedDate(String lastUpdatedDate) { - this.lastUpdatedDate = lastUpdatedDate; - } - -} diff --git a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java index 43dd5f451..2e025ba7d 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java +++ b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java @@ -481,63 +481,6 @@ public class ToscaYamlToJsonConvertor { private void processDictionaryElements(JSONObject childObject, String dictionaryReference) { - /* - * if (dictionaryReference.contains("#")) { String[] dictionaryKeyArray = - * dictionaryReference - * .substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, - * dictionaryReference.length()) .split("#"); // We support only one # as of - * now. List cldsDictionaryElements = null; - * List subDictionaryElements = null; if (dictionaryKeyArray - * != null && dictionaryKeyArray.length == 2) { cldsDictionaryElements = - * getCldsDao().getDictionaryElements(dictionaryKeyArray[0], null, null); - * subDictionaryElements = - * getCldsDao().getDictionaryElements(dictionaryKeyArray[1], null, null); - * - * if (cldsDictionaryElements != null) { List subCldsDictionaryNames = - * subDictionaryElements.stream() - * .map(CldsDictionaryItem::getDictElementShortName).collect(Collectors.toList() - * ); JSONArray jsonArray = new JSONArray(); - * - * Optional.ofNullable(cldsDictionaryElements).get().stream().forEach(c -> { - * JSONObject jsonObject = new JSONObject(); - * jsonObject.put(JsonEditorSchemaConstants.TYPE, - * getJsonType(c.getDictElementType())); if (c.getDictElementType() != null && - * c.getDictElementType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) { - * jsonObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1); } - * jsonObject.put(JsonEditorSchemaConstants.ID, c.getDictElementName()); - * jsonObject.put(JsonEditorSchemaConstants.LABEL, c.getDictElementShortName()); - * jsonObject.put(JsonEditorSchemaConstants.OPERATORS, subCldsDictionaryNames); - * jsonArray.put(jsonObject); }); ; JSONObject filterObject = new JSONObject(); - * filterObject.put(JsonEditorSchemaConstants.FILTERS, jsonArray); - * - * childObject.put(JsonEditorSchemaConstants.TYPE, - * JsonEditorSchemaConstants.TYPE_QBLDR); // TO invoke validation on such - * parameters childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1); - * childObject.put(JsonEditorSchemaConstants.QSSCHEMA, filterObject); - * - * } } } else { String dictionaryKey = dictionaryReference.substring( - * dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, - * dictionaryReference.length()); if (dictionaryKey != null) { - * List cldsDictionaryElements = - * getCldsDao().getDictionaryElements(dictionaryKey, null, null); if - * (cldsDictionaryElements != null) { List cldsDictionaryNames = new - * ArrayList<>(); List cldsDictionaryFullNames = new ArrayList<>(); - * cldsDictionaryElements.stream().forEach(c -> { // Json type will be - * translated before Policy creation if (c.getDictElementType() != null && - * !c.getDictElementType().equalsIgnoreCase("json")) { - * cldsDictionaryFullNames.add(c.getDictElementName()); } - * cldsDictionaryNames.add(c.getDictElementShortName()); }); - * - * if (cldsDictionaryFullNames.size() > 0) { - * childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryFullNames); // - * Add Enum titles for generated translated values during JSON instance // - * generation JSONObject enumTitles = new JSONObject(); - * enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, cldsDictionaryNames); - * childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles); } else { - * childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryNames); } - * - * } } } - */ } private String getJsonType(String toscaType) { diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 531587a75..66046f02b 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -56,18 +56,20 @@ import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; +import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.components.external.DcaeComponent; import org.onap.clamp.loop.components.external.ExternalComponent; import org.onap.clamp.loop.components.external.PolicyComponent; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.service.Service; +import org.onap.clamp.loop.template.LoopTemplate; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; @Entity @Table(name = "loops") @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) -public class Loop implements Serializable { +public class Loop extends AuditEntity implements Serializable { /** * The serial version id. @@ -103,7 +105,7 @@ public class Loop implements Serializable { private JsonObject globalPropertiesJson; @Expose - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) @JoinColumn(name = "service_uuid") private Service modelService; @@ -120,19 +122,24 @@ public class Loop implements Serializable { private final Map components = new HashMap<>(); @Expose - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true) private Set operationalPolicies = new HashSet<>(); @Expose - @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER) @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id")) private Set microServicePolicies = new HashSet<>(); @Expose - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true) @SortNatural private SortedSet loopLogs = new TreeSet<>(); + @Expose + @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER) + @JoinColumn(name = "loop_template_name") + private LoopTemplate loopTemplate; + private void initializeExternalComponents() { this.addComponent(new PolicyComponent()); this.addComponent(new DcaeComponent()); @@ -280,6 +287,14 @@ public class Loop implements Serializable { this.components.put(component.getComponentName(), component); } + public LoopTemplate getLoopTemplate() { + return loopTemplate; + } + + public void setLoopTemplate(LoopTemplate loopTemplate) { + this.loopTemplate = loopTemplate; + } + /** * Generate the loop name. * diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index d1ab0e396..85e24cd00 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -31,29 +31,23 @@ import java.util.Set; import javax.persistence.EntityNotFoundException; import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.onap.clamp.policy.operational.OperationalPolicyService; -import org.springframework.stereotype.Component; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -@Component public class LoopService { - private final LoopsRepository loopsRepository; - private final MicroservicePolicyService microservicePolicyService; - private final OperationalPolicyService operationalPolicyService; - - /** - * Constructor. - */ - public LoopService(LoopsRepository loopsRepository, MicroservicePolicyService microservicePolicyService, - OperationalPolicyService operationalPolicyService) { - this.loopsRepository = loopsRepository; - this.microservicePolicyService = microservicePolicyService; - this.operationalPolicyService = operationalPolicyService; - } + @Autowired + private LoopsRepository loopsRepository; + + @Autowired + private MicroServicePolicyService microservicePolicyService; + + @Autowired + private OperationalPolicyService operationalPolicyService; Loop saveOrUpdateLoop(Loop loop) { return loopsRepository.save(loop); @@ -109,6 +103,6 @@ public class LoopService { private Loop findClosedLoopByName(String loopName) { return loopsRepository.findById(loopName) - .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName)); + .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName)); } } diff --git a/src/main/java/org/onap/clamp/loop/LoopsRepository.java b/src/main/java/org/onap/clamp/loop/LoopsRepository.java index 37c47622f..aaa49116f 100644 --- a/src/main/java/org/onap/clamp/loop/LoopsRepository.java +++ b/src/main/java/org/onap/clamp/loop/LoopsRepository.java @@ -24,12 +24,13 @@ package org.onap.clamp.loop; import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository -public interface LoopsRepository extends CrudRepository { +public interface LoopsRepository extends JpaRepository { @Query("SELECT loop.name FROM Loop as loop") List getAllLoopNames(); diff --git a/src/main/java/org/onap/clamp/loop/common/AuditEntity.java b/src/main/java/org/onap/clamp/loop/common/AuditEntity.java new file mode 100644 index 000000000..445f5b9e8 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/common/AuditEntity.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.common; + +import com.google.gson.annotations.Expose; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; + +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public class AuditEntity { + + @Expose + @CreatedDate + @Column(name = "created_timestamp", nullable = false, updatable = false) + private Instant createdDate; + + @Expose + @LastModifiedDate + @Column(name = "updated_timestamp", nullable = false) + private Instant updatedDate; + + @Expose + @LastModifiedBy + @Column(name = "updated_by") + private String updatedBy; + + @Expose + @CreatedBy + @Column(name = "created_by") + private String createdBy; + + public Instant getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(Instant createdDate) { + if (createdDate != null) { + this.createdDate = createdDate.truncatedTo(ChronoUnit.SECONDS); + } else { + this.createdDate = null; + } + } + + public Instant getUpdatedDate() { + return updatedDate; + } + + public void setUpdatedDate(Instant updatedDate) { + if (updatedDate != null) { + this.updatedDate = updatedDate.truncatedTo(ChronoUnit.SECONDS); + } else { + this.updatedDate = null; + } + } + + public String getUpdatedBy() { + return updatedBy; + } + + public void setUpdatedBy(String updatedBy) { + this.updatedBy = updatedBy; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public AuditEntity() { + } + +} diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLog.java b/src/main/java/org/onap/clamp/loop/log/LoopLog.java index 0e1153a32..e49598879 100644 --- a/src/main/java/org/onap/clamp/loop/log/LoopLog.java +++ b/src/main/java/org/onap/clamp/loop/log/LoopLog.java @@ -190,9 +190,7 @@ public class LoopLog implements Serializable, Comparable { if (arg0.getId() == null) { return -1; } - return arg0.getId().compareTo(this.getId()); - } } diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java b/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java index 103341fa5..0b3c34ec0 100644 --- a/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java +++ b/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java @@ -23,10 +23,10 @@ package org.onap.clamp.loop.log; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface LoopLogRepository extends CrudRepository { +public interface LoopLogRepository extends JpaRepository { } diff --git a/src/main/java/org/onap/clamp/loop/service/Service.java b/src/main/java/org/onap/clamp/loop/service/Service.java index 115f9f768..b74ee0b0d 100644 --- a/src/main/java/org/onap/clamp/loop/service/Service.java +++ b/src/main/java/org/onap/clamp/loop/service/Service.java @@ -39,9 +39,9 @@ import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; - @Entity @Table(name = "services") @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) @@ -76,13 +76,25 @@ public class Service implements Serializable { private JsonObject resourceDetails; /** - * Public constructor. + * Default constructor for serialization. */ public Service() { } /** - * Constructor. + * Constructor with string. + */ + public Service(String serviceDetails, String resourceDetails) { + JsonObject serviceDetailsJson = JsonUtils.GSON.fromJson(serviceDetails, JsonObject.class); + JsonObject resourceDetailsJson = JsonUtils.GSON.fromJson(resourceDetails, JsonObject.class); + this.name = serviceDetailsJson.get("name").getAsString(); + this.serviceUuid = serviceDetailsJson.get("UUID").getAsString(); + this.serviceDetails = serviceDetailsJson; + this.resourceDetails = resourceDetailsJson; + } + + /** + * Constructor with Json Object. */ public Service(JsonObject serviceDetails, JsonObject resourceDetails, String version) { this.name = serviceDetails.get("name").getAsString(); diff --git a/src/main/java/org/onap/clamp/loop/service/ServicesRepository.java b/src/main/java/org/onap/clamp/loop/service/ServicesRepository.java new file mode 100644 index 000000000..fe5ba8ed0 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/service/ServicesRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.service; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ServicesRepository extends JpaRepository { +} diff --git a/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java b/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java new file mode 100644 index 000000000..10367e77d --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/LoopTemplate.java @@ -0,0 +1,268 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.annotations.SortNatural; +import org.onap.clamp.loop.common.AuditEntity; +import org.onap.clamp.loop.service.Service; + +@Entity +@Table(name = "loop_templates") +public class LoopTemplate extends AuditEntity implements Serializable { + + /** + * The serial version id. + */ + private static final long serialVersionUID = -286522707701388642L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + /** + * This field is used when we have a blueprint defining all microservices. The + * other option would be to have independent blueprint for each microservices. + * In that case they are stored in each MicroServiceModel + */ + @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml") + private String blueprint; + + @Expose + @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation") + private String svgRepresentation; + + @Expose + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loopTemplate", orphanRemoval = true) + @SortNatural + private SortedSet microServiceModelUsed = new TreeSet<>(); + + @Expose + @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) + @JoinColumn(name = "service_uuid") + private Service modelService; + + @Expose + @Column(name = "maximum_instances_allowed") + private Integer maximumInstancesAllowed; + + /** + * name getter. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * blueprint getter. + * + * @return the blueprint + */ + public String getBlueprint() { + return blueprint; + } + + /** + * blueprint setter. + * + * @param blueprint the blueprint to set + */ + public void setBlueprint(String blueprint) { + this.blueprint = blueprint; + } + + /** + * svgRepresentation getter. + * + * @return the svgRepresentation + */ + public String getSvgRepresentation() { + return svgRepresentation; + } + + /** + * svgRepresentation setter. + * + * @param svgRepresentation the svgRepresentation to set + */ + public void setSvgRepresentation(String svgRepresentation) { + this.svgRepresentation = svgRepresentation; + } + + /** + * microServiceModelUsed getter. + * + * @return the microServiceModelUsed + */ + public SortedSet getMicroServiceModelUsed() { + return microServiceModelUsed; + } + + /** + * maximumInstancesAllowed getter. + * + * @return the maximumInstancesAllowed + */ + public Integer getMaximumInstancesAllowed() { + return maximumInstancesAllowed; + } + + /** + * maximumInstancesAllowed setter. + * + * @param maximumInstancesAllowed the maximumInstancesAllowed to set + */ + public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) { + this.maximumInstancesAllowed = maximumInstancesAllowed; + } + + /** + * Add a microService model to the current template, the microservice is added + * at the end of the list so the flowOrder is computed automatically. + * + * @param microServiceModel The microserviceModel to add + */ + public void addMicroServiceModel(MicroServiceModel microServiceModel) { + TemplateMicroServiceModel jointEntry = new TemplateMicroServiceModel(this, microServiceModel, + this.microServiceModelUsed.size()); + this.microServiceModelUsed.add(jointEntry); + microServiceModel.getUsedByLoopTemplates().add(jointEntry); + } + + /** + * Add a microService model to the current template, the flow order must be + * specified manually. + * + * @param microServiceModel The microserviceModel to add + * @param listPosition The position in the flow + */ + public void addMicroServiceModel(MicroServiceModel microServiceModel, Integer listPosition) { + TemplateMicroServiceModel jointEntry = new TemplateMicroServiceModel(this, microServiceModel, listPosition); + this.microServiceModelUsed.add(jointEntry); + microServiceModel.getUsedByLoopTemplates().add(jointEntry); + } + + /** + * modelService getter. + * + * @return the modelService + */ + public Service getModelService() { + return modelService; + } + + /** + * modelService setter. + * + * @param modelService the modelService to set + */ + public void setModelService(Service modelService) { + this.modelService = modelService; + } + + /** + * Default constructor for serialization. + */ + public LoopTemplate() { + + } + + /** + * Constructor. + * + * @param name The loop template name id + * @param blueprint The blueprint containing all microservices (legacy + * case) + * @param svgRepresentation The svg representation of that loop template + * @param maxInstancesAllowed The maximum number of instances that can be + * created from that template + * @param service The service associated to that loop template + */ + public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed, + Service service) { + this.name = name; + this.blueprint = blueprint; + this.svgRepresentation = svgRepresentation; + + this.maximumInstancesAllowed = maxInstancesAllowed; + this.modelService = service; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + LoopTemplate other = (LoopTemplate) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } +} diff --git a/src/main/java/org/onap/clamp/loop/template/LoopTemplatesRepository.java b/src/main/java/org/onap/clamp/loop/template/LoopTemplatesRepository.java new file mode 100644 index 000000000..07f304de7 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/LoopTemplatesRepository.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +@Repository +public interface LoopTemplatesRepository extends JpaRepository { + + @Query("SELECT looptemplate.name FROM LoopTemplate as looptemplate") + List getAllLoopTemplateNames(); +} diff --git a/src/main/java/org/onap/clamp/loop/template/MicroServiceModel.java b/src/main/java/org/onap/clamp/loop/template/MicroServiceModel.java new file mode 100644 index 000000000..1e2b140e7 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/MicroServiceModel.java @@ -0,0 +1,217 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.onap.clamp.loop.common.AuditEntity; + +/** + * This class represents a micro service model for a loop template. + */ + +@Entity +@Table(name = "micro_service_models") +public class MicroServiceModel extends AuditEntity implements Serializable { + + /** + * The serial version id. + */ + private static final long serialVersionUID = -286522707701376645L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + /** + * This variable is used to store the type mentioned in the micro-service + * blueprint. + */ + @Expose + @Column(nullable = false, name = "policy_type") + private String policyType; + + @Column(nullable = false, name = "blueprint_yaml") + private String blueprint; + + @Expose + @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) + @JoinColumns({ @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @JoinColumn(name = "policy_model_version", referencedColumnName = "version") }) + private PolicyModel policyModel; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "microServiceModel", orphanRemoval = true) + private Set usedByLoopTemplates = new HashSet<>(); + + /** + * policyModel getter. + * + * @return the policyModel + */ + public PolicyModel getPolicyModel() { + return policyModel; + } + + /** + * policyModel setter. + * + * @param policyModel the policyModel to set + */ + public void setPolicyModel(PolicyModel policyModel) { + this.policyModel = policyModel; + } + + /** + * name getter. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * policyType getter. + * + * @return the policyType + */ + public String getPolicyType() { + return policyType; + } + + /** + * policyType setter. + * + * @param policyType the policyType to set + */ + public void setPolicyType(String policyType) { + this.policyType = policyType; + } + + /** + * blueprint getter. + * + * @return the blueprint + */ + public String getBlueprint() { + return blueprint; + } + + /** + * blueprint setter. + * + * @param blueprint the blueprint to set + */ + public void setBlueprint(String blueprint) { + this.blueprint = blueprint; + } + + /** + * usedByLoopTemplates getter. + * + * @return the usedByLoopTemplates + */ + public Set getUsedByLoopTemplates() { + return usedByLoopTemplates; + } + + /** + * Default constructor for serialization. + */ + public MicroServiceModel() { + } + + /** + * Constructor. + * + * @param name The name id + * @param policyType The policy model type like + * onap.policies.controlloop.operational.common.Apex + * @param blueprint The blueprint defined for dcae that contains the policy + * type to use + * @param policyModel The policy model for the policy type mentioned here + */ + public MicroServiceModel(String name, String policyType, String blueprint, PolicyModel policyModel) { + this.name = name; + this.policyType = policyType; + this.blueprint = blueprint; + this.policyModel = policyModel; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + MicroServiceModel other = (MicroServiceModel) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/onap/clamp/loop/template/MicroServiceModelsRepository.java b/src/main/java/org/onap/clamp/loop/template/MicroServiceModelsRepository.java new file mode 100644 index 000000000..2b1870485 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/MicroServiceModelsRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MicroServiceModelsRepository extends JpaRepository { +} diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModel.java b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java new file mode 100644 index 000000000..e6580beed --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java @@ -0,0 +1,239 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; + +import org.onap.clamp.loop.common.AuditEntity; +import org.onap.clamp.util.SemanticVersioning; + +/** + * This class represents the policy model tosca revision that we can have to a + * specific microservice. + */ +@Entity +@Table(name = "policy_models") +@IdClass(PolicyModelId.class) +public class PolicyModel extends AuditEntity implements Serializable, Comparable { + + /** + * The serial version id. + */ + private static final long serialVersionUID = -286522705701376645L; + + /** + * This variable is used to store the type mentioned in the micro-service + * blueprint. + */ + @Id + @Expose + @Column(nullable = false, name = "policy_model_type") + private String policyModelType; + + /** + * Semantic versioning on policy side. + */ + @Id + @Expose + @Column(name = "version") + private String version; + + @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca") + private String policyModelTosca; + + @Expose + @Column(name = "policy_acronym") + private String policyAcronym; + + @Expose + @Column(name = "policy_variant") + private String policyVariant; + + /** + * policyModelTosca getter. + * + * @return the policyModelTosca + */ + public String getPolicyModelTosca() { + return policyModelTosca; + } + + /** + * policyModelTosca setter. + * + * @param policyModelTosca the policyModelTosca to set + */ + public void setPolicyModelTosca(String policyModelTosca) { + this.policyModelTosca = policyModelTosca; + } + + /** + * policyModelType getter. + * + * @return the modelType + */ + public String getPolicyModelType() { + return policyModelType; + } + + /** + * policyModelType setter. + * + * @param modelType the modelType to set + */ + public void setPolicyModelType(String modelType) { + this.policyModelType = modelType; + } + + /** + * version getter. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * version setter. + * + * @param version the version to set + */ + public void setVersion(String version) { + // Try to convert it before + this.version = version; + } + + /** + * policyAcronym getter. + * + * @return the policyAcronym value + */ + public String getPolicyAcronym() { + return policyAcronym; + } + + /** + * policyAcronym setter. + * + * @param policyAcronym The policyAcronym to set + */ + public void setPolicyAcronym(String policyAcronym) { + this.policyAcronym = policyAcronym; + } + + /** + * policyVariant getter. + * + * @return the policyVariant value + */ + public String getPolicyVariant() { + return policyVariant; + } + + /** + * policyVariant setter. + * + * @param policyVariant The policyVariant to set + */ + public void setPolicyVariant(String policyVariant) { + this.policyVariant = policyVariant; + } + + /** + * Default constructor for serialization. + */ + public PolicyModel() { + } + + /** + * Constructor. + * + * @param policyType The policyType (referenced in the blueprint) + * @param policyModelTosca The policy tosca model in yaml + * @param version the version like 1.0.0 + * @param policyAcronym Short policy name if it exists + * @param policyVariant Subtype for policy if it exists (could be used by UI) + */ + public PolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant) { + this.policyModelType = policyType; + this.policyModelTosca = policyModelTosca; + this.version = version; + this.policyAcronym = policyAcronym; + this.policyVariant = policyVariant; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + PolicyModel other = (PolicyModel) obj; + if (policyModelType == null) { + if (other.policyModelType != null) { + return false; + } + } else if (!policyModelType.equals(other.policyModelType)) { + return false; + } + if (version == null) { + if (other.version != null) { + return false; + } + } else if (!version.equals(other.version)) { + return false; + } + return true; + } + + @Override + public int compareTo(PolicyModel arg0) { + // Reverse it, so that by default we have the latest + return SemanticVersioning.compare(arg0.getVersion(), this.version); + } +} diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelId.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelId.java new file mode 100644 index 000000000..c4dd1933b --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelId.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +public class PolicyModelId implements Serializable { + + /** + * Serial Id. + */ + private static final long serialVersionUID = -2846526482064334745L; + + @Expose + private String policyModelType; + + @Expose + private String version; + + /** + * Default constructor for serialization. + */ + public PolicyModelId() { + + } + + /** + * Constructor. + */ + public PolicyModelId(String policyModelType, String version) { + this.policyModelType = policyModelType; + this.version = version; + } + + /** + * policyModelType getter. + * + * @return the policyModelType + */ + public String getPolicyModelType() { + return policyModelType; + } + + /** + * policyModelType setter. + * + * @param policyModelType the policyModelType to set + */ + public void setPolicyModelType(String policyModelType) { + this.policyModelType = policyModelType; + } + + /** + * version getter. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * version setter. + * + * @param version the version to set + */ + public void setVersion(String version) { + this.version = version; + } +} diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsRepository.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsRepository.java new file mode 100644 index 000000000..a76e386b5 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsRepository.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +@Repository +public interface PolicyModelsRepository extends JpaRepository { + @Query("SELECT policymodel.policyModelType FROM PolicyModel as policymodel") + List getAllPolicyModelType(); + + List findByPolicyModelType(String policyModelType); +} diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java new file mode 100644 index 000000000..8e22852a9 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class PolicyModelsService { + private final PolicyModelsRepository policyModelsRepository; + + @Autowired + public PolicyModelsService(PolicyModelsRepository policyModelrepo) { + policyModelsRepository = policyModelrepo; + } + + public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) { + return policyModelsRepository.save(policyModel); + } + + public List getAllPolicyModelTypes() { + return policyModelsRepository.getAllPolicyModelType(); + } + + public Iterable getAllPolicyModels() { + return policyModelsRepository.findAll(); + } + + public PolicyModel getPolicyModel(String type, String version) { + return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null); + } + + public Iterable getAllPolicyModelsByType(String type) { + return policyModelsRepository.findByPolicyModelType(type); + } +} diff --git a/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModel.java b/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModel.java new file mode 100644 index 000000000..7547c1f70 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModel.java @@ -0,0 +1,194 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; +import javax.persistence.Table; + +@Entity +@Table(name = "templates_microservicemodels") +public class TemplateMicroServiceModel implements Serializable, Comparable { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 5924989899078094245L; + + @EmbeddedId + private TemplateMicroServiceModelId templateMicroServiceModelId; + + @ManyToOne(fetch = FetchType.LAZY) + @MapsId("loopTemplateName") + @JoinColumn(name = "loop_template_name") + private LoopTemplate loopTemplate; + + @Expose + @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }) + @MapsId("microServiceModelName") + @JoinColumn(name = "micro_service_model_name") + private MicroServiceModel microServiceModel; + + @Expose + @Column(nullable = false, name = "flow_order") + private Integer flowOrder; + + /** + * Default constructor for serialization. + */ + public TemplateMicroServiceModel() { + + } + + /** + * Constructor. + * + * @param loopTemplate The loop template object + * @param microServiceModel The microServiceModel object + * @param flowOrder The position of the micro service in the flow + */ + public TemplateMicroServiceModel(LoopTemplate loopTemplate, MicroServiceModel microServiceModel, + Integer flowOrder) { + this.loopTemplate = loopTemplate; + this.microServiceModel = microServiceModel; + this.flowOrder = flowOrder; + this.templateMicroServiceModelId = new TemplateMicroServiceModelId(loopTemplate.getName(), + microServiceModel.getName()); + } + + /** + * loopTemplate getter. + * + * @return the loopTemplate + */ + public LoopTemplate getLoopTemplate() { + return loopTemplate; + } + + /** + * loopTemplate setter. + * + * @param loopTemplate the loopTemplate to set + */ + public void setLoopTemplate(LoopTemplate loopTemplate) { + this.loopTemplate = loopTemplate; + } + + /** + * microServiceModel getter. + * + * @return the microServiceModel + */ + public MicroServiceModel getMicroServiceModel() { + return microServiceModel; + } + + /** + * microServiceModel setter. + * + * @param microServiceModel the microServiceModel to set + */ + public void setMicroServiceModel(MicroServiceModel microServiceModel) { + this.microServiceModel = microServiceModel; + } + + /** + * flowOrder getter. + * + * @return the flowOrder + */ + public Integer getFlowOrder() { + return flowOrder; + } + + /** + * flowOrder setter. + * + * @param flowOrder the flowOrder to set + */ + public void setFlowOrder(Integer flowOrder) { + this.flowOrder = flowOrder; + } + + @Override + public int compareTo(TemplateMicroServiceModel arg0) { + // Reverse it, so that by default we have the latest + if (getFlowOrder() == null) { + return 1; + } + if (arg0.getFlowOrder() == null) { + return -1; + } + return arg0.getFlowOrder().compareTo(this.getFlowOrder()); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((loopTemplate == null) ? 0 : loopTemplate.hashCode()); + result = prime * result + ((microServiceModel == null) ? 0 : microServiceModel.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + TemplateMicroServiceModel other = (TemplateMicroServiceModel) obj; + if (loopTemplate == null) { + if (other.loopTemplate != null) { + return false; + } + } else if (!loopTemplate.equals(other.loopTemplate)) { + return false; + } + if (microServiceModel == null) { + if (other.microServiceModel != null) { + return false; + } + } else if (!microServiceModel.equals(other.microServiceModel)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModelId.java b/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModelId.java new file mode 100644 index 000000000..74c768974 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/template/TemplateMicroServiceModelId.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop.template; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class TemplateMicroServiceModelId implements Serializable { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 4089888115504914773L; + + @Expose + @Column(name = "loop_template_name") + private String loopTemplateName; + + @Expose + @Column(name = "micro_service_model_name") + private String microServiceModelName; + + /** + * Default constructor for serialization. + */ + public TemplateMicroServiceModelId() { + + } + + /** + * Constructor. + * + * @param loopTemplateName The loop template name id + * @param microServiceModelName THe micro Service name id + */ + public TemplateMicroServiceModelId(String loopTemplateName, String microServiceModelName) { + this.loopTemplateName = loopTemplateName; + this.microServiceModelName = microServiceModelName; + } + + /** + * loopTemplateName getter. + * + * @return the loopTemplateName + */ + public String getLoopTemplateName() { + return loopTemplateName; + } + + /** + * loopTemplateName setter. + * + * @param loopTemplateName the loopTemplateName to set + */ + public void setLoopTemplateName(String loopTemplateName) { + this.loopTemplateName = loopTemplateName; + } + + /** + * microServiceModelName getter. + * + * @return the microServiceModelName + */ + public String getMicroServiceModelName() { + return microServiceModelName; + } + + /** + * microServiceModelName setter. + * + * @param microServiceModelName the microServiceModelName to set + */ + public void setMicroServiceModelName(String microServiceModelName) { + this.microServiceModelName = microServiceModelName; + } +} diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 2943c39a2..98742d22b 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -40,7 +40,9 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; +import javax.persistence.JoinColumn; import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; @@ -52,13 +54,15 @@ import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.common.AuditEntity; +import org.onap.clamp.loop.template.MicroServiceModel; import org.onap.clamp.policy.Policy; import org.yaml.snakeyaml.Yaml; @Entity @Table(name = "micro_service_policies") @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) -public class MicroServicePolicy implements Serializable, Policy { +public class MicroServicePolicy extends AuditEntity implements Serializable, Policy { /** * The serial version ID. */ @@ -73,9 +77,17 @@ public class MicroServicePolicy implements Serializable, Policy { private String name; @Expose - @Column(nullable = false, name = "model_type") + @Column(nullable = false, name = "policy_model_type") private String modelType; + @Expose + @Column(name = "context") + private String context; + + @Expose + @Column(name = "device_type_scope") + private String deviceTypeScope; + @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "properties") @@ -96,6 +108,11 @@ public class MicroServicePolicy implements Serializable, Policy { @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER) private Set usedByLoops = new HashSet<>(); + @Expose + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "micro_service_model_id") + private MicroServiceModel microServiceModel; + public MicroServicePolicy() { // serialization } @@ -203,6 +220,49 @@ public class MicroServicePolicy implements Serializable, Policy { this.usedByLoops = usedBy; } + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + public String getDeviceTypeScope() { + return deviceTypeScope; + } + + public void setDeviceTypeScope(String deviceTypeScope) { + this.deviceTypeScope = deviceTypeScope; + } + + /** + * microServiceModel getter. + * + * @return the microServiceModel + */ + public MicroServiceModel getMicroServiceModel() { + return microServiceModel; + } + + /** + * microServiceModel setter. + * + * @param microServiceModel the microServiceModel to set + */ + public void setMicroServiceModel(MicroServiceModel microServiceModel) { + this.microServiceModel = microServiceModel; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java index f658aacc3..38b310ce8 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java @@ -23,10 +23,10 @@ package org.onap.clamp.policy.microservice; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -interface MicroServicePolicyRepository extends CrudRepository { +public interface MicroServicePolicyRepository extends JpaRepository { } diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java new file mode 100644 index 000000000..346cdf624 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.policy.microservice; + +import com.google.common.collect.Sets; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.onap.clamp.loop.Loop; +import org.onap.clamp.policy.PolicyService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class MicroServicePolicyService implements PolicyService { + + private final MicroServicePolicyRepository repository; + + @Autowired + public MicroServicePolicyService(MicroServicePolicyRepository repository) { + this.repository = repository; + } + + @Override + public Set updatePolicies(Loop loop, List newMicroservicePolicies) { + return newMicroservicePolicies.stream().map(policy -> getAndUpdateMicroServicePolicy(loop, policy)) + .collect(Collectors.toSet()); + } + + @Override + public boolean isExisting(String policyName) { + return repository.existsById(policyName); + } + + /** + * Get and update the MicroService policy properties. + * + * @param loop + * The loop + * @param policy + * The new MicroService policy + * @return The updated MicroService policy + */ + public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) { + return repository + .save(repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) + .orElse(new MicroServicePolicy(policy.getName(), policy.getModelType(), policy.getPolicyTosca(), + policy.getShared(), policy.getJsonRepresentation(), Sets.newHashSet(loop)))); + } + + private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy, + MicroServicePolicy newPolicy, Loop loop) { + oldPolicy.setProperties(newPolicy.getProperties()); + if (!oldPolicy.getUsedByLoops().contains(loop)) { + oldPolicy.getUsedByLoops().add(loop); + } + return oldPolicy; + } +} diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java deleted file mode 100644 index 59ddaa009..000000000 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java +++ /dev/null @@ -1,82 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2019 Nokia 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. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.policy.microservice; - -import com.google.common.collect.Sets; - -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import org.onap.clamp.loop.Loop; -import org.onap.clamp.policy.PolicyService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class MicroservicePolicyService implements PolicyService { - - private final MicroServicePolicyRepository repository; - - @Autowired - public MicroservicePolicyService(MicroServicePolicyRepository repository) { - this.repository = repository; - } - - @Override - public Set updatePolicies(Loop loop, List newMicroservicePolicies) { - return newMicroservicePolicies.stream().map(policy -> getAndUpdateMicroServicePolicy(loop, policy)) - .collect(Collectors.toSet()); - } - - @Override - public boolean isExisting(String policyName) { - return repository.existsById(policyName); - } - - /** - * Get and update the MicroService policy properties. - * - * @param loop - * The loop - * @param policy - * The new MicroService policy - * @return The updated MicroService policy - */ - public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) { - return repository - .save(repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) - .orElse(new MicroServicePolicy(policy.getName(), policy.getModelType(), policy.getPolicyTosca(), - policy.getShared(), policy.getJsonRepresentation(), Sets.newHashSet(loop)))); - } - - private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy, - MicroServicePolicy newPolicy, Loop loop) { - oldPolicy.setProperties(newPolicy.getProperties()); - if (!oldPolicy.getUsedByLoops().contains(loop)) { - oldPolicy.getUsedByLoops().add(loop); - } - return oldPolicy; - } -} diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index 14112694e..e8bf4a655 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -46,6 +46,7 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; @@ -55,6 +56,7 @@ import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.Policy; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -90,6 +92,12 @@ public class OperationalPolicy implements Serializable, Policy { @JoinColumn(name = "loop_id", nullable = false) private Loop loop; + @Expose + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumns({ @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @JoinColumn(name = "policy_model_version", referencedColumnName = "version") }) + private PolicyModel policyModel; + public OperationalPolicy() { // Serialization } @@ -137,6 +145,33 @@ public class OperationalPolicy implements Serializable, Policy { this.configurationsJson = configurationsJson; } + /** + * policyModel getter. + * + * @return the policyModel + */ + public PolicyModel getPolicyModel() { + return policyModel; + } + + /** + * policyModel setter. + * + * @param policyModel the policyModel to set + */ + public void setPolicyModel(PolicyModel policyModel) { + this.policyModel = policyModel; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + @Override public JsonObject getJsonRepresentation() { return jsonRepresentation; diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java index 97b183f72..c0a6e12cd 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java @@ -23,10 +23,10 @@ package org.onap.clamp.policy.operational; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -interface OperationalPolicyRepository extends CrudRepository { +public interface OperationalPolicyRepository extends JpaRepository { void deleteByName(String policyName); } diff --git a/src/main/java/org/onap/clamp/tosca/Dictionary.java b/src/main/java/org/onap/clamp/tosca/Dictionary.java new file mode 100644 index 000000000..7b4e513a2 --- /dev/null +++ b/src/main/java/org/onap/clamp/tosca/Dictionary.java @@ -0,0 +1,174 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.tosca; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.onap.clamp.loop.common.AuditEntity; + +/** + * Represents Dictionary. + */ + +@Entity +@Table(name = "dictionary") +public class Dictionary extends AuditEntity implements Serializable { + + /** + * The serial version id. + */ + private static final long serialVersionUID = -286522707701388645L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + @Expose + @Column(name = "dictionary_second_level") + private int secondLevelDictionary; + + @Expose + @Column(name = "dictionary_type") + private String subDictionaryType; + + @Expose + @OneToMany(mappedBy = "dictionary", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List dictionaryElements = new ArrayList<>(); + + /** + * name getter. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * secondLevelDictionary getter. + * + * @return the secondLevelDictionary + */ + public int getSecondLevelDictionary() { + return secondLevelDictionary; + } + + /** + * secondLevelDictionary setter. + * + * @param secondLevelDictionary the secondLevelDictionary to set + */ + public void setSecondLevelDictionary(int secondLevelDictionary) { + this.secondLevelDictionary = secondLevelDictionary; + } + + /** + * subDictionaryType getter. + * + * @return the subDictionaryType + */ + public String getSubDictionaryType() { + return subDictionaryType; + } + + /** + * subDictionaryType setter. + * + * @param subDictionaryType the subDictionaryType to set + */ + public void setSubDictionaryType(String subDictionaryType) { + this.subDictionaryType = subDictionaryType; + } + + /** + * dictionaryElements getter. + * + * @return the dictionaryElements + */ + public List getDictionaryElements() { + return dictionaryElements; + } + + /** + * dictionaryElements setter. + * + * @param dictionaryElements the dictionaryElements to set + */ + public void setDictionaryElements(List dictionaryElements) { + this.dictionaryElements = dictionaryElements; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Dictionary other = (Dictionary) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/onap/clamp/tosca/DictionaryElement.java b/src/main/java/org/onap/clamp/tosca/DictionaryElement.java new file mode 100644 index 000000000..e81885f3e --- /dev/null +++ b/src/main/java/org/onap/clamp/tosca/DictionaryElement.java @@ -0,0 +1,249 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.tosca; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.onap.clamp.loop.common.AuditEntity; + +/** + * Represents a Dictionary Item. + */ +@Entity +@Table(name = "dictionary_elements") +public class DictionaryElement extends AuditEntity implements Serializable { + + /** + * The serial version id. + */ + private static final long serialVersionUID = -286522707701388644L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + @Expose + @Column(nullable = false, name = "short_name", unique = true) + private String shortName; + + @Expose + @Column(name = "description") + private String description; + + @Expose + @Column(nullable = false, name = "type") + private String type; + + @Column(name = "subdictionary_id", nullable = false) + @Expose + private String subDictionary; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "dictionary_id") + private Dictionary dictionary; + + /** + * name getter. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * shortName getter. + * + * @return the shortName + */ + public String getShortName() { + return shortName; + } + + /** + * shortName setter. + * + * @param shortName the shortName to set + */ + public void setShortName(String shortName) { + this.shortName = shortName; + } + + /** + * description getter. + * + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * description setter. + * + * @param description the description to set + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * type getter. + * + * @return the type + */ + public String getType() { + return type; + } + + /** + * type setter. + * + * @param type the type to set + */ + public void setType(String type) { + this.type = type; + } + + /** + * subDictionary getter. + * + * @return the subDictionary + */ + public String getSubDictionary() { + return subDictionary; + } + + /** + * subDictionary setter. + * + * @param subDictionary the subDictionary to set + */ + public void setSubDictionary(String subDictionary) { + this.subDictionary = subDictionary; + } + + /** + * dictionary getter. + * + * @return the dictionary + */ + public Dictionary getDictionary() { + return dictionary; + } + + /** + * dictionary setter. + * + * @param dictionary the dictionary to set + */ + public void setDictionary(Dictionary dictionary) { + this.dictionary = dictionary; + } + + /** + * Default Constructor. + */ + public DictionaryElement() { + } + + /** + * Constructor. + * + * @param name The Dictionary element name + * @param shortName The short name + * @param description The description + * @param type The type of element + * @param subDictionary The sub type + * @param dictionary The parent dictionary + */ + public DictionaryElement(String name, String shortName, String description, String type, String subDictionary, + Dictionary dictionary) { + this.name = name; + this.shortName = shortName; + this.description = description; + this.type = type; + this.subDictionary = subDictionary; + this.dictionary = dictionary; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((dictionary == null) ? 0 : dictionary.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + DictionaryElement other = (DictionaryElement) obj; + if (dictionary == null) { + if (other.dictionary != null) { + return false; + } + } else if (!dictionary.equals(other.dictionary)) { + return false; + } + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } + +} diff --git a/src/main/java/org/onap/clamp/tosca/DictionaryElementsRepository.java b/src/main/java/org/onap/clamp/tosca/DictionaryElementsRepository.java new file mode 100644 index 000000000..96cb2e35e --- /dev/null +++ b/src/main/java/org/onap/clamp/tosca/DictionaryElementsRepository.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.tosca; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface DictionaryElementsRepository extends JpaRepository { + +} diff --git a/src/main/java/org/onap/clamp/tosca/DictionaryRepository.java b/src/main/java/org/onap/clamp/tosca/DictionaryRepository.java new file mode 100644 index 000000000..2a087b6d9 --- /dev/null +++ b/src/main/java/org/onap/clamp/tosca/DictionaryRepository.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.tosca; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +@Repository +public interface DictionaryRepository extends JpaRepository { + + @Query("SELECT dict.name FROM Dictionary as dict") + List getAllDictionaryNames(); + +} diff --git a/src/main/java/org/onap/clamp/util/SemanticVersioning.java b/src/main/java/org/onap/clamp/util/SemanticVersioning.java new file mode 100644 index 000000000..bf1529c2c --- /dev/null +++ b/src/main/java/org/onap/clamp/util/SemanticVersioning.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.util; + +/** + * This class is the base class for object that requires semantic versioning. + * ... This class supports also a.b.c.d... etc ... as a version. + * + * + */ +public class SemanticVersioning { + public static final int BEFORE = -1; + public static final int EQUAL = 0; + public static final int AFTER = 1; + + /** + * The compare method that compare arg0 to arg1. + * + * @param arg0 A version in string for semantice versioning (a.b.c.d...) + * @param arg1 A version in string for semantice versioning (a.b.c.d...) + * @return objects (arg0, arg1) given as parameters. It returns the value: 0: if + * (arg0==arg1) -1: if (arg0 < arg1) 1: if (arg0 > arg1) + */ + public static int compare(String arg0, String arg1) { + + if (arg0 == null && arg1 == null) { + return EQUAL; + } + if (arg0 == null) { + return BEFORE; + } + if (arg1 == null) { + return AFTER; + } + String[] arg0Array = arg0.split("\\."); + String[] arg1Array = arg1.split("\\."); + + int smalestStringLength = Math.min(arg0Array.length, arg1Array.length); + + for (int currentVersionIndex = 0; currentVersionIndex < smalestStringLength; ++currentVersionIndex) { + if (Integer.parseInt(arg0Array[currentVersionIndex]) < Integer.parseInt(arg1Array[currentVersionIndex])) { + return BEFORE; + } else if (Integer.parseInt(arg0Array[currentVersionIndex]) > Integer + .parseInt(arg1Array[currentVersionIndex])) { + return AFTER; + } + // equals, so do not return anything, continue + } + if (arg0Array.length == arg1Array.length) { + return EQUAL; + } else { + return Integer.compare(arg0Array.length, arg1Array.length); + } + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/swagger.html b/src/main/resources/META-INF/resources/swagger.html index b20d0c26c..66024470a 100644 --- a/src/main/resources/META-INF/resources/swagger.html +++ b/src/main/resources/META-INF/resources/swagger.html @@ -444,86 +444,86 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
  • 2. Paths @@ -583,13 +588,13 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b

    1.1. Version information

    -

    Version : 4.1.2-SNAPSHOT

    +

    Version : 4.2.0-SNAPSHOT

    1.2. URI scheme

    -

    Host : localhost:34219
    +

    Host : localhost:33953
    BasePath : /restservices/clds/
    Schemes : HTTP

    @@ -600,7 +605,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b

    2. Paths

    -

    2.1. GET /v1/clds/cldsInfo

    +

    2.1. GET /v1/clds/cldsInfo

    2.1.1. Responses

    @@ -637,7 +642,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -674,7 +679,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -708,7 +713,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -757,7 +762,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -819,7 +824,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -856,7 +861,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -918,7 +923,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -980,7 +985,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1042,7 +1047,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1104,7 +1109,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1166,7 +1171,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1228,7 +1233,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1306,7 +1311,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1384,7 +1389,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -1462,7 +1467,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    @@ -2184,6 +2189,16 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b + + + + + + + + @@ -2214,14 +2229,19 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b + + + + - - + + + + + + + + +

    < string, ExternalComponent > map

    createdBy
    +optional

    string

    createdDate
    +optional

    integer (int64)

    dcaeBlueprintId
    optional

    string

    < LoopLog > array

    loopTemplate
    +optional

    LoopTemplate

    microServicePolicies
    optional

    < MicroServicePolicy > array

    modelPropertiesJson
    +

    modelService
    optional

    JsonObject

    Service

    name
    @@ -2238,6 +2258,16 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b optional

    string

    updatedBy
    +optional

    string

    updatedDate
    +optional

    integer (int64)

    @@ -2289,7 +2319,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    -

    3.11. MicroServicePolicy

    +

    3.11. LoopTemplate

    @@ -2303,11 +2333,165 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    blueprint
    +optional

    string

    createdBy
    +optional

    string

    createdDate
    +optional

    integer (int64)

    maximumInstancesAllowed
    +optional

    integer (int32)

    microServiceModelUsed
    +optional

    < TemplateMicroServiceModel > array

    modelService
    +optional

    Service

    name
    +optional

    string

    svgRepresentation
    +optional

    string

    updatedBy
    +optional

    string

    updatedDate
    +optional

    integer (int64)

    +
    +
    +

    3.12. MicroServiceModel

    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameSchema

    blueprint
    +optional

    string

    createdBy
    +optional

    string

    createdDate
    +optional

    integer (int64)

    name
    +optional

    string

    policyModel
    +optional

    PolicyModel

    policyType
    +optional

    string

    updatedBy
    +optional

    string

    updatedDate
    +optional

    integer (int64)

    usedByLoopTemplates
    +optional

    < TemplateMicroServiceModel > array

    +
    +
    +

    3.13. MicroServicePolicy

    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2333,6 +2517,16 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b + + + + + + + + @@ -2341,13 +2535,13 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    NameSchema

    context
    +optional

    string

    createdBy
    +optional

    string

    createdDate
    +optional

    integer (int64)

    deviceTypeScope
    +optional

    string

    jsonRepresentation
    optional

    JsonObject

    microServiceModel
    +optional

    MicroServiceModel

    modelType
    optional

    string

    boolean

    updatedBy
    +optional

    string

    updatedDate
    +optional

    integer (int64)

    usedByLoops
    optional

    < Loop > array

    -

    3.12. Number

    +

    3.14. Number

    Type : object

    -

    3.13. OperationalPolicy

    +

    3.15. OperationalPolicy

    @@ -2380,6 +2574,137 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b optional

    + + + + + +

    string

    policyModel
    +optional

    PolicyModel

    +
    +
    +

    3.16. PolicyModel

    + ++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameSchema

    createdBy
    +optional

    string

    createdDate
    +optional

    integer (int64)

    policyAcronym
    +optional

    string

    policyModelTosca
    +optional

    string

    policyModelType
    +optional

    string

    policyVariant
    +optional

    string

    updatedBy
    +optional

    string

    updatedDate
    +optional

    integer (int64)

    version
    +optional

    string

    +
    +
    +

    3.17. Service

    + ++++ + + + + + + + + + + + + + + + + + + + + +
    NameSchema

    resourceDetails
    +optional

    JsonObject

    serviceDetails
    +optional

    JsonObject

    serviceUuid
    +optional

    string

    +
    +
    +

    3.18. TemplateMicroServiceModel

    + ++++ + + + + + + + + + + + + + + + + + + +
    NameSchema

    flowOrder
    +optional

    integer (int32)

    loopTemplate
    +optional

    LoopTemplate

    microServiceModel
    +optional

    MicroServiceModel

    @@ -2388,7 +2713,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
    diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java index df952aa16..2ebea7b17 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -63,7 +63,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Rollback; +import org.springframework.test.annotation.Commit; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @@ -183,7 +183,7 @@ public class CsarInstallerItCase { @Test @Transactional - @Rollback(value = false) + @Commit public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException, JSONException, InterruptedException { String generatedName = RandomStringUtils.randomAlphanumeric(5); @@ -209,7 +209,7 @@ public class CsarInstallerItCase { assertThat(loop.getOperationalPolicies()).hasSize(1); assertThat(loop.getModelService().getServiceUuid()).isEqualTo("63cac700-ab9a-4115-a74f-7eac85e3fce0"); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), - JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService()), true); + JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService()), true); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/service-details.json"), JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService().getServiceDetails()), true); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/resource-details.json"), diff --git a/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java index 67ae985c7..a41b5c251 100644 --- a/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java @@ -32,18 +32,15 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.util.Set; + import javax.transaction.Transactional; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.clamp.clds.Application; import org.onap.clamp.clds.util.JsonUtils; - import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -63,7 +60,7 @@ public class LoopControllerTestItCase { LoopsRepository loopsRepository; @Autowired - MicroservicePolicyService microServicePolicyService; + MicroServicePolicyService microServicePolicyService; @Autowired LoopController loopController; @@ -78,18 +75,10 @@ public class LoopControllerTestItCase { return new Loop(loopName, loopBlueprint, loopSvg); } - @Before - public void setUp() { - saveTestLoopToDb(); - } - - @After - public void tearDown() { - loopsRepository.deleteAll(); - } - @Test + @Transactional public void testUpdateOperationalPolicies() { + saveTestLoopToDb(); String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"," + "\"configurationsJson\":{\"guard_policies\":{}," + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\"," @@ -113,6 +102,7 @@ public class LoopControllerTestItCase { @Test @Transactional public void testUpdateGlobalProperties() { + saveTestLoopToDb(); String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\"," + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router" + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap" @@ -134,9 +124,10 @@ public class LoopControllerTestItCase { @Test @Transactional public void testUpdateMicroservicePolicy() { + saveTestLoopToDb(); MicroServicePolicy policy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy); assertThat(microServicePolicyService.isExisting("policyName")).isTrue(); } @@ -144,7 +135,8 @@ public class LoopControllerTestItCase { @Test @Transactional public void testGetSvgRepresentation() { - String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME); + saveTestLoopToDb(); + String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME); assertThat(svgRepresentation).isEqualTo("representation"); } } \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java index 57b2cef61..c172a9a07 100644 --- a/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java @@ -27,11 +27,11 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.gson.JsonObject; import java.util.Set; + import javax.transaction.Transactional; import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.clamp.clds.Application; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.log.LogType; @@ -77,10 +77,10 @@ public class LoopLogServiceTestItCase { assertThat(loopLogs).hasSize(1); LoopLog loopLog = loopLogs.iterator().next(); assertThat(loopLog.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE); - loopsRepository.deleteAll(); } @Test + @Transactional public void testLoopLog() { LoopLog log = new LoopLog(); Long id = Long.valueOf(100); diff --git a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java index 78e0d2e3d..44feaebd8 100644 --- a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java @@ -40,8 +40,17 @@ import org.onap.clamp.clds.Application; import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.log.LoopLogRepository; +import org.onap.clamp.loop.service.Service; +import org.onap.clamp.loop.service.ServicesRepository; +import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.loop.template.LoopTemplatesRepository; +import org.onap.clamp.loop.template.MicroServiceModel; +import org.onap.clamp.loop.template.MicroServiceModelsRepository; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; @@ -59,7 +68,7 @@ public class LoopRepositoriesItCase { private LoopsRepository loopRepository; @Autowired - private MicroservicePolicyService microServicePolicyService; + private MicroServicePolicyService microServicePolicyService; @Autowired private OperationalPolicyService operationalPolicyService; @@ -67,12 +76,47 @@ public class LoopRepositoriesItCase { @Autowired private LoopLogRepository loopLogRepository; + @Autowired + private LoopTemplatesRepository loopTemplateRepository; + + @Autowired + private MicroServiceModelsRepository microServiceModelsRepository; + + @Autowired + private PolicyModelsRepository policyModelsRepository; + + @Autowired + private ServicesRepository servicesRepository; + + private Service getService(String serviceDetails, String resourceDetails) { + return new Service(serviceDetails, resourceDetails); + } + private OperationalPolicy getOperationalPolicy(String configJson, String name) { return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class)); } + private MicroServiceModel getMicroServiceModel(String yaml, String name, String policyType, String createdBy, + PolicyModel policyModel) { + MicroServiceModel model = new MicroServiceModel(name, policyType, yaml, policyModel); + return model; + } + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant, String createdBy) { + return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant); + } + + private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy, + Integer maxInstancesAllowed) { + LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null); + template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1", "org.onap.policy.drools", createdBy, + getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1", createdBy))); + return template; + } + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { Loop loop = new Loop(); loop.setName(name); loop.setSvgRepresentation(svgRepresentation); @@ -82,13 +126,14 @@ public class LoopRepositoriesItCase { loop.setDcaeDeploymentId(dcaeId); loop.setDcaeDeploymentStatusUrl(dcaeUrl); loop.setDcaeBlueprintId(dcaeBlueprintId); + loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1)); return loop; } private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation, - String policyTosca, String jsonProperties, boolean shared) { + String policyTosca, String jsonProperties, boolean shared) { MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared, - gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); return microService; } @@ -100,52 +145,108 @@ public class LoopRepositoriesItCase { @Test @Transactional public void crudTest() { + // Setup Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", - "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", - "{\"param1\":\"value1\"}", true); + "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", + "{\"param1\":\"value1\"}", true); loopTest.addMicroServicePolicy(microServicePolicy); LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest); loopTest.addLog(loopLog); + Service service = getService( + "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}"); + loopTest.setModelService(service); - // Attemp to save into the database the entire loop + // Attempt to save into the database the entire loop Loop loopInDb = loopRepository.save(loopTest); assertThat(loopInDb).isNotNull(); + assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull(); + assertThat(loopInDb.getCreatedDate()).isNotNull(); + assertThat(loopInDb.getUpdatedDate()).isNotNull(); + assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate()); assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); - // Now set the ID in the previous model so that we can compare the objects + // Autogen id so now set the ID in the previous model so that we can compare the + // objects loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId()); - assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components"); + assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate", + "createdBy", "updatedBy"); assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true); assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true); assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true); assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true); + assertThat(microServiceModelsRepository.existsById( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName())) + .isEqualTo(true); + assertThat(policyModelsRepository.existsById(new PolicyModelId( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getPolicyModelType(), + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getVersion()))).isEqualTo(true); // Now attempt to read from database Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); - assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components"); + assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate", + "createdBy", "updatedBy"); + assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate", + "createdBy", "updatedBy"); assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) - .isEqualToComparingFieldByField(opPolicy); + .isEqualToComparingFieldByField(opPolicy); assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) - .isEqualToComparingFieldByField(microServicePolicy); + .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy", + "updatedBy"); // Attempt an update ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); - loopRepository.save(loopInDbRetrieved); - Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); + loopInDbRetrieved.setBlueprint("yaml2"); + Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved); + // Loop loopInDbRetrievedUpdated = + // loopRepository.findById(loopTest.getName()).get(); + assertThat(loopInDbRetrievedUpdated.getBlueprint()).isEqualTo("yaml2"); assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) - .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + // UpdatedDate should have been changed + assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate()); + // createdDate should have NOT been changed + assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate()); + // other audit are the same + assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo(""); + assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo(""); // Attempt to delete the object and check it has well been cascaded + loopRepository.delete(loopInDbRetrieved); assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false); assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false); - assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(false); + assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true); assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true); + assertThat(microServiceModelsRepository.existsById( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName())) + .isEqualTo(true); + + assertThat(policyModelsRepository.existsById(new PolicyModelId( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getPolicyModelType(), + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getVersion()))).isEqualTo(true); + + // Cleanup + // microServiceModelsRepository + // .delete(loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel()); + // + // policyModelsRepository.delete( + // loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()); + // loopTemplateRepository.delete(loopInDb.getLoopTemplate()); + // servicesRepository.delete(service); } } diff --git a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java index 28a92e371..d19c8a808 100644 --- a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java @@ -33,7 +33,6 @@ import java.util.stream.Collectors; import javax.transaction.Transactional; import org.assertj.core.util.Lists; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.Application; @@ -42,7 +41,7 @@ import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.log.LoopLogService; import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; @@ -63,7 +62,7 @@ public class LoopServiceTestItCase { LoopsRepository loopsRepository; @Autowired - MicroservicePolicyService microServicePolicyService; + MicroServicePolicyService microServicePolicyService; @Autowired OperationalPolicyService operationalPolicyService; @@ -71,11 +70,6 @@ public class LoopServiceTestItCase { @Autowired LoopLogService loopLogService; - @After - public void tearDown() { - loopsRepository.deleteAll(); - } - @Test @Transactional public void shouldCreateEmptyLoop() { @@ -96,7 +90,7 @@ public class LoopServiceTestItCase { assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint); assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg); assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString()) - .isEqualTo("testValue"); + .isEqualTo("testValue"); } @Test @@ -105,11 +99,11 @@ public class LoopServiceTestItCase { // given saveTestLoopToDb(); OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); // when Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(operationalPolicy)); + Lists.newArrayList(operationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -128,20 +122,20 @@ public class LoopServiceTestItCase { // given saveTestLoopToDb(); MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); // when Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(microServicePolicy)); + Lists.newArrayList(microServicePolicy)); // then assertThat(actualLoop).isNotNull(); assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); Set savedPolicies = actualLoop.getMicroServicePolicies(); assertThat(savedPolicies).hasSize(1); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactly(microServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactly(microServicePolicy); assertThat(savedPolicies).extracting("usedByLoops").hasSize(1); } @@ -153,16 +147,16 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, JsonUtils.GSON.fromJson("{}", JsonObject.class), - null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, + JsonUtils.GSON.fromJson("{}", JsonObject.class), null); // when firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class)); Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); + Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); // then assertThat(actualLoop).isNotNull(); @@ -171,8 +165,8 @@ public class LoopServiceTestItCase { assertThat(savedPolicies).hasSize(2); assertThat(savedPolicies).contains(firstMicroServicePolicy); assertThat(savedPolicies).contains(secondMicroServicePolicy); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); } @@ -189,24 +183,24 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", - "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca", - true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null); + true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null); // when Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(secondMicroServicePolicy)); + Lists.newArrayList(secondMicroServicePolicy)); // then assertThat(actualLoop).isNotNull(); assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); Set savedPolicies = actualLoop.getMicroServicePolicies(); assertThat(savedPolicies).hasSize(1); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactly(secondMicroServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy); } @@ -219,16 +213,16 @@ public class LoopServiceTestItCase { JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class); OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null, - newJsonConfiguration); + newJsonConfiguration); // when firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration); Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); + Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -236,9 +230,9 @@ public class LoopServiceTestItCase { Set savedPolicies = actualLoop.getOperationalPolicies(); assertThat(savedPolicies).hasSize(2); assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") - .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); + .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); Set policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop) - .map(Loop::getName).collect(Collectors.toSet()); + .map(Loop::getName).collect(Collectors.toSet()); assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME); } @@ -249,15 +243,15 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null, - JsonUtils.GSON.fromJson("{}", JsonObject.class)); + JsonUtils.GSON.fromJson("{}", JsonObject.class)); // when Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(secondOperationalPolicy)); + Lists.newArrayList(secondOperationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -300,21 +294,21 @@ public class LoopServiceTestItCase { loop = loopService.saveOrUpdateLoop(loop); // Add op policy OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy)); // Add Micro service policy MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy)); // Verify it's there assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull(); loopService.deleteLoop(EXAMPLE_LOOP_NAME); - // Verify it's well deleted and has been cascaded + // Verify it's well deleted and has been cascaded, except for Microservice assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull(); - assertThat(microServicePolicyService.isExisting("microPolicy")).isFalse(); + assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue(); assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse(); assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse(); } @@ -334,8 +328,8 @@ public class LoopServiceTestItCase { public void testUpdateDcaeDeploymentFields() { saveTestLoopToDb(); Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME); - loopService.updateDcaeDeploymentFields(loop,"CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85", - "https4://deployment-handler.onap:8443"); + loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85", + "https4://deployment-handler.onap:8443"); loop = loopService.getLoop(EXAMPLE_LOOP_NAME); assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85"); assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443"); @@ -347,8 +341,8 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); assertThat(microServicePolicyService.isExisting("policyName")).isFalse(); MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy); assertThat(microServicePolicyService.isExisting("policyName")).isTrue(); } diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java index 68fe487ef..914c64ea5 100644 --- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -44,6 +44,9 @@ import org.onap.clamp.loop.components.external.PolicyComponent; import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.service.Service; +import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.loop.template.MicroServiceModel; +import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; import org.skyscreamer.jsonassert.JSONAssert; @@ -72,10 +75,30 @@ public class LoopToJsonTest { MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared, gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - return microService; } + private MicroServiceModel getMicroServiceModel(String yaml, String name, PolicyModel policyModel) { + MicroServiceModel model = new MicroServiceModel(); + model.setBlueprint(yaml); + model.setName(name); + model.setPolicyModel(policyModel); + return model; + } + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant) { + return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant); + } + + private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, + Integer maxInstancesAllowed) { + LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null); + template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1", + getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1"))); + return template; + } + private LoopLog getLoopLog(LogType type, String message, Loop loop) { LoopLog log = new LoopLog(message, type, "CLAMP", loop); log.setId(Long.valueOf(new Random().nextInt())); @@ -95,6 +118,8 @@ public class LoopToJsonTest { loopTest.addMicroServicePolicy(microServicePolicy); LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest); loopTest.addLog(loopLog); + LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1); + loopTest.setLoopTemplate(loopTemplate); String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); assertThat(jsonSerialized).isNotNull().isNotEmpty(); @@ -116,6 +141,9 @@ public class LoopToJsonTest { assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, "loop"); + + // Verify the loop template + assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate); } @Test @@ -128,17 +156,14 @@ public class LoopToJsonTest { Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(), jsonModel.get("resourceDetails").getAsJsonObject(), "1.0"); loopTest2.setModelService(service); - String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2); assertThat(jsonSerialized).isNotNull().isNotEmpty(); System.out.println(jsonSerialized); - JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/loop.json"), - jsonSerialized, true); Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); assertNotNull(loopTestDeserialized); - assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", - "svgRepresentation", "blueprint", "components"); + assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation", + "blueprint", "components"); } @Test diff --git a/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java b/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java new file mode 100644 index 000000000..b284dd795 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java @@ -0,0 +1,161 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.transaction.Transactional; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.clamp.clds.Application; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; +import org.onap.clamp.loop.template.PolicyModelsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class PolicyModelServiceItCase { + + @Autowired + PolicyModelsService policyModelsService; + + @Autowired + PolicyModelsRepository policyModelsRepository; + + private static final String POLICY_MODEL_TYPE_1 = "org.onap.test"; + private static final String POLICY_MODEL_TYPE_1_VERSION_1 = "1.0.0"; + + private static final String POLICY_MODEL_TYPE_2 = "org.onap.test2"; + private static final String POLICY_MODEL_TYPE_2_VERSION_1 = "1.0.0"; + private static final String POLICY_MODEL_TYPE_2_VERSION_2 = "2.0.0"; + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant, String createdBy) { + PolicyModel policyModel = new PolicyModel(); + policyModel.setCreatedBy(createdBy); + policyModel.setPolicyAcronym(policyAcronym); + policyModel.setPolicyModelTosca(policyModelTosca); + policyModel.setPolicyModelType(policyType); + policyModel.setPolicyVariant(policyVariant); + policyModel.setUpdatedBy(createdBy); + policyModel.setVersion(version); + return policyModel; + } + + @Test + @Transactional + public void shouldCreatePolicyModel() { + // given + PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST", + "VARIANT", "user"); + + // when + PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel); + + // then + assertThat(actualPolicyModel).isNotNull(); + assertThat(actualPolicyModel).isEqualTo(policyModelsRepository + .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion())) + .get()); + assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType()); + assertThat(actualPolicyModel.getCreatedBy()).isEqualTo(""); + assertThat(actualPolicyModel.getCreatedDate()).isNotNull(); + assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym()); + assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca()); + assertThat(actualPolicyModel.getPolicyVariant()).isEqualTo(policyModel.getPolicyVariant()); + assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo(""); + assertThat(actualPolicyModel.getUpdatedDate()).isNotNull(); + assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion()); + + assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1)) + .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy"); + } + + @Test + @Transactional + public void shouldReturnAllPolicyModelTypes() { + // given + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + List policyModelTypesList = policyModelsService.getAllPolicyModelTypes(); + + assertThat(policyModelTypesList).containsOnly(policyModel1.getPolicyModelType(), + policyModel2.getPolicyModelType()); + } + + @Test + @Transactional + public void shouldReturnAllPolicyModels() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + assertThat(policyModelsService.getAllPolicyModels()).containsOnly(policyModel1, policyModel2); + } + + @Test + @Transactional + public void shouldReturnAllModelsByType() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + assertThat(policyModelsService.getAllPolicyModelsByType(POLICY_MODEL_TYPE_2)).containsOnly(policyModel1, + policyModel2); + } + + @Test + @Transactional + public void shouldReturnSortedSet() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + SortedSet sortedSet = new TreeSet<>(); + policyModelsService.getAllPolicyModels().forEach(sortedSet::add); + assertThat(sortedSet).containsExactly(policyModel2, policyModel1); + } +} diff --git a/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java b/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java new file mode 100644 index 000000000..1fb5922fd --- /dev/null +++ b/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class SemanticVersioningTest { + + @Test + public void compareTest() { + assertThat(SemanticVersioning.compare("1.0.0", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.0", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.0", "2.1.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.3", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.3", "2.6.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.0", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.0.0", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.1.0", "2.5.1")).isEqualTo(1); + + assertThat(SemanticVersioning.compare("2.0.0", "1.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.0.0", "1.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.1.0", "1.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.0.0", "1.5.3")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.6.0", "2.5.3")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("1", "1.2.3.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.2", "1")).isEqualTo(1); + } + + @Test + public void compareEqualsTest() { + assertThat(SemanticVersioning.compare("1.0.0", "1.0.0")).isEqualTo(0); + assertThat(SemanticVersioning.compare("1.0.0.0", "1.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("1.2.3", "1.2.3")).isEqualTo(0); + assertThat(SemanticVersioning.compare("1.2.3", "1.2.3.0")).isEqualTo(-1); + + } + + @Test + public void compareNullTest() { + assertThat(SemanticVersioning.compare(null, null)).isEqualTo(0); + assertThat(SemanticVersioning.compare(null, "1.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.0", null)).isEqualTo(1); + } +} -- cgit 1.2.3-korg