diff options
Diffstat (limited to 'models-tosca/src/main/java')
31 files changed, 3582 insertions, 756 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaConstraint.java deleted file mode 100644 index 47a4d35a6..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaConstraint.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the Constraint of property in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaConstraint { - - @Getter - @Setter - @SerializedName("equal") - private String equal; - - @Getter - @Setter - @SerializedName("greater_than") - private String greaterThan; - - @Getter - @Setter - @SerializedName("greater_or_equal") - private String greaterOrEqual; - - @Getter - @Setter - @SerializedName("less_than") - private String lessThan; - - @Getter - @Setter - @SerializedName("less_or_equal") - private String lessOrEqual; - - @Getter - @Setter - @SerializedName("in_range") - private String inRange; - - @Getter - @Setter - @SerializedName("valid_values") - private String validValues; - - @Getter - @Setter - @SerializedName("length") - private String length; - - @Getter - @Setter - @SerializedName("min_length") - private String minLength; - - @Getter - @Setter - @SerializedName("max_length") - private String maxLength; - - @Getter - @Setter - @SerializedName("pattern") - private String pattern; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaDataType.java deleted file mode 100644 index 0366ee467..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaDataType.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; -import java.util.Map; - -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent custom data type in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaDataType { - @Getter - @Setter - @SerializedName("derived_from") - private String derivedFrom; - - @Getter - @Setter - @SerializedName("version") - private String version; - - @Getter - @Setter - @SerializedName("metadata") - private Map<String, String> metadata; - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("constraints") - private List<ToscaConstraint> constraints; - - @Getter - @Setter - @SerializedName("properties") - private List<Map<String, ToscaProperty>> properties; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEntrySchema.java deleted file mode 100644 index b3079afc0..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEntrySchema.java +++ /dev/null @@ -1,55 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; - -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the EntrySchema of list/map property in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaEntrySchema { - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("type") - private String type; - - @Getter - @Setter - @SerializedName("constraints") - private List<ToscaConstraint> constraints; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEventFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEventFilter.java deleted file mode 100644 index 34d791202..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEventFilter.java +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the EventFilter in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaEventFilter { - - @Getter - @Setter - @SerializedName("node") - private String node; - - @Getter - @Setter - @SerializedName("requirement") - private String requirement; - - @Getter - @Setter - @SerializedName("capability") - private String capability; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicy.java deleted file mode 100644 index 4f9210c43..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicy.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; -import java.util.Map; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the policy in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaPolicy { - - @Getter - @Setter - @SerializedName("type") - private String type; - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("metadata") - private Map<String, String> metadata; - - @Getter - @Setter - @SerializedName("properties") - private List<Map<String, String>> properties; - - @Getter - @Setter - @SerializedName("targets") - private List<String> targets; - - @Getter - @Setter - @SerializedName("triggers") - private List<Map<String, ToscaTrigger>> triggers; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyList.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyList.java deleted file mode 100644 index ad8f1a498..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyList.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; -import java.util.Map; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the policy list in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaPolicyList { - - @Getter - @Setter - @SerializedName("policies") - private List<Map<String, ToscaPolicy>> policies; - - @Getter - @Setter - @SerializedName("data_types") - private List<Map<String, ToscaDataType>> dataTypes; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyType.java deleted file mode 100644 index 241e178f5..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyType.java +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; -import java.util.Map; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the policy type in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaPolicyType { - - @Getter - @Setter - @SerializedName("derived_from") - private String derivedFrom; - - @Getter - @Setter - @SerializedName("version") - private String version; - - @Getter - @Setter - @SerializedName("metadata") - private Map<String, String> metadata; - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("properties") - private List<Map<String, ToscaProperty>> properties; - - @Getter - @Setter - @SerializedName("targets") - private List<String> targets; - - @Getter - @Setter - @SerializedName("triggers") - private List<Map<String, ToscaTrigger>> triggers; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyTypeList.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyTypeList.java deleted file mode 100644 index 4a6a42dbd..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaPolicyTypeList.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; -import java.util.Map; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the policy type list in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaPolicyTypeList { - - @Getter - @Setter - @SerializedName("policy_types") - private List<Map<String, ToscaPolicyType>> policyTypes; - - @Getter - @Setter - @SerializedName("data_types") - private List<Map<String, ToscaDataType>> dataTypes; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaProperty.java deleted file mode 100644 index 4ee5c45ec..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaProperty.java +++ /dev/null @@ -1,80 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import java.util.List; - -import javax.persistence.Column; - -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -import org.onap.policy.models.base.PfConceptKey; - -/** - * Class to represent the property in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaProperty { - - @Getter - @Setter - @SerializedName("type") - @Column(name = "derivedFrom") - private PfConceptKey type; - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("required") - private boolean required; - - @Getter - @Setter - @SerializedName("default_value") - private Object defaultValue; - - @Getter - @Setter - @SerializedName("status") - private String status; - - @Getter - @Setter - @SerializedName("constraints") - private List<ToscaConstraint> constraints; - - @Getter - @Setter - @SerializedName("entry_schema") - private ToscaEntrySchema entrySchema; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTimeInterval.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTimeInterval.java deleted file mode 100644 index 86334ab7f..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTimeInterval.java +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the TimeInterval in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaTimeInterval { - @Getter - @Setter - @SerializedName("start_time") - private String startTime; - - @Getter - @Setter - @SerializedName("end_time") - private String endTime; - -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTrigger.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTrigger.java deleted file mode 100644 index 2464c2e06..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaTrigger.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca; - -import com.google.gson.annotations.SerializedName; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to represent the trigger of policy type in TOSCA definition. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -@ToString -public class ToscaTrigger { - - @Getter - @Setter - @SerializedName("description") - private String description; - - @Getter - @Setter - @SerializedName("event_type") - private String eventType; - - @Getter - @Setter - @SerializedName("schedule") - private ToscaTimeInterval schedule; - - @Getter - @Setter - @SerializedName("target_filter") - private ToscaEventFilter targetFilter; - - @Getter - @Setter - @SerializedName("condition") - private ToscaConstraint condition; - - @Getter - @Setter - @SerializedName("constraint") - private ToscaConstraint constraint; - - @Getter - @Setter - @SerializedName("period") - private String period; - - @Getter - @Setter - @SerializedName("evaluations") - private int evaluations; - - @Getter - @Setter - @SerializedName("method") - private String method; - - @Getter - @Setter - @SerializedName("action") - private String action; -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraint.java new file mode 100644 index 000000000..dadad6fd6 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraint.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.List; + +import javax.persistence.EmbeddedId; +import javax.ws.rs.core.Response; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Immutable class to represent the Constraint of property in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +@EqualsAndHashCode(callSuper = false) +public abstract class ToscaConstraint extends PfConcept { + private static final long serialVersionUID = 6426438089914347734L; + + @EmbeddedId + private final PfReferenceKey key; + + /** + * The Default Constructor creates a {@link ToscaConstraint} object with a null key. + */ + public ToscaConstraint() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraint} object with the given concept key. + * + * @param key the key + */ + public ToscaConstraint(@NonNull final PfReferenceKey key) { + this.key = key; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaConstraint(@NonNull final ToscaConstraint copyConcept) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } + + @Override + public List<PfKey> getKeys() { + return getKey().getKeys(); + } + + @Override + public void clean() { + key.clean(); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + return key.validate(result); + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaConstraint other = (ToscaConstraint) otherConcept; + + return key.compareTo(other.key); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogical.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogical.java new file mode 100644 index 000000000..f186d8525 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogical.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import javax.persistence.Column; +import javax.ws.rs.core.Response; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; + +/** + * This class represents a logical TOSCA constraint: =,>,>=,<,<=. + */ +@EqualsAndHashCode(callSuper = false) +@ToString +public class ToscaConstraintLogical extends ToscaConstraint { + private static final long serialVersionUID = 2562306457768745444L; + + public enum Operation { + EQ, + GT, + GE, + LT, + LE + } + + @Column + @NonNull + @Getter + private final Operation operation; + + /** + * The Default Constructor creates a {@link ToscaConstraintLogical} object with a null key. + */ + public ToscaConstraintLogical() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogical} object with the given concept key. + * + * @param key the key of the constraint + */ + public ToscaConstraintLogical(final PfReferenceKey key) { + this(key, Operation.EQ); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogical} object with the given concept key and operation. + * + * @param key the key of the constraint + * @param operation the logical operation of the constraint + * + */ + public ToscaConstraintLogical(final PfReferenceKey key, @NonNull final Operation operation) { + super(key); + this.operation = operation; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaConstraintLogical(@NonNull final ToscaConstraintLogical copyConcept) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaConstraintLogical other = (ToscaConstraintLogical) otherConcept; + + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + return operation.compareTo(other.operation); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKey.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKey.java new file mode 100644 index 000000000..1a63f30c8 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalKey.java @@ -0,0 +1,155 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import javax.ws.rs.core.Response; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.ToString; + +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * This immutable class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner + * of an instance of the class to the referenced key. + */ +@Entity +@Table(name = "ToscaConstraintLogicalKey") +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@EqualsAndHashCode(callSuper = false) +@ToString +public final class ToscaConstraintLogicalKey extends ToscaConstraintLogical { + private static final long serialVersionUID = -2420828090326264341L; + + @Column + @NonNull + @Getter + private final PfKey compareToKey; + + /** + * The Default Constructor creates a {@link ToscaConstraintLogicalKey} object with a null key. + */ + public ToscaConstraintLogicalKey() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogicalKey} object with the given concept + * key. + * + * @param key the key of the constraint + */ + public ToscaConstraintLogicalKey(final PfReferenceKey key) { + this(key, Operation.EQ, PfConceptKey.getNullKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogicalKey} object with the given concept + * key, operation, and compare key. + * + * @param key the key of the constraint + * @param operation the logical operation of the constraint + * @param compareToKey the key of the object to which the object that owns this constraint will + * be compared + */ + public ToscaConstraintLogicalKey(final PfReferenceKey key, @NonNull final Operation operation, + @NonNull final PfKey compareToKey) { + super(key, operation); + this.compareToKey = compareToKey; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaConstraintLogicalKey(@NonNull final ToscaConstraintLogical copyConcept) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + keyList.addAll(compareToKey.getKeys()); + return keyList; + } + + @Override + public void clean() { + super.clean(); + compareToKey.clean(); + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (compareToKey.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "comparison key is a null key")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaConstraintLogicalKey other = (ToscaConstraintLogicalKey) otherConcept; + + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + return compareToKey.compareTo(other.compareToKey); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalString.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalString.java new file mode 100644 index 000000000..c786d626b --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaConstraintLogicalString.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import javax.ws.rs.core.Response; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * This class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner of an + * instance of the class to the given string. + */ +@Entity +@Table(name = "ToscaConstraintLogicalString") +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@EqualsAndHashCode(callSuper = false) +public class ToscaConstraintLogicalString extends ToscaConstraintLogical { + private static final long serialVersionUID = 8167550632122339195L; + + @Column + @NonNull + @Getter + private final String compareToString; + + /** + * The Default Constructor creates a {@link ToscaConstraintLogicalString} object with a null key. + */ + public ToscaConstraintLogicalString() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogicalString} object with the given concept + * key. + * + * @param key the key of the constraint + */ + public ToscaConstraintLogicalString(final PfReferenceKey key) { + this(key, Operation.EQ, ""); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogicalString} object with the given concept + * key, operation, and compare string. + * + * @param key the key of the constraint + * @param operation the logical operation of the constraint + * @param compareToString the key of the object to which the object that owns this constraint will + * be compared + */ + public ToscaConstraintLogicalString(final PfReferenceKey key, @NonNull final Operation operation, + @NonNull final String compareToString) { + super(key, operation); + this.compareToString = compareToString.trim(); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaConstraintLogicalString(@NonNull final ToscaConstraintLogical copyConcept) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (!ParameterValidationUtils.validateStringParameter(compareToString)) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "comparison string is null or blank")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaConstraintLogicalString other = (ToscaConstraintLogicalString) otherConcept; + + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + return compareToString.compareTo(other.compareToString); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataType.java new file mode 100644 index 000000000..5af24d6ed --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataType.java @@ -0,0 +1,246 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent custom data type in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaDataType") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaDataType extends ToscaEntityType { + private static final long serialVersionUID = -3922690413436539164L; + + @ElementCollection + private List<ToscaConstraint> constraints; + + @ElementCollection + private List<ToscaProperty> properties; + + /** + * The Default Constructor creates a {@link ToscaDataType} object with a null key. + */ + public ToscaDataType() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaDataType} object with the given concept key. + * + * @param key the key + */ + public ToscaDataType(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaDataType(final ToscaDataType copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + keyList.addAll(constraint.getKeys()); + } + } + + if (properties != null) { + for (ToscaProperty property : properties) { + keyList.addAll(property.getKeys()); + } + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + constraint.clean(); + } + } + + if (properties != null) { + for (ToscaProperty property : properties) { + property.clean(); + } + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (constraints != null) { + result = validateConstraints(result); + } + + if (properties != null) { + result = validateProperties(result); + } + + return result; + } + + /** + * Validate the constraints. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateConstraints(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (ToscaConstraint constraint : constraints) { + if (constraint == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "data type constraint may not be null ")); + } else { + result = constraint.validate(result); + } + } + return result; + } + + /** + * Validate the properties. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (ToscaProperty property : properties) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "data type property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaDataType other = (ToscaDataType) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + int result = PfUtils.compareObjects(constraints, other.constraints); + if (result != 0) { + return result; + } + + result = PfUtils.compareObjects(properties, other.properties); + if (result != 0) { + return result; + } + + return 0; + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final ToscaDataType copy = ((ToscaDataType) copyObject); + super.copyTo(target); + + if (constraints == null) { + copy.setConstraints(null); + } + else { + final List<ToscaConstraint> newConstraints = new ArrayList<>(); + for (final ToscaConstraint constraint : constraints) { + newConstraints.add(constraint); // Constraints are immutable + } + copy.setConstraints(newConstraints); + } + + if (properties == null) { + copy.setProperties(null); + } + else { + final List<ToscaProperty> newProperties = new ArrayList<>(); + for (final ToscaProperty property : properties) { + newProperties.add(new ToscaProperty(property)); + } + copy.setProperties(newProperties); + } + + return copy; + } +}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataTypes.java new file mode 100644 index 000000000..61c3500cb --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaDataTypes.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.Map; +import java.util.TreeMap; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * This class is a container for TOSCA data types. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaDataTypes") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaDataTypes extends PfConceptContainer<ToscaDataType> { + private static final long serialVersionUID = 2941102271022190348L; + + /** + * The Default Constructor creates a {@link ToscaDataTypes} object with a null artifact key + * and creates an empty concept map. + */ + public ToscaDataTypes() { + super(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaDataTypes} object with the given artifact key + * and creates an empty concept map. + * + * @param key the concept key + */ + public ToscaDataTypes(final PfConceptKey key) { + super(key, new TreeMap<PfConceptKey, ToscaDataType>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public ToscaDataTypes(final PfConceptKey key, final Map<PfConceptKey, ToscaDataType> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaDataTypes(final ToscaDataTypes copyConcept) { + super(copyConcept); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEntityType.java index 52a82cc53..b0dc58d93 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/ToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEntityType.java @@ -18,11 +18,10 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.tosca; +package org.onap.policy.models.tosca.concepts; import com.google.gson.annotations.SerializedName; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -41,11 +40,16 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; +import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; /** * Class to represent the EntrySchema of list/map property in TOSCA definition. @@ -59,8 +63,7 @@ public class ToscaEntityType extends PfConcept { private static final long serialVersionUID = -1330661834220739393L; @EmbeddedId - @NonNull - private PfConceptKey key = PfConceptKey.getNullKey(); + private PfConceptKey key; @SerializedName("derived_from") @Column(name = "derivedFrom") @@ -99,23 +102,65 @@ public class ToscaEntityType extends PfConcept { @Override public List<PfKey> getKeys() { - final List<PfKey> keyList = new ArrayList<>(); - keyList.add(getKey()); + final List<PfKey> keyList = getKey().getKeys(); + if (derivedFrom != null) { + keyList.addAll(derivedFrom.getKeys()); + } return keyList; } @Override public void clean() { - description = description.trim(); + key.clean(); + + if (derivedFrom != null) { + derivedFrom.clean(); + } - for (Entry<String, String> metadataEntry : metadata.entrySet()) { - metadataEntry.setValue(metadataEntry.getValue().trim()); + if (metadata != null) { + for (Entry<String, String> metadataEntry : metadata.entrySet()) { + metadataEntry.setValue(metadataEntry.getValue().trim()); + } } + + description = (description != null ? description.trim() : null); } @Override - public PfValidationResult validate(PfValidationResult result) { - return null; + public PfValidationResult validate(PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (derivedFrom != null && derivedFrom.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "derived from key is a null key")); + } + + if (metadata != null) { + for (Entry<String, String> metadataEntry : metadata.entrySet()) { + if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property metadata key may not be null")); + } + if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property metadata value may not be null")); + } + } + } + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property description may not be blank")); + } + + return result; } @Override @@ -135,15 +180,17 @@ public class ToscaEntityType extends PfConcept { return key.compareTo(other.key); } - if (!derivedFrom.equals(other.derivedFrom)) { - return derivedFrom.compareTo(other.derivedFrom); + int result = ObjectUtils.compare(derivedFrom, other.derivedFrom); + if (result != 0) { + return result; } - if (!metadata.equals(other.metadata)) { - return (metadata.hashCode() - other.metadata.hashCode()); + result = PfUtils.compareObjects(metadata, other.metadata); + if (result != 0) { + return result; } - return description.compareTo(other.description); + return ObjectUtils.compare(description, other.description); } @Override @@ -152,16 +199,18 @@ public class ToscaEntityType extends PfConcept { Assertions.instanceOf(copyObject, PfConcept.class); final ToscaEntityType copy = ((ToscaEntityType) copyObject); - copy.key = new PfConceptKey(key); - copy.derivedFrom = new PfConceptKey(derivedFrom); - - final Map<String, String> newMatadata = new TreeMap<>(); - for (final Entry<String, String> metadataEntry : metadata.entrySet()) { - newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue()); + copy.setKey(new PfConceptKey(key)); + copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null); + + if (metadata != null) { + final Map<String, String> newMatadata = new TreeMap<>(); + for (final Entry<String, String> metadataEntry : metadata.entrySet()) { + newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue()); + } + copy.setMetadata(newMatadata); } - copy.metadata = newMatadata; - copy.description = description; + copy.setDescription(description); return copy; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchema.java new file mode 100644 index 000000000..72bedbd9e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEntrySchema.java @@ -0,0 +1,228 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + + +/** + * Class to represent the EntrySchema of list/map property in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaEntrySchema") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaEntrySchema extends PfConcept { + private static final long serialVersionUID = 3645882081163287058L; + + @EmbeddedId + private PfReferenceKey key; + + @Column + private PfConceptKey type; + + @Column + private String description; + + @ElementCollection + private List<ToscaConstraint> constraints; + + /** + * The Default Constructor creates a {@link ToscaEntrySchema} object with a null key. + */ + public ToscaEntrySchema() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaEntrySchema} object with the given concept key. + * + * @param key the key + */ + public ToscaEntrySchema(@NonNull final PfReferenceKey key) { + this(key, new PfConceptKey()); + } + + /** + * The full constructor creates a {@link ToscaEntrySchema} object with mandatory fields. + * + * @param key the key + * @param type the type of the entry schema + */ + public ToscaEntrySchema(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) { + this.key = key; + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaEntrySchema(final ToscaEntrySchema copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + + keyList.addAll(type.getKeys()); + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + keyList.addAll(constraint.getKeys()); + } + } + + return keyList; + } + + @Override + public void clean() { + key.clean(); + + type.clean(); + description = (description != null ? description.trim() : null); + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + constraint.clean(); + } + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (type == null || type.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "entry schema type may not be null")); + } + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "entry schema description may not be blank")); + } + + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + if (constraint == null) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property constraint may not be null ")); + } else { + result = constraint.validate(result); + } + } + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaEntrySchema other = (ToscaEntrySchema) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + if (!type.equals(other.type)) { + return type.compareTo(other.type); + } + + int result = ObjectUtils.compare(description, other.description); + if (result != 0) { + return result; + } + + return PfUtils.compareObjects(constraints, other.constraints); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + Assertions.instanceOf(target, ToscaEntrySchema.class); + + final ToscaEntrySchema copy = ((ToscaEntrySchema) target); + copy.setKey(new PfReferenceKey(key)); + copy.setType(new PfConceptKey(type)); + copy.setDescription(description); + + if (constraints != null) { + final List<ToscaConstraint> newConstraints = new ArrayList<>(); + for (final ToscaConstraint constraint : constraints) { + newConstraints.add(constraint); // Constraints are immutable + } + copy.setConstraints(newConstraints); + } + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEventFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEventFilter.java new file mode 100644 index 000000000..4e2d73763 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaEventFilter.java @@ -0,0 +1,197 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the EventFilter in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaEventFilter") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaEventFilter extends PfConcept { + private static final long serialVersionUID = 8769020537228210247L; + + @EmbeddedId + private PfReferenceKey key; + + @Column + private PfConceptKey node; + + @Column + private String requirement; + + @Column + private String capability; + + /** + * The Default Constructor creates a {@link ToscaEventFilter} object with a null key. + */ + public ToscaEventFilter() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaEventFilter} object with the given concept key. + * + * @param key the key + */ + public ToscaEventFilter(@NonNull final PfReferenceKey key) { + this(key, new PfConceptKey()); + } + + /** + * The full Constructor creates a {@link ToscaEventFilter} object with the given concept key and node. + * + * @param key the key + */ + public ToscaEventFilter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey node) { + this.key = key; + this.node = node; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaEventFilter(final ToscaEventFilter copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + keyList.addAll(node.getKeys()); + return keyList; + } + + @Override + public void clean() { + key.clean(); + node.clean(); + + requirement = (requirement != null ? requirement.trim() : requirement); + capability = (capability != null ? capability.trim() : capability); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (node == null || node.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "node on an event filter may not be null")); + } + + if (requirement != null && requirement.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "event filter requirement may not be blank")); + } + + if (capability != null && capability.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "event filter capability may not be blank")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaEventFilter other = (ToscaEventFilter) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + if (!node.equals(other.node)) { + return node.compareTo(other.node); + } + + int result = ObjectUtils.compare(requirement, other.requirement); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(capability, other.capability); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, ToscaEventFilter.class); + + final ToscaEventFilter copy = ((ToscaEventFilter) copyObject); + copy.setKey(new PfReferenceKey(key)); + copy.setNode(new PfConceptKey(node)); + copy.setRequirement(requirement); + copy.setCapability(capability); + + return copy; + } +}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaModel.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaModel.java new file mode 100644 index 000000000..9169e6778 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaModel.java @@ -0,0 +1,160 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModel; +import org.onap.policy.models.base.PfModelService; +import org.onap.policy.models.base.PfValidationResult; + +/** + * A container class for a TOSCA model with multiple service templates. This class is a container + * class that allows a model with many service templates to be constructed that contains a well + * formed overall TOSCA model. + * + * <p>Validation runs {@link ToscaModel} validation on the model and all its sub concepts. + */ + +@Entity +@Table(name = "ToscaModel") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaModel extends PfModel { + private static final long serialVersionUID = 8800599637708309945L; + + @OneToOne(cascade = CascadeType.ALL) + private ToscaServiceTemplates serviceTemplates; + + /** + * The Default Constructor creates a {@link ToscaModel} object with a null concept key and + * creates an empty TOSCA model. + */ + public ToscaModel() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaModel} object with the given concept key and + * creates an empty TOSCA model. + * + * @param key the TOSCA model key + */ + public ToscaModel(final PfConceptKey key) { + this(key, new ToscaServiceTemplates(new PfConceptKey())); + } + + /** + * Constructor that initiates a {@link ToscaModel} with all its fields. + * + * @param key the TOSCA model key + * @param serviceTemplates the service templates in the event model + */ + public ToscaModel(@NonNull final PfConceptKey key, @NonNull final ToscaServiceTemplates serviceTemplates) { + super(key); + this.serviceTemplates = serviceTemplates; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaModel(@NonNull final ToscaModel copyConcept) { + super(copyConcept); + } + + @Override + public void register() { + PfModelService.registerModel(serviceTemplates.getId(), getServiceTemplates()); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + keyList.addAll(serviceTemplates.getKeys()); + + return keyList; + } + + @Override + public void clean() { + super.clean(); + serviceTemplates.clean(); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + return serviceTemplates.validate(result); + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaModel other = (ToscaModel) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + return serviceTemplates.compareTo(other.serviceTemplates); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept targetObject) { + Assertions.instanceOf(targetObject, ToscaModel.class); + + final ToscaModel copy = ((ToscaModel) targetObject); + super.copyTo(targetObject); + copy.setServiceTemplates(new ToscaServiceTemplates(serviceTemplates)); + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicies.java new file mode 100644 index 000000000..7209fd081 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicies.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.Map; +import java.util.TreeMap; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * This class is a container for TOSCA data types. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaPolicies") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaPolicies extends PfConceptContainer<ToscaPolicy> { + private static final long serialVersionUID = -7526648702327776101L; + + /** + * The Default Constructor creates a {@link ToscaPolicies} object with a null artifact key and + * creates an empty concept map. + */ + public ToscaPolicies() { + super(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaPolicies} object with the given artifact key and + * creates an empty concept map. + * + * @param key the concept key + */ + public ToscaPolicies(final PfConceptKey key) { + super(key, new TreeMap<PfConceptKey, ToscaPolicy>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public ToscaPolicies(final PfConceptKey key, final Map<PfConceptKey, ToscaPolicy> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaPolicies(final ToscaPolicies copyConcept) { + super(copyConcept); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicy.java new file mode 100644 index 000000000..3e1fddd31 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicy.java @@ -0,0 +1,273 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the policy in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaPolicy") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaPolicy extends ToscaEntityType { + private static final long serialVersionUID = 3265174757061982805L; + + @Column + private PfConceptKey type; + + @ElementCollection + private List<ToscaProperty> properties; + + @ElementCollection + private List<PfConceptKey> targets; + + /** + * The Default Constructor creates a {@link ToscaPolicy} object with a null key. + */ + public ToscaPolicy() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaPolicy} object with the given concept key. + * + * @param key the key + */ + public ToscaPolicy(@NonNull final PfConceptKey key) { + this(key, new PfConceptKey()); + } + + /** + * The full Constructor creates a {@link ToscaPolicy} object with all mandatory fields. + * + * @param key the key + * @param type the type of the policy + */ + public ToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) { + super(key); + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaPolicy(@NonNull final ToscaPolicy copyConcept) { + super(copyConcept); + } + + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + keyList.addAll(type.getKeys()); + + if (properties != null) { + for (ToscaProperty property : properties) { + keyList.addAll(property.getKeys()); + } + } + + if (targets != null) { + keyList.addAll(targets); + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + type.clean(); + + if (properties != null) { + for (ToscaProperty property : properties) { + property.clean(); + } + } + + if (targets != null) { + for (PfConceptKey target : targets) { + target.clean(); + } + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (type == null || type.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID, + "type is null or a null key")); + } + else { + result = type.validate(result); + } + + if (properties != null) { + result = validateProperties(result); + } + + if (targets != null) { + result = validateTargets(result); + } + + return result; + } + + /** + * Validate the policy properties. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (ToscaProperty property : properties) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "policy property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + /** + * Validate the policy targets. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateTargets(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (PfConceptKey target : targets) { + if (target == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "policy target may not be null ")); + } else { + result = target.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + + if (this == otherConcept) { + return 0; + } + + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaPolicy other = (ToscaPolicy) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + if (!type.equals(other.type)) { + return type.compareTo(other.type); + } + + int retVal = PfUtils.compareObjects(properties, other.properties); + if (retVal != 0) { + return retVal; + } + + return PfUtils.compareObjects(targets, other.targets); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final ToscaPolicy copy = ((ToscaPolicy) copyObject); + super.copyTo(target); + + copy.setType(new PfConceptKey(type)); + + if (properties == null) { + copy.setProperties(null); + } else { + final List<ToscaProperty> newProperties = new ArrayList<>(); + for (final ToscaProperty property : properties) { + newProperties.add(new ToscaProperty(property)); + } + copy.setProperties(newProperties); + } + + if (targets == null) { + copy.setTargets(null); + } else { + final List<PfConceptKey> newTargets = new ArrayList<>(); + for (final PfConceptKey oldTarget : targets) { + newTargets.add(new PfConceptKey(oldTarget)); + } + copy.setTargets(newTargets); + } + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyType.java new file mode 100644 index 000000000..dbd011468 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyType.java @@ -0,0 +1,294 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the policy type in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ + +@Entity +@Table(name = "ToscaPolicyType") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaPolicyType extends ToscaEntityType { + private static final long serialVersionUID = -563659852901842616L; + + @ElementCollection + private List<ToscaProperty> properties; + + @ElementCollection + private List<PfConceptKey> targets; + + @ElementCollection + private List<ToscaTrigger> triggers; + + /** + * The Default Constructor creates a {@link ToscaPolicyType} object with a null key. + */ + public ToscaPolicyType() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaPolicyType} object with the given concept key. + * + * @param key the key + */ + public ToscaPolicyType(@NonNull final PfConceptKey key) { + super(key); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaPolicyType(final ToscaPolicyType copyConcept) { + super(copyConcept); + } + + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (properties != null) { + for (ToscaProperty property : properties) { + keyList.addAll(property.getKeys()); + } + } + + if (targets != null) { + keyList.addAll(targets); + } + + if (triggers != null) { + for (ToscaTrigger trigger : triggers) { + keyList.addAll(trigger.getKeys()); + } + } + + return keyList; + } + + @Override + public void clean() { + super.clean(); + + if (properties != null) { + for (ToscaProperty property : properties) { + property.clean(); + } + } + + if (targets != null) { + for (PfConceptKey target : targets) { + target.clean(); + } + } + + if (triggers != null) { + for (ToscaTrigger trigger : triggers) { + trigger.clean(); + } + } + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (properties != null) { + result = validateProperties(result); + } + + if (targets != null) { + result = validateTargets(result); + } + + if (triggers != null) { + result = validateTriggers(result); + } + + return result; + } + + /** + * Validate the policy properties. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateProperties(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (ToscaProperty property : properties) { + if (property == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "policy property may not be null ")); + } else { + result = property.validate(result); + } + } + return result; + } + + /** + * Validate the policy targets. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateTargets(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (PfConceptKey target : targets) { + if (target == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "policy target may not be null ")); + } else { + result = target.validate(result); + } + } + return result; + } + + /** + * Validate the policy triggers. + * + * @param result The result of validations up to now + * @return the validation result + */ + private PfValidationResult validateTriggers(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + for (ToscaTrigger trigger : triggers) { + if (trigger == null) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "policy trigger may not be null ")); + } else { + result = trigger.validate(result); + } + } + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaPolicyType other = (ToscaPolicyType) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + int retVal = PfUtils.compareObjects(properties, other.properties); + if (retVal != 0) { + return retVal; + } + + retVal = PfUtils.compareObjects(targets, other.targets); + if (retVal != 0) { + return retVal; + } + + return PfUtils.compareObjects(triggers, other.triggers); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final ToscaPolicyType copy = ((ToscaPolicyType) copyObject); + super.copyTo(target); + + final List<ToscaProperty> newProperties = new ArrayList<>(); + + if (properties == null) { + copy.setProperties(null); + } else { + for (final ToscaProperty property : properties) { + newProperties.add(new ToscaProperty(property)); + } + copy.setProperties(newProperties); + } + + if (targets == null) { + copy.setTargets(null); + } else { + final List<PfConceptKey> newTargets = new ArrayList<>(); + for (final PfConceptKey oldTarget : targets) { + newTargets.add(new PfConceptKey(oldTarget)); + } + copy.setTargets(newTargets); + } + + if (triggers == null) { + copy.setTargets(null); + } else { + final List<ToscaTrigger> newTriggers = new ArrayList<>(); + for (final ToscaTrigger trigger : triggers) { + newTriggers.add(new ToscaTrigger(trigger)); + } + copy.setTriggers(newTriggers); + } + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypes.java new file mode 100644 index 000000000..e33e6c0c9 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaPolicyTypes.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.Map; +import java.util.TreeMap; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * This class is a container for TOSCA policy types. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaPolicyTypes") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaPolicyTypes extends PfConceptContainer<ToscaPolicyType> { + private static final long serialVersionUID = -4157979965271220098L; + + /** + * The Default Constructor creates a {@link ToscaPolicyTypes} object with a null artifact key + * and creates an empty concept map. + */ + public ToscaPolicyTypes() { + super(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaPolicyTypes} object with the given artifact key and + * creates an empty concept map. + * + * @param key the concept key + */ + public ToscaPolicyTypes(final PfConceptKey key) { + super(key, new TreeMap<PfConceptKey, ToscaPolicyType>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public ToscaPolicyTypes(final PfConceptKey key, final Map<PfConceptKey, ToscaPolicyType> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaPolicyTypes(final ToscaPolicyTypes copyConcept) { + super(copyConcept); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaProperty.java new file mode 100644 index 000000000..ab72ff60e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaProperty.java @@ -0,0 +1,308 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the property in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaProperty") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaProperty extends PfConcept { + private static final long serialVersionUID = 1675770231921107988L; + + public enum Status { + SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED + } + + @EmbeddedId + private PfReferenceKey key; + + @Column + private PfConceptKey type; + + @Column + private String description; + + @Column + private boolean required = false; + + @Column(name = "default") + @SerializedName("default") + private PfKey defaultValue; + + @Column + @NonNull + private Status status = Status.SUPPORTED; + + @ElementCollection + private List<ToscaConstraint> constraints; + + @Column + @SerializedName("entry_schema") + private ToscaEntrySchema entrySchema; + + /** + * The Default Constructor creates a {@link ToscaProperty} object with a null key. + */ + public ToscaProperty() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaProperty} object with the given concept key. + * + * @param key the key + */ + public ToscaProperty(@NonNull final PfReferenceKey key) { + this(key, new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaProperty} object with the given concept key. + * + * @param key the key + * @param type the key of the property type + */ + public ToscaProperty(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) { + this.key = key; + this.type = type; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaProperty(final ToscaProperty copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + + keyList.addAll(type.getKeys()); + + if (defaultValue != null) { + keyList.addAll(defaultValue.getKeys()); + } + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + keyList.addAll(constraint.getKeys()); + } + } + + if (entrySchema != null) { + keyList.addAll(entrySchema.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + key.clean(); + + type.clean(); + + if (description != null) { + description = description.trim(); + } + + if (defaultValue != null) { + defaultValue.clean(); + } + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + constraint.clean(); + } + } + + if (entrySchema != null) { + entrySchema.clean(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (type == null || type.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property type may not be null")); + } + + return validateFields(result); + } + + /** + * Validate the property fields. + * + * @param resultIn the incoming validation results so far + * @return the validation results including this validation + */ + private PfValidationResult validateFields(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property description may not be blank")); + } + + if (defaultValue != null && defaultValue.isNullKey()) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property default value may not be null")); + } + + if (constraints != null) { + for (ToscaConstraint constraint : constraints) { + if (constraint == null) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property constraint may not be null ")); + } else { + result = constraint.validate(result); + } + } + } + return (entrySchema != null ? entrySchema.validate(result) : result); + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaProperty other = (ToscaProperty) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + return compareFields(other); + } + + /** + * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty + * object. + * + * @param other the other ToscaProperty object + */ + private int compareFields(final ToscaProperty other) { + if (!type.equals(other.type)) { + return type.compareTo(other.type); + } + + int result = ObjectUtils.compare(description, other.description); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(required, other.required); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(defaultValue, other.defaultValue); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(status, other.status); + if (result != 0) { + return result; + } + + result = PfUtils.compareObjects(constraints, other.constraints); + if (result != 0) { + return result; + } + + return entrySchema.compareTo(other.entrySchema); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + Assertions.instanceOf(target, ToscaProperty.class); + + final ToscaProperty copy = ((ToscaProperty) target); + copy.setKey(new PfReferenceKey(key)); + copy.setType(new PfConceptKey(type)); + copy.setDescription(description); + copy.setRequired(required); + copy.setDefaultValue(defaultValue); + copy.setStatus(status); + copy.constraints = constraints; // Constraints are immutable + copy.setEntrySchema(entrySchema != null ? new ToscaEntrySchema(entrySchema) : null); + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplate.java new file mode 100644 index 000000000..acf231a3e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplate.java @@ -0,0 +1,222 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA + * service template are implemented. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaServiceTemplate") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaServiceTemplate extends ToscaEntityType { + private static final long serialVersionUID = 8084846046148349401L; + + @Column + @SerializedName("tosca_definitions_version") + private String toscaDefinitionsVersion; + + @OneToOne(cascade = CascadeType.ALL) + @SerializedName("data_types") + private ToscaDataTypes dataTypes; + + @OneToOne(cascade = CascadeType.ALL) + @SerializedName("policy_types") + private ToscaPolicyTypes policyTypes; + + @SerializedName("topology_template") + private ToscaTopologyTemplate topologyTemplate; + + + /** + * The Default Constructor creates a {@link ToscaServiceTemplate} object with a null key. + */ + public ToscaServiceTemplate() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaServiceTemplate} object with the given concept key. + * + * @param key the key + */ + public ToscaServiceTemplate(@NonNull final PfConceptKey key) { + this(key, ""); + } + + /** + * The full constructor creates a {@link ToscaServiceTemplate} object with all mandatory + * parameters. + * + * @param key the key + * @param toscaDefinitionsVersion the TOSCA version string + */ + public ToscaServiceTemplate(@NonNull final PfConceptKey key, @NonNull final String toscaDefinitionsVersion) { + super(key); + this.toscaDefinitionsVersion = toscaDefinitionsVersion; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaServiceTemplate(final ToscaServiceTemplate copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (dataTypes != null) { + keyList.addAll(dataTypes.getKeys()); + } + + if (policyTypes != null) { + keyList.addAll(policyTypes.getKeys()); + } + + if (topologyTemplate != null) { + keyList.addAll(topologyTemplate.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + toscaDefinitionsVersion = toscaDefinitionsVersion.trim(); + + if (dataTypes != null) { + dataTypes.clean(); + } + + if (policyTypes != null) { + policyTypes.clean(); + } + + if (topologyTemplate != null) { + topologyTemplate.clean(); + } + } + + @Override + public PfValidationResult validate(final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + if (!ParameterValidationUtils.validateStringParameter(toscaDefinitionsVersion)) { + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, + "service template tosca definitions version may not be null")); + } + + if (dataTypes != null) { + result = dataTypes.validate(result); + } + + if (policyTypes != null) { + result = policyTypes.validate(result); + } + + return (topologyTemplate != null ? topologyTemplate.validate(result) : result); + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaServiceTemplate other = (ToscaServiceTemplate) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(dataTypes, other.dataTypes); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(policyTypes, other.policyTypes); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(topologyTemplate, other.topologyTemplate); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final ToscaServiceTemplate copy = ((ToscaServiceTemplate) copyObject); + super.copyTo(target); + copy.setToscaDefinitionsVersion(toscaDefinitionsVersion); + + copy.setDataTypes(dataTypes != null ? new ToscaDataTypes(dataTypes) : null); + copy.setPolicyTypes(policyTypes != null ? new ToscaPolicyTypes(policyTypes) : null); + copy.setTopologyTemplate(topologyTemplate != null ? new ToscaTopologyTemplate(topologyTemplate) : null); + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplates.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplates.java new file mode 100644 index 000000000..bd1620747 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaServiceTemplates.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.Map; +import java.util.TreeMap; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; + +/** + * This class is a container for TOSCA service templates. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaServiceTemplates") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = true) +public class ToscaServiceTemplates extends PfConceptContainer<ToscaServiceTemplate> { + private static final long serialVersionUID = -3053257884307604114L; + + /** + * The Default Constructor creates a {@link ToscaServiceTemplates} object with a null artifact + * key and creates an empty concept map. + */ + public ToscaServiceTemplates() { + super(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaServiceTemplates} object with the given artifact + * key and creates an empty concept map. + * + * @param key the concept key + */ + public ToscaServiceTemplates(final PfConceptKey key) { + super(key, new TreeMap<PfConceptKey, ToscaServiceTemplate>()); + } + + /** + * This Constructor creates an concept container with all of its fields defined. + * + * @param key the concept container key + * @param conceptMap the concepts to be stored in the concept container + */ + public ToscaServiceTemplates(final PfConceptKey key, final Map<PfConceptKey, ToscaServiceTemplate> conceptMap) { + super(key, conceptMap); + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaServiceTemplates(final ToscaServiceTemplates copyConcept) { + super(copyConcept); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTimeInterval.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTimeInterval.java new file mode 100644 index 000000000..10ee6cc7d --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTimeInterval.java @@ -0,0 +1,188 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.util.Date; +import java.util.List; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the TimeInterval in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + * + */ +@Entity +@Table(name = "ToscaTimeInterval") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaTimeInterval extends PfConcept { + private static final long serialVersionUID = 9151467029611969980L; + + @EmbeddedId + private PfReferenceKey key; + + @SerializedName("start_time") + private Date startTime; + + @SerializedName("end_time") + private Date endTime; + + /** + * The Default Constructor creates a {@link ToscaTimeInterval} object with a null key. + */ + public ToscaTimeInterval() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaTimeInterval} object with the given concept key. + * + * @param key the key + */ + public ToscaTimeInterval(@NonNull final PfReferenceKey key) { + this(key, new Date(0), new Date(0)); + } + + /** + * The full constructor creates a {@link ToscaTimeInterval} object with all fields. + * + * @param key the key + */ + public ToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Date startTime, + @NonNull final Date endTime) { + this.key = key; + this.startTime = startTime; + this.endTime = endTime; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaTimeInterval(final ToscaTimeInterval copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + return getKey().getKeys(); + } + + @Override + public void clean() { + key.clean(); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (startTime == null || startTime.getTime() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "start time on time interval may not be null or zero")); + } + + if (endTime == null || endTime.getTime() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "end time on time interval may not be null or zero")); + } + + if (startTime != null && endTime != null && endTime.before(startTime)) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "end time \"" + endTime.toString() + "\" on time interval may not be before start time \"" + + startTime.toString() + "\"")); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaTimeInterval other = (ToscaTimeInterval) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + int returnVal = PfUtils.compareObjects(startTime, other.startTime); + if (returnVal != 0) { + return returnVal; + } + + return PfUtils.compareObjects(endTime, other.endTime); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, ToscaTimeInterval.class); + + final ToscaTimeInterval copy = ((ToscaTimeInterval) copyObject); + copy.setKey(new PfReferenceKey(key)); + copy.setStartTime(startTime); + copy.setEndTime(endTime); + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java new file mode 100644 index 000000000..e8541f514 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java @@ -0,0 +1,178 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA + * topology template are implemented. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaTopologyTemplate") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaTopologyTemplate extends PfConcept { + private static final long serialVersionUID = 8969698734673232603L; + + @EmbeddedId + private PfConceptKey key; + + @Column(name = "description") + private String description; + + @OneToOne(cascade = CascadeType.ALL) + private ToscaPolicies policies; + + /** + * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key. + */ + public ToscaTopologyTemplate() { + this(new PfConceptKey()); + } + + /** + * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept + * key. + * + * @param key the key + */ + public ToscaTopologyTemplate(@NonNull final PfConceptKey key) { + this.key = key; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaTopologyTemplate(final ToscaTopologyTemplate copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + + if (policies != null) { + keyList.addAll(policies.getKeys()); + } + + return keyList; + } + + @Override + public void clean() { + key.clean(); + + description = (description != null ? description.trim() : null); + + if (policies != null) { + policies.clean(); + } + } + + @Override + public PfValidationResult validate(PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "property description may not be blank")); + } + + if (policies != null) { + result = policies.validate(result); + } + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaTopologyTemplate other = (ToscaTopologyTemplate) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + int result = ObjectUtils.compare(description, other.description); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(policies, other.policies); + } + + @Override + public PfConcept copyTo(@NonNull PfConcept target) { + final Object copyObject = target; + Assertions.instanceOf(copyObject, PfConcept.class); + + final ToscaTopologyTemplate copy = ((ToscaTopologyTemplate) copyObject); + copy.setKey(new PfConceptKey(key)); + copy.setDescription(description); + copy.setPolicies(policies != null ? new ToscaPolicies(policies) : null); + + return copy; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTrigger.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTrigger.java new file mode 100644 index 000000000..5f3b6392f --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTrigger.java @@ -0,0 +1,335 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.time.Duration; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Class to represent the trigger of policy type in TOSCA definition. + * + * @author Chenfei Gao (cgao@research.att.com) + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Entity +@Table(name = "ToscaTrigger") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class ToscaTrigger extends PfConcept { + private static final long serialVersionUID = -6515211640208986971L; + + @EmbeddedId + private PfReferenceKey key; + + @Column + private String description; + + @Column + @SerializedName("event_type") + private String eventType; + + @Column + @SerializedName("schedule") + private ToscaTimeInterval schedule; + + @Column + @SerializedName("target_filter") + private ToscaEventFilter targetFilter; + + @Column + private ToscaConstraint condition; + + @Column + private ToscaConstraint constraint; + + @Column + @SerializedName("period") + private Duration period; + + @Column + private int evaluations = 0; + + @Column + private String method; + + @Column + private String action; + + /** + * The Default Constructor creates a {@link ToscaTrigger} object with a null key. + */ + public ToscaTrigger() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaTrigger} object with the given concept key. + * + * @param key the key + */ + public ToscaTrigger(@NonNull final PfReferenceKey key) { + this(key, "", ""); + } + + /** + * The full Constructor creates a {@link ToscaTrigger} object with all mandatory objects. + * + * @param key the key + * @param eventType the event type + * @param action the trigger action + */ + public ToscaTrigger(@NonNull final PfReferenceKey key, @NonNull final String eventType, + @NonNull final String action) { + this.key = key; + this.eventType = eventType; + this.action = action; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaTrigger(final ToscaTrigger copyConcept) { + super(copyConcept); + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + if (schedule != null) { + keyList.addAll(schedule.getKeys()); + } + if (targetFilter != null) { + keyList.addAll(targetFilter.getKeys()); + } + if (condition != null) { + keyList.addAll(condition.getKeys()); + } + if (constraint != null) { + keyList.addAll(constraint.getKeys()); + } + return keyList; + } + + @Override + public void clean() { + key.clean(); + + description = (description != null ? description.trim() : description); + eventType = eventType.trim(); + + if (schedule != null) { + schedule.clean(); + } + if (targetFilter != null) { + targetFilter.clean(); + } + if (condition != null) { + condition.clean(); + } + if (constraint != null) { + constraint.clean(); + } + + method = (method != null ? method.trim() : method); + action = action.trim(); + } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + if (key.isNullKey()) { + result.addValidationMessage( + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + } + + result = key.validate(result); + + if (description != null && description.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "trigger description may not be blank")); + } + + if (!ParameterValidationUtils.validateStringParameter(eventType)) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "event type on trigger must be defined")); + } + + result = validateOptionalFields(result); + + if (evaluations < 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "evaluations on trigger must be zero or a positive integer")); + } + + if (method != null && method.trim().length() == 0) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "method on trigger may not be blank")); + } + + if (!ParameterValidationUtils.validateStringParameter(action)) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "action on trigger must be defined")); + } + + return result; + } + + /** + * Validate optional fields. + * + * @param resultIn the validation result so far + * @return the validation resutls including these fields + */ + private PfValidationResult validateOptionalFields(final PfValidationResult resultIn) { + PfValidationResult result = resultIn; + + result = (schedule != null ? schedule.validate(result) : result); + result = (targetFilter != null ? targetFilter.validate(result) : result); + result = (condition != null ? condition.validate(result) : result); + result = (constraint != null ? constraint.validate(result) : result); + + return result; + } + + @Override + public int compareTo(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return this.hashCode() - otherConcept.hashCode(); + } + + final ToscaTrigger other = (ToscaTrigger) otherConcept; + if (!key.equals(other.key)) { + return key.compareTo(other.key); + } + + return compareFields(other); + } + + /** + * Compare the fields of this ToscaTrigger object with the fields of the other ToscaProperty + * object. + * + * @param other the other ToscaTrigger object + */ + private int compareFields(final ToscaTrigger other) { + int result = ObjectUtils.compare(description, other.description); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(eventType, other.eventType); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(schedule, other.schedule); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(targetFilter, other.targetFilter); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(condition, other.condition); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(constraint, other.constraint); + if (result != 0) { + return result; + } + + result = ObjectUtils.compare(period, other.period); + if (result != 0) { + return result; + } + + if (evaluations != other.evaluations) { + return evaluations - other.evaluations; + } + + result = ObjectUtils.compare(method, other.method); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(action, other.action); + } + + @Override + public PfConcept copyTo(@NonNull final PfConcept target) { + Assertions.instanceOf(target, ToscaTrigger.class); + + final ToscaTrigger copy = ((ToscaTrigger) target); + copy.setKey(new PfReferenceKey(key)); + copy.setDescription(description); + copy.setEventType(eventType); + copy.setSchedule(schedule != null ? new ToscaTimeInterval(schedule) : null); + copy.setTargetFilter(targetFilter != null ? new ToscaEventFilter(targetFilter) : null); + copy.setCondition(condition); + copy.setConstraint(constraint); + copy.setPeriod(period); + copy.setEvaluations(evaluations); + copy.setMethod(method); + copy.setAction(action); + + return copy; + } +} |