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;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
34 * Represents a transformation from the PNFD transformation descriptor.
38 public class Transformation {
40 private static final Logger LOGGER = LoggerFactory.getLogger(Transformation.class);
43 private String description;
44 private TransformationBlock block;
45 private Set<TransformationProperty> propertySet;
46 private ConversionQuery conversionQuery;
47 private List<ConversionDefinition> conversionDefinitionList;
50 * Checks if the instance contains all necessary information to be used.
52 * @return {code true} if the instance is valid, {code false} otherwise
54 public boolean isValid() {
55 if (block == TransformationBlock.GET_INPUT_FUNCTION) {
56 return !StringUtils.isEmpty(name) && !CollectionUtils.isEmpty(conversionDefinitionList);
58 return block != null && conversionQuery != null && !CollectionUtils.isEmpty(conversionDefinitionList);
62 public boolean equals(Object o) {
66 if (o == null || getClass() != o.getClass()) {
69 final Transformation that = (Transformation) o;
70 //if there is no query, compares by block and name.
71 if (conversionQuery == null && that.conversionQuery == null) {
72 return block == that.block &&
73 Objects.equals(name, that.name);
75 //transformations with the same block and query will override themselves.
76 return block == that.block &&
77 Objects.equals(conversionQuery, that.conversionQuery);
81 public int hashCode() {
82 return Objects.hash(block, conversionQuery);
85 public <T> Optional<T> getPropertyValue(final TransformationPropertyType type, Class<T> clazz) {
86 if (CollectionUtils.isEmpty(propertySet)) {
87 return Optional.empty();
90 final Optional<TransformationProperty> transformationProperty = propertySet.stream()
91 .filter(transformationProperty1 -> transformationProperty1.getType() == type)
93 if (transformationProperty.isPresent()) {
95 T value = clazz.cast(transformationProperty.get().getValue());
96 return Optional.of(value);
97 } catch (final ClassCastException e) {
98 LOGGER.warn("Could not get property value.", e);
99 return Optional.empty();
103 return Optional.empty();