summaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/ComponentManagerImpl.java
blob: f509658b37b7cfcb22403ce88896b506dbb322c3 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. 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.vendorsoftwareproduct.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCategory;
import org.openecomp.sdc.common.errors.ErrorCode;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
import org.openecomp.sdc.logging.types.LoggerConstants;
import org.openecomp.sdc.logging.types.LoggerErrorCode;
import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentCompositionSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.ComponentQuestionnaireSchemaInput;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
import org.openecomp.sdc.versioning.VersioningUtil;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.COMPUTE_TYPE_PREFIX;

public class ComponentManagerImpl implements ComponentManager {
  private final ComponentDao componentDao;
  private final CompositionEntityDataManager compositionEntityDataManager;
  private final NicManager nicManager;
  private final VendorSoftwareProductInfoDao vspInfoDao;
  private static final String VSP_ID = "VSP id";
  private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";

  public ComponentManagerImpl(ComponentDao componentDao,
                              CompositionEntityDataManager compositionEntityDataManager,
                              NicManager nicManager,
                              VendorSoftwareProductInfoDao vspInfoDao) {
    this.componentDao = componentDao;
    this.compositionEntityDataManager = compositionEntityDataManager;
    this.nicManager = nicManager;
    this.vspInfoDao = vspInfoDao;
  }

  @Override
  public Collection<ComponentEntity> listComponents(String vspId, Version version) {
    return componentDao.list(new ComponentEntity(vspId, version, null));
  }

  @Override
  public void deleteComponents(String vspId, Version version) {
    if (!vspInfoDao.isManual(vspId, version)) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
      throw new CoreException(
          new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
    }
  }

