From 5f14c7ada8aca6bdfecdb4ac3bd2d1387315250c Mon Sep 17 00:00:00 2001 From: Krysiak Adam Date: Fri, 1 Mar 2019 15:32:19 +0100 Subject: created Api for closed loop managing Change-Id: Ib6bd92d1ae3119a857c5a63df10e8f9bb130f75f Issue-ID: CLAMP-301 Signed-off-by: Krysiak Adam --- src/main/java/org/onap/clamp/clds/Application.java | 2 +- .../onap/clamp/clds/config/AAFConfiguration.java | 1 + .../onap/clamp/clds/config/CamelConfiguration.java | 2 +- .../configuration/CamelGsonConfiguration.java | 63 +++++ .../java/org/onap/clamp/dao/LoopLogRepository.java | 33 --- .../java/org/onap/clamp/dao/LoopsRepository.java | 33 --- .../clamp/dao/MicroServicePolicyRepository.java | 33 --- .../clamp/dao/OperationalPolicyRepository.java | 33 --- .../java/org/onap/clamp/dao/model/LogType.java | 28 --- src/main/java/org/onap/clamp/dao/model/Loop.java | 237 ------------------ .../java/org/onap/clamp/dao/model/LoopLog.java | 147 ----------- .../java/org/onap/clamp/dao/model/LoopState.java | 28 --- .../onap/clamp/dao/model/MicroServicePolicy.java | 152 ------------ .../onap/clamp/dao/model/OperationalPolicy.java | 116 --------- src/main/java/org/onap/clamp/loop/Loop.java | 249 +++++++++++++++++++ .../java/org/onap/clamp/loop/LoopController.java | 70 ++++++ src/main/java/org/onap/clamp/loop/LoopService.java | 91 +++++++ src/main/java/org/onap/clamp/loop/LoopState.java | 28 +++ .../java/org/onap/clamp/loop/LoopsRepository.java | 36 +++ src/main/java/org/onap/clamp/loop/log/LogType.java | 28 +++ src/main/java/org/onap/clamp/loop/log/LoopLog.java | 148 +++++++++++ .../org/onap/clamp/loop/log/LoopLogRepository.java | 32 +++ src/main/java/org/onap/clamp/policy/Policy.java | 35 +++ .../java/org/onap/clamp/policy/PolicyService.java | 36 +++ .../policy/microservice/MicroServicePolicy.java | 163 +++++++++++++ .../microservice/MicroServicePolicyRepository.java | 32 +++ .../microservice/MicroservicePolicyService.java | 79 ++++++ .../policy/operational/OperationalPolicy.java | 128 ++++++++++ .../operational/OperationalPolicyRepository.java | 32 +++ .../operational/OperationalPolicyService.java | 66 +++++ .../resources/clds/camel/rest/clamp-api-v2.xml | 34 +++ .../resources/clds/camel/rest/clds-services.xml | 56 ++--- .../java/org/onap/clamp/clds/it/HttpsItCase.java | 2 +- .../org/onap/clamp/dao/model/LoopToJsonTest.java | 104 -------- .../clamp/it/dao/model/LoopRepositoriesItCase.java | 160 ------------ .../onap/clamp/loop/LoopRepositoriesItCase.java | 151 ++++++++++++ .../org/onap/clamp/loop/LoopServiceTestItCase.java | 270 +++++++++++++++++++++ .../java/org/onap/clamp/loop/LoopToJsonTest.java | 107 ++++++++ 38 files changed, 1910 insertions(+), 1135 deletions(-) create mode 100644 src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java delete mode 100644 src/main/java/org/onap/clamp/dao/LoopLogRepository.java delete mode 100644 src/main/java/org/onap/clamp/dao/LoopsRepository.java delete mode 100644 src/main/java/org/onap/clamp/dao/MicroServicePolicyRepository.java delete mode 100644 src/main/java/org/onap/clamp/dao/OperationalPolicyRepository.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/LogType.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/Loop.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/LoopLog.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/LoopState.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java delete mode 100644 src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java create mode 100644 src/main/java/org/onap/clamp/loop/Loop.java create mode 100644 src/main/java/org/onap/clamp/loop/LoopController.java create mode 100644 src/main/java/org/onap/clamp/loop/LoopService.java create mode 100644 src/main/java/org/onap/clamp/loop/LoopState.java create mode 100644 src/main/java/org/onap/clamp/loop/LoopsRepository.java create mode 100644 src/main/java/org/onap/clamp/loop/log/LogType.java create mode 100644 src/main/java/org/onap/clamp/loop/log/LoopLog.java create mode 100644 src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java create mode 100644 src/main/java/org/onap/clamp/policy/Policy.java create mode 100644 src/main/java/org/onap/clamp/policy/PolicyService.java create mode 100644 src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java create mode 100644 src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java create mode 100644 src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java create mode 100644 src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java create mode 100644 src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java create mode 100644 src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java create mode 100644 src/main/resources/clds/camel/rest/clamp-api-v2.xml delete mode 100644 src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java delete mode 100644 src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopToJsonTest.java diff --git a/src/main/java/org/onap/clamp/clds/Application.java b/src/main/java/org/onap/clamp/clds/Application.java index 920b0f28..7e78504f 100644 --- a/src/main/java/org/onap/clamp/clds/Application.java +++ b/src/main/java/org/onap/clamp/clds/Application.java @@ -108,7 +108,7 @@ public class Application extends SpringBootServletInitializer { eelfLogger.info(ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v" + ClampVersioning.getCldsVersionFromProps() + ")" + System.getProperty("line.separator")); ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), - "/restservices/clds/v1/*"); + "/restservices/clds/*"); registration.setName("CamelServlet"); return registration; } diff --git a/src/main/java/org/onap/clamp/clds/config/AAFConfiguration.java b/src/main/java/org/onap/clamp/clds/config/AAFConfiguration.java index 8bc6f69d..c91494df 100644 --- a/src/main/java/org/onap/clamp/clds/config/AAFConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/AAFConfiguration.java @@ -58,6 +58,7 @@ public class AAFConfiguration { registration.addUrlPatterns("/restservices/clds/v1/tosca/*"); registration.addUrlPatterns("/restservices/clds/v1/dictionary/*"); registration.addUrlPatterns("/restservices/clds/v1/user/*"); + registration.addUrlPatterns("/restservices/clds/v2/loop/*"); //registration.addUrlPatterns("*"); registration.setName("cadiFilter"); registration.setOrder(0); diff --git a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java index 45c945c4..f1056504 100644 --- a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java @@ -36,7 +36,7 @@ public class CamelConfiguration extends RouteBuilder { // turn on swagger api-doc .apiContextPath("api-doc").apiVendorExtension(true).apiProperty("api.title", "Clamp Rest API") .apiProperty("api.version", ClampVersioning.getCldsVersionFromProps()) - .apiProperty("base.path", "/restservices/clds/v1/"); + .apiProperty("base.path", "/restservices/clds/"); // .apiProperty("cors", "true"); } } diff --git a/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java b/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java new file mode 100644 index 00000000..f71d2f09 --- /dev/null +++ b/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java @@ -0,0 +1,63 @@ +/*- + * ============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.configuration; + +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.annotations.Expose; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.spi.DataFormatCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CamelGsonConfiguration { + + @Bean + public List> provideGsonCustomizers() { + DataFormatCustomizer dataFormatCustomizer = dataformat -> + dataformat.setExclusionStrategies( + Collections.singletonList(new ExcludeFieldsWithoutExposedAnnotation()) + ); + return Collections.singletonList(dataFormatCustomizer); + } + + private static class ExcludeFieldsWithoutExposedAnnotation implements ExclusionStrategy { + + @Override + public boolean shouldSkipField(FieldAttributes f) { + return Optional.ofNullable(f.getAnnotation(Expose.class)) + .map(expose -> !expose.serialize()) + .orElse(true); + } + + @Override + public boolean shouldSkipClass(Class clazz) { + return false; + } + } +} diff --git a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java b/src/main/java/org/onap/clamp/dao/LoopLogRepository.java deleted file mode 100644 index 199a25e8..00000000 --- a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============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.dao; - -import org.onap.clamp.dao.model.LoopLog; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface LoopLogRepository extends CrudRepository { - -} diff --git a/src/main/java/org/onap/clamp/dao/LoopsRepository.java b/src/main/java/org/onap/clamp/dao/LoopsRepository.java deleted file mode 100644 index 51019baa..00000000 --- a/src/main/java/org/onap/clamp/dao/LoopsRepository.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============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.dao; - -import org.onap.clamp.dao.model.Loop; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface LoopsRepository extends CrudRepository { - -} diff --git a/src/main/java/org/onap/clamp/dao/MicroServicePolicyRepository.java b/src/main/java/org/onap/clamp/dao/MicroServicePolicyRepository.java deleted file mode 100644 index 1bdaf3e5..00000000 --- a/src/main/java/org/onap/clamp/dao/MicroServicePolicyRepository.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============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.dao; - -import org.onap.clamp.dao.model.MicroServicePolicy; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface MicroServicePolicyRepository extends CrudRepository { - -} diff --git a/src/main/java/org/onap/clamp/dao/OperationalPolicyRepository.java b/src/main/java/org/onap/clamp/dao/OperationalPolicyRepository.java deleted file mode 100644 index cf03a812..00000000 --- a/src/main/java/org/onap/clamp/dao/OperationalPolicyRepository.java +++ /dev/null @@ -1,33 +0,0 @@ -/*- - * ============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.dao; - -import org.onap.clamp.dao.model.OperationalPolicy; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface OperationalPolicyRepository extends CrudRepository { - -} diff --git a/src/main/java/org/onap/clamp/dao/model/LogType.java b/src/main/java/org/onap/clamp/dao/model/LogType.java deleted file mode 100644 index 1a8ed36c..00000000 --- a/src/main/java/org/onap/clamp/dao/model/LogType.java +++ /dev/null @@ -1,28 +0,0 @@ -/*- - * ============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.dao.model; - -public enum LogType { - INFO, WARNING, ERROR; -} diff --git a/src/main/java/org/onap/clamp/dao/model/Loop.java b/src/main/java/org/onap/clamp/dao/model/Loop.java deleted file mode 100644 index 78175cc5..00000000 --- a/src/main/java/org/onap/clamp/dao/model/Loop.java +++ /dev/null @@ -1,237 +0,0 @@ -/*- - * ============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.dao.model; - -import com.google.gson.JsonObject; -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.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.FetchType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; -import javax.persistence.OneToMany; -import javax.persistence.Table; - -import org.hibernate.annotations.Type; -import org.hibernate.annotations.TypeDef; -import org.hibernate.annotations.TypeDefs; -import org.onap.clamp.dao.model.jsontype.StringJsonUserType; - -@Entity -@Table(name = "loops") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) -public class Loop implements Serializable { - - /** - * - */ - private static final long serialVersionUID = -286522707701388642L; - - @Id - @Expose - @Column(nullable = false, name = "name", unique = true) - private String name; - - @Expose - @Column(name = "dcae_deployment_id") - private String dcaeDeploymentId; - - @Expose - @Column(name = "dcae_deployment_status_url") - private String dcaeDeploymentStatusUrl; - - @Expose - @Column(name = "dcae_blueprint_id") - private String dcaeBlueprintId; - - @Expose - @Column(name = "svg_representation") - private String svgRepresentation; - - @Expose - @Type(type = "json") - @Column(columnDefinition = "json", name = "global_properties_json") - private JsonObject globalPropertiesJson; - - @Expose - @Column(nullable = false, name = "blueprint_yaml") - private String blueprint; - - @Expose - @Column(nullable = false, name = "last_computed_state") - @Enumerated(EnumType.STRING) - private LoopState lastComputedState; - - @Expose - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") - private Set operationalPolicies = new HashSet<>(); - - @Expose - @ManyToMany(cascade = CascadeType.ALL, 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") - private Set loopLogs = new HashSet<>(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDcaeDeploymentId() { - return dcaeDeploymentId; - } - - public void setDcaeDeploymentId(String dcaeDeploymentId) { - this.dcaeDeploymentId = dcaeDeploymentId; - } - - public String getDcaeDeploymentStatusUrl() { - return dcaeDeploymentStatusUrl; - } - - public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) { - this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl; - } - - public String getSvgRepresentation() { - return svgRepresentation; - } - - public void setSvgRepresentation(String svgRepresentation) { - this.svgRepresentation = svgRepresentation; - } - - public String getBlueprint() { - return blueprint; - } - - public void setBlueprint(String blueprint) { - this.blueprint = blueprint; - } - - public LoopState getLastComputedState() { - return lastComputedState; - } - - public void setLastComputedState(LoopState lastComputedState) { - this.lastComputedState = lastComputedState; - } - - public Set getOperationalPolicies() { - return operationalPolicies; - } - - public void setOperationalPolicies(Set operationalPolicies) { - this.operationalPolicies = operationalPolicies; - } - - public Set getMicroServicePolicies() { - return microServicePolicies; - } - - public void setMicroServicePolicies(Set microServicePolicies) { - this.microServicePolicies = microServicePolicies; - } - - public JsonObject getGlobalPropertiesJson() { - return globalPropertiesJson; - } - - public void setGlobalPropertiesJson(JsonObject globalPropertiesJson) { - this.globalPropertiesJson = globalPropertiesJson; - } - - public Set getLoopLogs() { - return loopLogs; - } - - public void setLoopLogs(Set loopLogs) { - this.loopLogs = loopLogs; - } - - public void addOperationalPolicy(OperationalPolicy opPolicy) { - opPolicy.setLoop(this); - operationalPolicies.add(opPolicy); - } - - public void addMicroServicePolicy(MicroServicePolicy microServicePolicy) { - microServicePolicies.add(microServicePolicy); - microServicePolicy.getUsedByLoops().add(this); - } - - public void addLog(LoopLog log) { - loopLogs.add(log); - log.setLoop(this); - } - - public String getDcaeBlueprintId() { - return dcaeBlueprintId; - } - - public void setDcaeBlueprintId(String dcaeBlueprintId) { - this.dcaeBlueprintId = dcaeBlueprintId; - } - - @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; - Loop other = (Loop) 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/dao/model/LoopLog.java b/src/main/java/org/onap/clamp/dao/model/LoopLog.java deleted file mode 100644 index ba88bbee..00000000 --- a/src/main/java/org/onap/clamp/dao/model/LoopLog.java +++ /dev/null @@ -1,147 +0,0 @@ -/*- - * ============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.dao.model; - -import com.google.gson.annotations.Expose; - -import java.io.Serializable; -import java.time.Instant; -import java.time.temporal.ChronoUnit; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; - -/** - * - * This class holds the logs created by the Clamp Backend. The Instant is always - * rounded to the nearest second as the nano seconds can't be stored in the - * database. The logs can be therefore exposed to the UI or the client doing - * some GET Loop on the backend. - * - */ -@Entity -@Table(name = "loop_logs") -public class LoopLog implements Serializable { - /** - * - */ - private static final long serialVersionUID = 1988276670074437631L; - - @Expose - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Expose - @Column(name = "log_type", nullable = false) - @Enumerated(EnumType.STRING) - private LogType logType; - - @Expose - @Column(name = "message", nullable = false) - private String message; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "loop_id", nullable = false) - private Loop loop; - - @Expose - @Column(name = "log_instant", nullable = false) - private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS); - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public LogType getLogType() { - return logType; - } - - public void setLogType(LogType logType) { - this.logType = logType; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Loop getLoop() { - return loop; - } - - public void setLoop(Loop loop) { - this.loop = loop; - } - - public Instant getLogInstant() { - return logInstant; - } - - public void setLogInstant(Instant logInstant) { - this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.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; - LoopLog other = (LoopLog) obj; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } - -} diff --git a/src/main/java/org/onap/clamp/dao/model/LoopState.java b/src/main/java/org/onap/clamp/dao/model/LoopState.java deleted file mode 100644 index 1cde0aa9..00000000 --- a/src/main/java/org/onap/clamp/dao/model/LoopState.java +++ /dev/null @@ -1,28 +0,0 @@ -/*- - * ============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.dao.model; - -public enum LoopState { - DESIGN, SUBMITTED, DEPLOYED, RUNNING, STOPPED, IN_ERROR, WAITING; -} diff --git a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java deleted file mode 100644 index 8ea6d265..00000000 --- a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java +++ /dev/null @@ -1,152 +0,0 @@ -/*- - * ============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.dao.model; - -import com.google.gson.JsonObject; -import com.google.gson.annotations.Expose; - -import java.io.Serializable; -import java.util.HashSet; -import java.util.Set; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; -import javax.persistence.Table; - -import org.hibernate.annotations.Type; -import org.hibernate.annotations.TypeDef; -import org.hibernate.annotations.TypeDefs; -import org.onap.clamp.dao.model.jsontype.StringJsonUserType; - -@Entity -@Table(name = "micro_service_policies") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) -public class MicroServicePolicy implements Serializable { - /** - * - */ - private static final long serialVersionUID = 6271238288583332616L; - - @Expose - @Id - @Column(nullable = false, name = "name", unique = true) - private String name; - - @Expose - @Type(type = "json") - @Column(columnDefinition = "json", name = "properties") - private JsonObject properties; - - @Expose - @Column(name = "shared", nullable = false) - private Boolean shared; - - @Expose - @Column(name = "policy_tosca", nullable = false) - private String policyTosca; - - @Expose - @Type(type = "json") - @Column(columnDefinition = "json", name = "json_representation", nullable = false) - private JsonObject jsonRepresentation; - - @ManyToMany(mappedBy = "microServicePolicies") - private Set usedByLoops = new HashSet<>(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public JsonObject getProperties() { - return properties; - } - - public void setProperties(JsonObject properties) { - this.properties = properties; - } - - public Boolean getShared() { - return shared; - } - - public void setShared(Boolean shared) { - this.shared = shared; - } - - public String getPolicyTosca() { - return policyTosca; - } - - public void setPolicyTosca(String policyTosca) { - this.policyTosca = policyTosca; - } - - public JsonObject getJsonRepresentation() { - return jsonRepresentation; - } - - public void setJsonRepresentation(JsonObject jsonRepresentation) { - this.jsonRepresentation = jsonRepresentation; - } - - public Set getUsedByLoops() { - return usedByLoops; - } - - public void setUsedByLoops(Set usedBy) { - this.usedByLoops = usedBy; - } - - @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; - MicroServicePolicy other = (MicroServicePolicy) 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/dao/model/OperationalPolicy.java b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java deleted file mode 100644 index ba2c442c..00000000 --- a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java +++ /dev/null @@ -1,116 +0,0 @@ -/*- - * ============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.dao.model; - -import com.google.gson.JsonObject; -import com.google.gson.annotations.Expose; - -import java.io.Serializable; - -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.Table; - -import org.hibernate.annotations.Type; -import org.hibernate.annotations.TypeDef; -import org.hibernate.annotations.TypeDefs; -import org.onap.clamp.dao.model.jsontype.StringJsonUserType; - -@Entity -@Table(name = "operational_policies") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) -public class OperationalPolicy implements Serializable { - /** - * - */ - private static final long serialVersionUID = 6117076450841538255L; - - @Expose - @Id - @Column(nullable = false, name = "name", unique = true) - private String name; - - @Expose - @Type(type = "json") - @Column(columnDefinition = "json", name = "configurations_json") - private JsonObject configurationsJson; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "loop_id", nullable = false) - private Loop loop; - - public Loop getLoop() { - return loop; - } - - public void setLoop(Loop loop) { - this.loop = loop; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public JsonObject getConfigurationsJson() { - return configurationsJson; - } - - public void setConfigurationsJson(JsonObject configurationsJson) { - this.configurationsJson = configurationsJson; - } - - @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; - OperationalPolicy other = (OperationalPolicy) 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/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java new file mode 100644 index 00000000..910c5aad --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/Loop.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.loop; + +import com.google.gson.JsonObject; +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.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.loop.log.LoopLog; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; + +@Entity +@Table(name = "loops") +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +public class Loop implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -286522707701388642L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + @Expose + @Column(name = "dcae_deployment_id") + private String dcaeDeploymentId; + + @Expose + @Column(name = "dcae_deployment_status_url") + private String dcaeDeploymentStatusUrl; + + @Expose + @Column(name = "dcae_blueprint_id") + private String dcaeBlueprintId; + + @Column(name = "svg_representation") + private String svgRepresentation; + + @Expose + @Type(type = "json") + @Column(columnDefinition = "json", name = "global_properties_json") + private JsonObject globalPropertiesJson; + + @Column(nullable = false, name = "blueprint_yaml") + private String blueprint; + + @Expose + @Column(nullable = false, name = "last_computed_state") + @Enumerated(EnumType.STRING) + private LoopState lastComputedState; + + @Expose + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop") + private Set operationalPolicies = new HashSet<>(); + + @Expose + @ManyToMany(cascade = CascadeType.ALL, 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") + private Set loopLogs = new HashSet<>(); + + public Loop() { + } + + public Loop(String name, String blueprint, String svgRepresentation) { + this.name = name; + this.svgRepresentation = svgRepresentation; + this.blueprint = blueprint; + this.lastComputedState = LoopState.DESIGN; + this.globalPropertiesJson = new JsonObject(); + } + + public String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + public String getDcaeDeploymentId() { + return dcaeDeploymentId; + } + + void setDcaeDeploymentId(String dcaeDeploymentId) { + this.dcaeDeploymentId = dcaeDeploymentId; + } + + public String getDcaeDeploymentStatusUrl() { + return dcaeDeploymentStatusUrl; + } + + void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) { + this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl; + } + + public String getSvgRepresentation() { + return svgRepresentation; + } + + void setSvgRepresentation(String svgRepresentation) { + this.svgRepresentation = svgRepresentation; + } + + public String getBlueprint() { + return blueprint; + } + + void setBlueprint(String blueprint) { + this.blueprint = blueprint; + } + + LoopState getLastComputedState() { + return lastComputedState; + } + + void setLastComputedState(LoopState lastComputedState) { + this.lastComputedState = lastComputedState; + } + + Set getOperationalPolicies() { + return operationalPolicies; + } + + void setOperationalPolicies(Set operationalPolicies) { + this.operationalPolicies = operationalPolicies; + } + + Set getMicroServicePolicies() { + return microServicePolicies; + } + + void setMicroServicePolicies(Set microServicePolicies) { + this.microServicePolicies = microServicePolicies; + } + + JsonObject getGlobalPropertiesJson() { + return globalPropertiesJson; + } + + void setGlobalPropertiesJson(JsonObject globalPropertiesJson) { + this.globalPropertiesJson = globalPropertiesJson; + } + + Set getLoopLogs() { + return loopLogs; + } + + void setLoopLogs(Set loopLogs) { + this.loopLogs = loopLogs; + } + + void addOperationalPolicy(OperationalPolicy opPolicy) { + operationalPolicies.add(opPolicy); + opPolicy.setLoop(this); + } + + void addMicroServicePolicy(MicroServicePolicy microServicePolicy) { + microServicePolicies.add(microServicePolicy); + microServicePolicy.getUsedByLoops().add(this); + } + + void addLog(LoopLog log) { + loopLogs.add(log); + log.setLoop(this); + } + + public String getDcaeBlueprintId() { + return dcaeBlueprintId; + } + + public void setDcaeBlueprintId(String dcaeBlueprintId) { + this.dcaeBlueprintId = dcaeBlueprintId; + } + + @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; + Loop other = (Loop) 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/LoopController.java b/src/main/java/org/onap/clamp/loop/LoopController.java new file mode 100644 index 00000000..eeb6105b --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/LoopController.java @@ -0,0 +1,70 @@ +/*- + * ============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.loop; + +import com.google.gson.JsonArray; +import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.List; +import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + + +@Controller +public class LoopController { + + private final LoopService loopService; + private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken>() { + }.getType(); + private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken>() { + }.getType(); + + @Autowired + public LoopController(LoopService loopService) { + this.loopService = loopService; + } + + public List getLoopNames() { + return loopService.getClosedLoopNames(); + } + + public Loop getLoop(String loopName) { + return loopService.getLoop(loopName); + } + + public Loop updateOperationalPolicies(String loopName, JsonArray operationalPoliciesJson) { + List operationalPolicies = JsonUtils.GSON + .fromJson(operationalPoliciesJson, OPERATIONAL_POLICY_TYPE); + return loopService.updateOperationalPolicies(loopName, operationalPolicies); + } + + public Loop updateMicroservicePolicies(String loopName, JsonArray microServicePoliciesJson) { + List microservicePolicies = JsonUtils.GSON + .fromJson(microServicePoliciesJson, MICROSERVICE_POLICY_TYPE); + return loopService.updateMicroservicePolicies(loopName, microservicePolicies); + } +} diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java new file mode 100644 index 00000000..7b79c112 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -0,0 +1,91 @@ +/*- + * ============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.loop; + +import java.util.List; +import java.util.Set; +import javax.persistence.EntityNotFoundException; +import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.operational.OperationalPolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.springframework.stereotype.Service; + +@Service +public class LoopService { + + private final LoopsRepository loopsRepository; + private final MicroservicePolicyService microservicePolicyService; + private final OperationalPolicyService operationalPolicyService; + + public LoopService(LoopsRepository loopsRepository, + MicroservicePolicyService microservicePolicyService, + OperationalPolicyService operationalPolicyService) { + this.loopsRepository = loopsRepository; + this.microservicePolicyService = microservicePolicyService; + this.operationalPolicyService = operationalPolicyService; + } + + Loop addNewLoop(Loop loop) { + return loopsRepository.save(loop); + } + + List getClosedLoopNames() { + return loopsRepository.getAllLoopNames(); + } + + Loop getLoop(String loopName){ + return loopsRepository + .findById(loopName) + .orElse(null); + } + + String getClosedLoopModelSVG(String loopName) { + Loop closedLoopByName = findClosedLoopByName(loopName); + return closedLoopByName.getSvgRepresentation(); + } + + Loop updateOperationalPolicies(String loopName, List newOperationalPolicies) { + Loop loop = findClosedLoopByName(loopName); + Set newPolicies = operationalPolicyService + .updatePolicies(loop, newOperationalPolicies); + + loop.setOperationalPolicies(newPolicies); + return loopsRepository.save(loop); + } + + Loop updateMicroservicePolicies(String loopName, List newMicroservicePolicies) { + Loop loop = findClosedLoopByName(loopName); + Set newPolicies = microservicePolicyService + .updatePolicies(loop, newMicroservicePolicies); + + loop.setMicroServicePolicies(newPolicies); + return loopsRepository.save(loop); + } + + private Loop findClosedLoopByName(String loopName) { + return loopsRepository.findById(loopName) + .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName)); + } +} diff --git a/src/main/java/org/onap/clamp/loop/LoopState.java b/src/main/java/org/onap/clamp/loop/LoopState.java new file mode 100644 index 00000000..4707730c --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/LoopState.java @@ -0,0 +1,28 @@ +/*- + * ============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; + +public enum LoopState { + DESIGN, SUBMITTED, DEPLOYED, RUNNING, STOPPED, IN_ERROR, WAITING; +} diff --git a/src/main/java/org/onap/clamp/loop/LoopsRepository.java b/src/main/java/org/onap/clamp/loop/LoopsRepository.java new file mode 100644 index 00000000..37c47622 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/LoopsRepository.java @@ -0,0 +1,36 @@ +/*- + * ============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 java.util.List; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface LoopsRepository extends CrudRepository { + + @Query("SELECT loop.name FROM Loop as loop") + List getAllLoopNames(); +} diff --git a/src/main/java/org/onap/clamp/loop/log/LogType.java b/src/main/java/org/onap/clamp/loop/log/LogType.java new file mode 100644 index 00000000..13d9dccf --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/log/LogType.java @@ -0,0 +1,28 @@ +/*- + * ============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.log; + +public enum LogType { + INFO, WARNING, ERROR; +} diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLog.java b/src/main/java/org/onap/clamp/loop/log/LoopLog.java new file mode 100644 index 00000000..7b7fe1b1 --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/log/LoopLog.java @@ -0,0 +1,148 @@ +/*- + * ============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.log; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import org.onap.clamp.loop.Loop; + +/** + * + * This class holds the logs created by the Clamp Backend. The Instant is always + * rounded to the nearest second as the nano seconds can't be stored in the + * database. The logs can be therefore exposed to the UI or the client doing + * some GET Loop on the backend. + * + */ +@Entity +@Table(name = "loop_logs") +public class LoopLog implements Serializable { + /** + * + */ + private static final long serialVersionUID = 1988276670074437631L; + + @Expose + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Expose + @Column(name = "log_type", nullable = false) + @Enumerated(EnumType.STRING) + private LogType logType; + + @Expose + @Column(name = "message", nullable = false) + private String message; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "loop_id", nullable = false) + private Loop loop; + + @Expose + @Column(name = "log_instant", nullable = false) + private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public LogType getLogType() { + return logType; + } + + public void setLogType(LogType logType) { + this.logType = logType; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Loop getLoop() { + return loop; + } + + public void setLoop(Loop loop) { + this.loop = loop; + } + + public Instant getLogInstant() { + return logInstant; + } + + public void setLogInstant(Instant logInstant) { + this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.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; + LoopLog other = (LoopLog) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java b/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.java new file mode 100644 index 00000000..103341fa --- /dev/null +++ b/src/main/java/org/onap/clamp/loop/log/LoopLogRepository.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.loop.log; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface LoopLogRepository extends CrudRepository { + +} diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java new file mode 100644 index 00000000..13ba1a9f --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -0,0 +1,35 @@ +/*- + * ============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; + +import com.google.gson.JsonObject; + +public interface Policy { + + String getName(); + + JsonObject getJsonRepresentation(); + +} diff --git a/src/main/java/org/onap/clamp/policy/PolicyService.java b/src/main/java/org/onap/clamp/policy/PolicyService.java new file mode 100644 index 00000000..05785c04 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/PolicyService.java @@ -0,0 +1,36 @@ +/*- + * ============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; + +import java.util.List; +import java.util.Set; +import org.onap.clamp.loop.Loop; + +public interface PolicyService { + + Set updatePolicies(Loop loop, + List newMicroservicePolicies); + + boolean isExisting(String policyName); +} diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java new file mode 100644 index 00000000..e5b333db --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -0,0 +1,163 @@ +/*- + * ============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.policy.microservice; + +import com.google.gson.JsonObject; +import com.google.gson.annotations.Expose; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.Table; + +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.loop.Loop; +import org.onap.clamp.policy.Policy; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; + +@Entity +@Table(name = "micro_service_policies") +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +public class MicroServicePolicy implements Serializable, Policy { + /** + * + */ + private static final long serialVersionUID = 6271238288583332616L; + + @Expose + @Id + @Column(nullable = false, name = "name", unique = true) + private String name; + + @Expose + @Type(type = "json") + @Column(columnDefinition = "json", name = "properties") + private JsonObject properties; + + @Expose + @Column(name = "shared", nullable = false) + private Boolean shared; + + @Expose + @Column(name = "policy_tosca", nullable = false) + private String policyTosca; + + @Expose + @Type(type = "json") + @Column(columnDefinition = "json", name = "json_representation", nullable = false) + private JsonObject jsonRepresentation; + + @ManyToMany(mappedBy = "microServicePolicies") + private Set usedByLoops = new HashSet<>(); + + public MicroServicePolicy() { + //serialization + } + + public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation, + Set usedByLoops) { + this.name = name; + this.policyTosca = policyTosca; + this.shared = shared; + this.jsonRepresentation = jsonRepresentation; + this.usedByLoops = usedByLoops; + } + + public String getName() { + return name; + } + + public JsonObject getProperties() { + return properties; + } + + public void setProperties(JsonObject properties) { + this.properties = properties; + } + + public Boolean getShared() { + return shared; + } + + public void setShared(Boolean shared) { + this.shared = shared; + } + + public String getPolicyTosca() { + return policyTosca; + } + + public void setPolicyTosca(String policyTosca) { + this.policyTosca = policyTosca; + } + + public JsonObject getJsonRepresentation() { + return jsonRepresentation; + } + + public void setJsonRepresentation(JsonObject jsonRepresentation) { + this.jsonRepresentation = jsonRepresentation; + } + + public Set getUsedByLoops() { + return usedByLoops; + } + + public void setUsedByLoops(Set usedBy) { + this.usedByLoops = usedBy; + } + + @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; + MicroServicePolicy other = (MicroServicePolicy) 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/policy/microservice/MicroServicePolicyRepository.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.java new file mode 100644 index 00000000..f658aacc --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyRepository.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.policy.microservice; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +interface MicroServicePolicyRepository extends CrudRepository { + +} 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 00000000..91794b75 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java @@ -0,0 +1,79 @@ +/*- + * ============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.policy.PolicyService; +import org.onap.clamp.loop.Loop; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class MicroservicePolicyService implements PolicyService { + + private final MicroServicePolicyRepository repository; + + @Autowired + public MicroservicePolicyService(MicroServicePolicyRepository repository) { + this.repository = repository; + } + + @Override + @Transactional + 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); + } + + private MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) { + return repository + .findById(policy.getName()) + .map(p -> updateMicroservicePolicyProperties(p, policy, loop)) + .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyTosca(), + policy.getShared(), policy.getJsonRepresentation(), + Sets.newHashSet(loop))); + } + + private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy, + MicroServicePolicy newPolicy, Loop loop) { + oldPolicy.setJsonRepresentation(newPolicy.getJsonRepresentation()); + 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 new file mode 100644 index 00000000..033b5397 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -0,0 +1,128 @@ +/*- + * ============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.policy.operational; + +import com.google.gson.JsonObject; +import com.google.gson.annotations.Expose; +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.loop.Loop; +import org.onap.clamp.policy.Policy; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; + +@Entity +@Table(name = "operational_policies") +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +public class OperationalPolicy implements Serializable, Policy { + /** + * + */ + private static final long serialVersionUID = 6117076450841538255L; + + @Id + @Expose + @Column(nullable = false, name = "name", unique = true) + private String name; + + @Expose + @Type(type = "json") + @Column(columnDefinition = "json", name = "configurations_json") + private JsonObject configurationsJson; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "loop_id", nullable = false) + private Loop loop; + + public OperationalPolicy() { + //Serialization + } + + public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) { + this.name = name; + this.loop = loop; + this.configurationsJson = configurationsJson; + } + + public String getName() { + return name; + } + + public void setLoop(Loop loopName) { + this.loop = loopName; + } + + public Loop getLoop(){ + return loop; + } + + @Override + public JsonObject getJsonRepresentation() { + return configurationsJson; + } + + public JsonObject getConfigurationsJson() { + return configurationsJson; + } + + public void setConfigurationsJson(JsonObject configurationsJson) { + this.configurationsJson = configurationsJson; + } + + @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; + OperationalPolicy other = (OperationalPolicy) 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/policy/operational/OperationalPolicyRepository.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.java new file mode 100644 index 00000000..97b183f7 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepository.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.policy.operational; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +interface OperationalPolicyRepository extends CrudRepository { + void deleteByName(String policyName); +} diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java new file mode 100644 index 00000000..b24a2db1 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java @@ -0,0 +1,66 @@ +/*- + * ============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.operational; + +import com.google.gson.JsonObject; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.onap.clamp.policy.PolicyService; +import org.onap.clamp.loop.Loop; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class OperationalPolicyService implements PolicyService { + + private final OperationalPolicyRepository repository; + + @Autowired + public OperationalPolicyService(OperationalPolicyRepository repository) { + this.repository = repository; + } + + @Override + public Set updatePolicies(Loop loop, List operationalPolicies) { + return operationalPolicies + .stream() + .map(policy -> + repository + .findById(policy.getName()) + .map(p -> setConfigurationJson(p, policy.getConfigurationsJson())) + .orElse(new OperationalPolicy(policy.getName(), loop, policy.getConfigurationsJson()))) + .collect(Collectors.toSet()); + } + + @Override + public boolean isExisting(String policyName) { + return repository.existsById(policyName); + } + + private OperationalPolicy setConfigurationJson(OperationalPolicy policy, JsonObject configurationsJson) { + policy.setConfigurationsJson(configurationsJson); + return policy; + } +} diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml new file mode 100644 index 00000000..0a72a0c1 --- /dev/null +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/clds/camel/rest/clds-services.xml b/src/main/resources/clds/camel/rest/clds-services.xml index 7abffd35..d4baa09a 100644 --- a/src/main/resources/clds/camel/rest/clds-services.xml +++ b/src/main/resources/clds/camel/rest/clds-services.xml @@ -1,36 +1,36 @@ - - + - - + - + - + - - - - - - + - - - - - - - - - - - @@ -142,14 +142,14 @@ uri="bean:org.onap.clamp.clds.service.CldsDictionaryService?method=createOrUpdateDictionary(${header.dictionaryName},${body})" /> - - @@ -157,14 +157,14 @@ uri="bean:org.onap.clamp.clds.service.CldsDictionaryService?method=createOrUpdateDictionaryElements(${header.dictionaryName},${body})" /> - - + diff --git a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java index 81b6b835..57dbcf7a 100644 --- a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java @@ -130,7 +130,7 @@ public class HttpsItCase { }); template.setRequestFactory(factory); ResponseEntity httpsEntity = template - .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/v1/api-doc", String.class); + .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/api-doc", String.class); assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(httpsEntity.getBody()).contains("swagger"); FileUtils.writeStringToFile( diff --git a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java deleted file mode 100644 index 2d9bfd2d..00000000 --- a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/*- - * ============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.dao.model; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -import java.util.Random; - -import org.junit.Test; -import org.onap.clamp.clds.util.JsonUtils; - -public class LoopToJsonTest { - - private OperationalPolicy getOperationalPolicy(String configJson, String name) { - OperationalPolicy opPolicy = new OperationalPolicy(); - opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); - return opPolicy; - } - - private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { - Loop loop = new Loop(); - loop.setName(name); - loop.setSvgRepresentation(svgRepresentation); - loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); - loop.setLastComputedState(LoopState.DESIGN); - loop.setDcaeDeploymentId(dcaeId); - loop.setDcaeDeploymentStatusUrl(dcaeUrl); - loop.setDcaeBlueprintId(dcaeBlueprintId); - return loop; - } - - private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, - String jsonProperties, boolean shared) { - MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); - µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - µService.setShared(shared); - - µService.setName(name); - return µService; - } - - private LoopLog getLoopLog(LogType type, String message) { - LoopLog log = new LoopLog(); - log.setLogType(type); - log.setMessage(message); - log.setId(Long.valueOf(new Random().nextInt())); - return log; - } - - @Test - public void LoopGsonTest() { - Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); - OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); - loopTest.addOperationalPolicy(opPolicy); - MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", - "YamlContent", "{\"param1\":\"value1\"}", true); - loopTest.addMicroServicePolicy(microServicePolicy); - LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); - loopTest.addLog(loopLog); - - String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); - assertThat(jsonSerialized).isNotNull().isNotEmpty(); - System.out.println(jsonSerialized); - Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); - assertNotNull(loopTestDeserialized); - assertThat(loopTestDeserialized).isEqualToComparingFieldByField(loopTest); - assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy); - assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); - assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); - assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, - "loop"); - } -} diff --git a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java deleted file mode 100644 index 8a523fc0..00000000 --- a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java +++ /dev/null @@ -1,160 +0,0 @@ -/*- - * ============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.it.dao.model; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; - -import java.time.Instant; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.clamp.clds.Application; -import org.onap.clamp.dao.LoopLogRepository; -import org.onap.clamp.dao.LoopsRepository; -import org.onap.clamp.dao.MicroServicePolicyRepository; -import org.onap.clamp.dao.OperationalPolicyRepository; -import org.onap.clamp.dao.model.LogType; -import org.onap.clamp.dao.model.Loop; -import org.onap.clamp.dao.model.LoopLog; -import org.onap.clamp.dao.model.LoopState; -import org.onap.clamp.dao.model.MicroServicePolicy; -import org.onap.clamp.dao.model.OperationalPolicy; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.transaction.annotation.Transactional; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class) -public class LoopRepositoriesItCase { - - private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); - - @Autowired - private LoopsRepository loopRepository; - - @Autowired - private MicroServicePolicyRepository microServicePolicyRepository; - - @Autowired - private OperationalPolicyRepository operationalPolicyRepository; - - @Autowired - private LoopLogRepository loopLogRepository; - - private OperationalPolicy getOperationalPolicy(String configJson, String name) { - OperationalPolicy opPolicy = new OperationalPolicy(); - opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); - return opPolicy; - } - - private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { - Loop loop = new Loop(); - loop.setName(name); - loop.setSvgRepresentation(svgRepresentation); - loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); - loop.setLastComputedState(LoopState.DESIGN); - loop.setDcaeDeploymentId(dcaeId); - loop.setDcaeDeploymentStatusUrl(dcaeUrl); - loop.setDcaeBlueprintId(dcaeBlueprintId); - return loop; - } - - private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, - String jsonProperties, boolean shared) { - MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); - µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - µService.setShared(shared); - - µService.setName(name); - return µService; - } - - private LoopLog getLoopLog(LogType type, String message) { - LoopLog log = new LoopLog(); - log.setLogType(type); - log.setMessage(message); - return log; - } - - @Test - @Transactional - public void CrudTest() { - Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); - OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); - loopTest.addOperationalPolicy(opPolicy); - MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", - "YamlContent", "{\"param1\":\"value1\"}", true); - loopTest.addMicroServicePolicy(microServicePolicy); - LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); - loopTest.addLog(loopLog); - - // Attemp to save into the database the entire loop - Loop loopInDb = loopRepository.save(loopTest); - assertThat(loopInDb).isNotNull(); - assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); - // 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).isEqualToComparingFieldByField(loopTest); - assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true); - assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(true); - assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(true); - assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true); - - // Now attempt to read from database - Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); - assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest); - assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); - assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) - .isEqualToComparingFieldByField(opPolicy); - assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) - .isEqualToComparingFieldByField(microServicePolicy); - - // Attempt an update - ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); - loopRepository.save(loopInDbRetrieved); - Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); - assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) - .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); - - // Attempt to delete the object and check it has well been cascaded - loopRepository.delete(loopInDbRetrieved); - assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false); - assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(false); - assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(false); - assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); - - } -} diff --git a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java new file mode 100644 index 00000000..c524eec3 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java @@ -0,0 +1,151 @@ +/*- + * ============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 com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; + +import java.time.Instant; + +import java.util.HashSet; +import org.junit.Test; +import org.junit.runner.RunWith; +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.policy.microservice.MicroServicePolicy; +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; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class LoopRepositoriesItCase { + + private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); + + @Autowired + private LoopsRepository loopRepository; + + @Autowired + private MicroservicePolicyService microServicePolicyService; + + @Autowired + private OperationalPolicyService operationalPolicyService; + + @Autowired + private LoopLogRepository loopLogRepository; + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class)); + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + Loop loop = new Loop(); + loop.setName(name); + loop.setSvgRepresentation(svgRepresentation); + loop.setBlueprint(blueprint); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared, + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + return log; + } + + @Test + @Transactional + public void CrudTest() { + Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org", "UUID-blueprint"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + // Attemp to save into the database the entire loop + Loop loopInDb = loopRepository.save(loopTest); + assertThat(loopInDb).isNotNull(); + assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); + // 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).isEqualToComparingFieldByField(loopTest); + 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); + + // Now attempt to read from database + Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); + assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest); + assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); + assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) + .isEqualToComparingFieldByField(opPolicy); + assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) + .isEqualToComparingFieldByField(microServicePolicy); + + // Attempt an update + ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); + loopRepository.save(loopInDbRetrieved); + Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); + assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) + .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + + // 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(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); + + } +} diff --git a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java new file mode 100644 index 00000000..a9c30873 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java @@ -0,0 +1,270 @@ +/*- + * ============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.loop; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.gson.JsonObject; +import java.util.Set; +import java.util.stream.Collectors; +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; +import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class LoopServiceTestItCase { + + private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest"; + private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}"; + + @Autowired + LoopService loopService; + + @Autowired + LoopsRepository loopsRepository; + + @After + public void tearDown() { + loopsRepository.deleteAll(); + } + + @Test + public void shouldCreateEmptyLoop() { + //given + String loopBlueprint = "blueprint"; + String loopSvg = "representation"; + Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg); + testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + testLoop.setLastComputedState(LoopState.DESIGN); + + //when + Loop actualLoop = loopService.addNewLoop(testLoop); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get()); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint); + assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg); + assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString()) + .isEqualTo("testValue"); + } + + @Test + public void shouldAddOperationalPolicyToLoop() { + //given + saveTestLoopToDb(); + JsonObject confJson = JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class); + String policyName = "policyName"; + OperationalPolicy operationalPolicy = new OperationalPolicy(policyName, null, confJson); + + //when + Loop actualLoop = loopService + .updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies) + .usingElementComparatorIgnoringFields("loop") + .contains(operationalPolicy); + OperationalPolicy savedPolicy = savedPolicies.iterator().next(); + assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME); + + } + + @Test + public void shouldAddMicroservicePolicyToLoop() { + //given + saveTestLoopToDb(); + JsonObject confJson = JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class); + String policyName = "policyName"; + String policyTosca = "policyTosca"; + MicroServicePolicy microServicePolicy = new MicroServicePolicy(policyName, policyTosca, false, confJson, null); + + //when + Loop actualLoop = loopService + .updateMicroservicePolicies(EXAMPLE_LOOP_NAME, 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).extracting("usedByLoops") + .hasSize(1); + + } + + @Test + @Transactional + public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() { + //given + saveTestLoopToDb(); + String firstPolicyName = "firstPolicyName"; + JsonObject newJsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String secondPolicyName = "secondPolicyName"; + String secondPolicyTosca = "secondPolicyTosca"; + MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy(firstPolicyName, "policyTosca", + false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); + + MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, secondPolicyTosca, true, + newJsonRepresentation, null); + + //when + firstMicroServicePolicy.setJsonRepresentation(newJsonRepresentation); + Loop actualLoop = loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, + Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getMicroServicePolicies(); + assertThat(savedPolicies).hasSize(2); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") + .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); + + } + + private void saveTestLoopToDb() { + Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation"); + testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.addNewLoop(testLoop); + } + + @Test + public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() { + //given + saveTestLoopToDb(); + + JsonObject jsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String firstPolicyName = "firstPolicyName"; + String secondPolicyName = "policyName"; + String secondPolicyTosca = "secondPolicyTosca"; + MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy(firstPolicyName, "policyTosca", + false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); + + MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, secondPolicyTosca, true, + jsonRepresentation, null); + + //when + Loop actualLoop = loopService + .updateMicroservicePolicies(EXAMPLE_LOOP_NAME, 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); + + } + + @Test + @Transactional + public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() { + //given + saveTestLoopToDb(); + + String firstPolicyName = "firstPolicyName"; + JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String secondPolicyName = "secondPolicyName"; + OperationalPolicy firstOperationalPolicy = new OperationalPolicy(firstPolicyName, null, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); + + OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, newJsonConfiguration); + + //when + firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration); + Loop actualLoop = loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, + Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(2); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") + .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); + Set policiesLoops = Lists.newArrayList(savedPolicies).stream() + .map(OperationalPolicy::getLoop) + .map(Loop::getName) + .collect(Collectors.toSet()); + assertThat(policiesLoops) + .containsExactly(EXAMPLE_LOOP_NAME); + } + + @Test + public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() { + //given + saveTestLoopToDb(); + + JsonObject jsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String firstPolicyName = "firstPolicyName"; + String secondPolicyName = "policyName"; + OperationalPolicy firstOperationalPolicy = new OperationalPolicy(firstPolicyName, null, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); + + OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, jsonRepresentation); + + //when + Loop actualLoop = loopService + .updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(secondOperationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") + .containsExactly(secondOperationalPolicy); + OperationalPolicy savedPolicy = savedPolicies.iterator().next(); + assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME); + + } + + private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) { + return new Loop(loopName, loopBlueprint, loopSvg); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java new file mode 100644 index 00000000..92a0e14c --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -0,0 +1,107 @@ +/*- + * ============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 static org.junit.Assert.assertNotNull; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.util.HashSet; +import java.util.Random; + +import org.junit.Test; +import org.onap.clamp.loop.log.LogType; +import org.onap.clamp.loop.log.LoopLog; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.onap.clamp.clds.util.JsonUtils; + +public class LoopToJsonTest { + + private Gson gson = new Gson(); + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class)); + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + Loop loop = new Loop(name, blueprint, svgRepresentation); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared, + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); + + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + log.setId(Long.valueOf(new Random().nextInt())); + return log; + } + + @Test + public void LoopGsonTest() { + Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org", "UUID-blueprint"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); + assertThat(jsonSerialized).isNotNull().isNotEmpty(); + System.out.println(jsonSerialized); + Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); + assertNotNull(loopTestDeserialized); + assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint"); + + //svg and blueprint not exposed so wont be deserialized + assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null); + assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null); + + assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy); + assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); + assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); + assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, + "loop"); + } +} -- cgit 1.2.3-korg