summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/attribute/ComponentAttributeDeclarator.java
blob: 56dec5d93aeac0fa5c8f4577fd10f03b0f1fa9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2021, Nordix Foundation. 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.attribute;

import fj.data.Either;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.apache.commons.collections.CollectionUtils;
import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.GetOutputValueDataDefinition;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.AttributeDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.OutputDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.AttributeOperation;

@org.springframework.stereotype.Component
public class ComponentAttributeDeclarator extends DefaultAttributeDeclarator<Component, AttributeDataDefinition> {

  private final ToscaOperationFacade toscaOperationFacade;
  private final AttributeBusinessLogic attributeBusinessLogic;

  public ComponentAttributeDeclarator(final ComponentsUtils componentsUtils,
                                      final AttributeOperation attributeOperation,
                                      final ToscaOperationFacade toscaOperationFacade,
                                      final AttributeBusinessLogic attributeBusinessLogic) {
//    super(componentsUtils, attributeOperation);
    this.toscaOperationFacade = toscaOperationFacade;
    this.attributeBusinessLogic = attributeBusinessLogic;
  }

  @Override
  public AttributeDataDefinition createDeclaredAttribute(final AttributeDataDefinition attributeDataDefinition) {
    return new AttributeDataDefinition(attributeDataDefinition);
  }

  @Override
  public Either<?, StorageOperationStatus> updateAttributesValues(final Component component,
                                                                  final String propertiesOwnerId,
                                                                  final List<AttributeDataDefinition> attributetypeList) {
    if (CollectionUtils.isNotEmpty(attributetypeList)) {
      for (AttributeDataDefinition attribute : attributetypeList) {
        Either<AttributeDefinition, StorageOperationStatus>
            storageStatus = toscaOperationFacade
            .updateAttributeOfComponent(component, new AttributeDefinition(attribute));
        if (storageStatus.isRight()) {
          return Either.right(storageStatus.right().value());
        }
      }
    }
    return Either.left(attributetypeList);
  }

  @Override
  public Optional<Component> resolvePropertiesOwner(final Component component, final String propertiesOwnerId) {
    return Optional.of(component);
  }

  @Override
  public StorageOperationStatus unDeclareAttributesAsOutputs(final Component component,
                                                             final OutputDefinition output) {
    AttributeDefinition attributeDefinition = new AttributeDefinition(output);

    // TODO - do we need this one
    if (attributeBusinessLogic.isAttributeUsedByOperation(component, attributeDefinition)) {
      return StorageOperationStatus.DECLARED_INPUT_USED_BY_OPERATION;
    }

    Optional<AttributeDefinition> attributeToUpdateCandidate =
        getDeclaredAttributeByOutputId(component, output.getUniqueId());

    if (attributeToUpdateCandidate.isPresent()) {
      AttributeDefinition attributeToUpdate = attributeToUpdateCandidate.get();
      return unDeclareOutput(component, output, attributeToUpdate);
    }

    return StorageOperationStatus.OK;
  }

  private StorageOperationStatus unDeclareOutput(final Component component,
                                                 final OutputDefinition output,
                                                 final AttributeDefinition attributeToUpdate) {
    prepareValueBeforeDelete(output, attributeToUpdate, Collections.emptyList());
    attributeToUpdate.setValue(output.getDefaultValue());
    Either<AttributeDefinition, StorageOperationStatus> status = toscaOperationFacade
        .updateAttributeOfComponent(component, attributeToUpdate);
    if (status.isRight()) {
      return status.right().value();
    }

    return StorageOperationStatus.OK;
  }

  private Optional<AttributeDefinition> getDeclaredAttributeByOutputId(final Component component, final String outputId) {
    List<AttributeDefinition> attributes = component.getAttributes();

    if (CollectionUtils.isEmpty(attributes)) {
      return Optional.empty();
    }

    for (AttributeDefinition attributeDefinition : attributes) {
      List<GetOutputValueDataDefinition> getOutputValues = attributeDefinition.getGetOutputValues();
      if (CollectionUtils.isEmpty(getOutputValues)) {
        continue;
      }

      Optional<GetOutputValueDataDefinition> getOutputCandidate =
          getOutputValues.stream().filter(getOutput -> getOutput.getOutputId().equals(outputId)).findAny();

      if (getOutputCandidate.isPresent()) {
        return Optional.of(attributeDefinition);
      }
    }

    return Optional.empty();
  }

}