  @Override
  public ComponentEntity createComponent(ComponentEntity component) {
    final String vfcAddNotAllowedInHeatOnboardingMsg =
        "VFCs cannot be added for VSPs onboarded with HEAT.";

    ComponentEntity createdComponent;
    if (!vspInfoDao.isManual(component.getVspId(), component.getVersion())) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component");
      throw new CoreException(
          new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
              .withId(VendorSoftwareProductErrorCodes.VFC_ADD_NOT_ALLOWED_IN_HEAT_ONBOARDING)
              .withMessage(vfcAddNotAllowedInHeatOnboardingMsg).build());
    } else {
      validateComponentManual(component);
      updateComponentName(component);
      createdComponent = compositionEntityDataManager.createComponent(component);
    }
    return createdComponent;
  }

  private void updateComponentName(ComponentEntity component) {
    ComponentData data = component.getComponentCompositionData();
    data.setName(COMPUTE_TYPE_PREFIX + data.getDisplayName());
    component.setComponentCompositionData(data);
  }

  private void validateComponentManual(ComponentEntity component) {
    final String vspVfcCountExceedMsg = "Creation of only one VFC per "
        + "VSP allowed.";

    final String vspVfcDuplicateNameMsg = "VFC with specified name "
        + "already present in given VSP.";

    Collection<ComponentEntity> vspComponentList =
        listComponents(component.getVspId(), component.getVersion());
    if (!vspComponentList.isEmpty()) //1707 release only supports 1 VFC in VSP (manual creation)
    {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
              + "vsp component count exceed");
      throw new CoreException(
          new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
              .withId(VendorSoftwareProductErrorCodes.VSP_VFC_COUNT_EXCEED)
              .withMessage(vspVfcCountExceedMsg).build());
    }
    if (!isVfcNameUnique(vspComponentList,
        component.getComponentCompositionData().getDisplayName())) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.CREATE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create component: "
              + "vsp component duplicate name");
      throw new CoreException(
          new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
              .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
              .withMessage(vspVfcDuplicateNameMsg).build());
    }
  }

  private boolean isVfcNameUnique(Collection<ComponentEntity> component, String displayName) {
    for (ComponentEntity comp : component) {
      if (comp.getComponentCompositionData().getDisplayName().equalsIgnoreCase(displayName)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public CompositionEntityValidationData updateComponent(ComponentEntity component) {
    ComponentEntity retrieved =
        getValidatedComponent(component.getVspId(), component.getVersion(), component.getId());

    boolean isManual = vspInfoDao.isManual(component.getVspId(), component.getVersion());
    if (isManual) {
      validateComponentUpdateManual(retrieved);
    }

    ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
    schemaInput.setManual(isManual);
    schemaInput.setComponent(retrieved.getComponentCompositionData());

    CompositionEntityValidationData validationData = compositionEntityDataManager
        .validateEntity(component, SchemaTemplateContext.composition, schemaInput);
    if (CollectionUtils.isEmpty(validationData.getErrors())) {
      if (isManual) {
        updateComponentName(component);
      }
      componentDao.update(component);
    }
    return validationData;
  }

  private void validateComponentUpdateManual(ComponentEntity component) {
    Collection<ComponentEntity> vspComponentList =
        listComponents(component.getVspId(), component.getVersion());
    //VFC name should be unique within VSP
    //Removing VFC with same ID from list to avoid self compare
    for (ComponentEntity ce : vspComponentList) {
      if (ce.getId().equals(component.getId())) {
        vspComponentList.remove(ce);
        break;
      }
    }
    if (!isVfcNameUnique(vspComponentList, component.getComponentCompositionData()
        .getDisplayName())) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.UPDATE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Component with same name already " +
              "exists for specified VSP");
      throw new CoreException(
          new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
              .withId(VendorSoftwareProductErrorCodes.VSP_VFC_DUPLICATE_NAME)
              .withMessage("VFC with specified name already present in given VSP.").build());

    }
  }

  @Override
  public CompositionEntityResponse<ComponentData> getComponent(String vspId, Version version,
                                                               String componentId) {
    ComponentEntity componentEntity = getValidatedComponent(vspId, version, componentId);
    ComponentData component = componentEntity.getComponentCompositionData();

    ComponentCompositionSchemaInput schemaInput = new ComponentCompositionSchemaInput();
    schemaInput.setManual(vspInfoDao.isManual(vspId, version));
    schemaInput.setComponent(component);

    CompositionEntityResponse<ComponentData> response = new CompositionEntityResponse<>();
    response.setId(componentId);
    response.setData(component);
    response.setSchema(getComponentCompositionSchema(schemaInput));
    return response;
  }

  @Override
  public void deleteComponent(String vspId, Version version, String componentId) {
    if (!vspInfoDao.isManual(vspId, version)) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.DELETE_COMPONENT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete component");
      throw new CoreException(
          new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
    }
  }

  @Override
  public QuestionnaireResponse getQuestionnaire(String vspId, Version version,
                                                String componentId) {
    QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
    ComponentEntity component = componentDao.getQuestionnaireData(vspId, version, componentId);
    VersioningUtil
        .validateEntityExistence(component, new ComponentEntity(vspId, version, componentId),
            VspDetails.ENTITY_TYPE);

    questionnaireResponse.setData(component.getQuestionnaireData());
    List<String> nicNames = nicManager.listNics(vspId, version, componentId).stream()
        .map(nic -> nic.getNicCompositionData().getName()).collect(Collectors.toList());
    questionnaireResponse.setSchema(getComponentQuestionnaireSchema(
        new ComponentQuestionnaireSchemaInput(nicNames, questionnaireResponse.getData() == null
            ? null
            : JsonUtil.json2Object(questionnaireResponse.getData(), Map.class))));
    return questionnaireResponse;
  }

  @Override
  public void updateQuestionnaire(String vspId, Version version, String componentId,
                                  String questionnaireData) {
    validateComponentExistence(vspId, version, componentId);

    componentDao.updateQuestionnaireData(vspId, version, componentId, questionnaireData);
  }

  @Override
  public void validateComponentExistence(String vspId, Version version, String componentId) {
    getValidatedComponent(vspId, version, componentId);
  }

  private ComponentEntity getValidatedComponent(String vspId, Version version, String componentId) {
    ComponentEntity retrieved = componentDao.get(new ComponentEntity(vspId, version, componentId));
    VersioningUtil
        .validateEntityExistence(retrieved, new ComponentEntity(vspId, version, componentId),
            VspDetails.ENTITY_TYPE);
    return retrieved;
  }

  protected String getComponentCompositionSchema(ComponentCompositionSchemaInput schemaInput) {
    return SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.component, schemaInput);
  }

  protected String getComponentQuestionnaireSchema(SchemaTemplateInput schemaInput) {
    return SchemaGenerator
        .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
            schemaInput);
  }
}