ff3e19bd1b67fbd87dbb6291cbe48bca72727aa0
[sdc.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.pnfd.model;
21
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.Set;
26 import lombok.Getter;
27 import lombok.Setter;
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;
32
33 /**
34  * Represents a transformation from the PNFD transformation descriptor.
35  */
36 @Getter
37 @Setter
38 public class Transformation {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(Transformation.class);
41
42     private String name;
43     private String description;
44     private TransformationBlock block;
45     private Set<TransformationProperty> propertySet;
46     private ConversionQuery conversionQuery;
47     private List<ConversionDefinition> conversionDefinitionList;
48
49     /**
50      * Checks if the instance contains all necessary information to be used.
51      *
52      * @return {code true} if the instance is valid, {code false} otherwise
53      */
54     public boolean isValid() {
55         if (block == TransformationBlock.GET_INPUT_FUNCTION) {
56             return !StringUtils.isEmpty(name) && !CollectionUtils.isEmpty(conversionDefinitionList);
57         }
58         return block != null && conversionQuery != null && !CollectionUtils.isEmpty(conversionDefinitionList);
59     }
60
61     @Override
62     public boolean equals(Object o) {
63         if (this == o) {
64             return true;
65         }
66         if (o == null || getClass() != o.getClass()) {
67             return false;
68         }
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);
74         }
75         //transformations with the same block and query will override themselves.
76         return block == that.block &&
77             Objects.equals(conversionQuery, that.conversionQuery);
78     }
79
80     @Override
81     public int hashCode() {
82         return Objects.hash(block, conversionQuery);
83     }
84
85     public <T> Optional<T> getPropertyValue(final TransformationPropertyType type, Class<T> clazz) {
86         if (CollectionUtils.isEmpty(propertySet)) {
87             return Optional.empty();
88         }
89
90         final Optional<TransformationProperty> transformationProperty = propertySet.stream()
91             .filter(transformationProperty1 -> transformationProperty1.getType() == type)
92             .findFirst();
93         if (transformationProperty.isPresent()) {
94             try {
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();
100             }
101         }
102
103         return Optional.empty();
104     }
105 }