2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.pnfd.model;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang3.StringUtils;
32 * Represents a transformation from the PNFD transformation descriptor.
36 public class Transformation {
39 private String description;
40 private TransformationBlock block;
41 private Set<TransformationProperty> propertySet;
42 private ConversionQuery conversionQuery;
43 private List<ConversionDefinition> conversionDefinitionList;
46 * Checks if the instance contains all necessary information to be used.
48 * @return {code true} if the instance is valid, {code false} otherwise
50 public boolean isValid() {
51 if (block == TransformationBlock.GET_INPUT_FUNCTION) {
52 return !StringUtils.isEmpty(name) && !CollectionUtils.isEmpty(conversionDefinitionList);
54 return block != null && conversionQuery != null && !CollectionUtils.isEmpty(conversionDefinitionList);
58 public boolean equals(Object o) {
62 if (o == null || getClass() != o.getClass()) {
65 final Transformation that = (Transformation) o;
66 //if there is no query, compares by block and name.
67 if (conversionQuery == null && that.conversionQuery == null) {
68 return block == that.block &&
69 Objects.equals(name, that.name);
71 //transformations with the same block and query will override themselves.
72 return block == that.block &&
73 Objects.equals(conversionQuery, that.conversionQuery);
77 public int hashCode() {
78 return Objects.hash(block, conversionQuery);
81 public <T> Optional<T> getPropertyValue(final TransformationPropertyType type, Class<T> clazz) {
82 if (CollectionUtils.isEmpty(propertySet)) {
83 return Optional.empty();
86 final Optional<TransformationProperty> transformationProperty = propertySet.stream()
87 .filter(transformationProperty1 -> transformationProperty1.getType() == type)
89 if (transformationProperty.isPresent()) {
91 T value = clazz.cast(transformationProperty.get().getValue());
92 return Optional.of(value);
93 } catch (final ClassCastException ignored) {
94 return Optional.empty();
98 return Optional.empty();