a3f7ca062670c69e7541a337122074e3a17dc4ce
[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
31 /**
32  * Represents a transformation from the PNFD transformation descriptor.
33  */
34 @Getter
35 @Setter
36 public class Transformation {
37
38     private String name;
39     private String description;
40     private TransformationBlock block;
41     private Set<TransformationProperty> propertySet;
42     private ConversionQuery conversionQuery;
43     private List<ConversionDefinition> conversionDefinitionList;
44
45     /**
46      * Checks if the instance contains all necessary information to be used.
47      *
48      * @return {code true} if the instance is valid, {code false} otherwise
49      */
50     public boolean isValid() {
51         if (block == TransformationBlock.GET_INPUT_FUNCTION) {
52             return !StringUtils.isEmpty(name) && !CollectionUtils.isEmpty(conversionDefinitionList);
53         }
54         return block != null && conversionQuery != null && !CollectionUtils.isEmpty(conversionDefinitionList);
55     }
56
57     @Override
58     public boolean equals(Object o) {
59         if (this == o) {
60             return true;
61         }
62         if (o == null || getClass() != o.getClass()) {
63             return false;
64         }
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);
70         }
71         //transformations with the same block and query will override themselves.
72         return block == that.block &&
73             Objects.equals(conversionQuery, that.conversionQuery);
74     }
75
76     @Override
77     public int hashCode() {
78         return Objects.hash(block, conversionQuery);
79     }
80
81     public <T> Optional<T> getPropertyValue(final TransformationPropertyType type, Class<T> clazz) {
82         if (CollectionUtils.isEmpty(propertySet)) {
83             return Optional.empty();
84         }
85
86         final Optional<TransformationProperty> transformationProperty = propertySet.stream()
87             .filter(transformationProperty1 -> transformationProperty1.getType() == type)
88             .findFirst();
89         if (transformationProperty.isPresent()) {
90             try {
91                 T value = clazz.cast(transformationProperty.get().getValue());
92                 return Optional.of(value);
93             } catch (final ClassCastException ignored) {
94                 return Optional.empty();
95             }
96         }
97
98         return Optional.empty();
99     }
100 }