aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/NicManagerImpl.java
blob: 0ed58a77038fdf898fa60922a37a4ea72ebfcc27 (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
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * 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.
 */

package org.openecomp.sdc.vendorsoftwareproduct.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCode;
import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.vendorsoftwareproduct.errors.*;
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.CompositionEntityType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NicCompositionSchemaInput;
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.Map;
import java.util.stream.Collectors;

public class NicManagerImpl implements NicManager {
  private final NicDao nicDao;
  private final CompositionEntityDataManager compositionEntityDataManager;
  private final NetworkManager networkManager;
  private final VendorSoftwareProductInfoDao vspInfoDao;

  public NicManagerImpl(NicDao nicDao,
                        CompositionEntityDataManager compositionEntityDataManager,
                        NetworkManager networkManager,
                        VendorSoftwareProductInfoDao vspInfoDao) {
    this.nicDao = nicDao;
    this.compositionEntityDataManager = compositionEntityDataManager;
    this.networkManager = networkManager;
    this.vspInfoDao = vspInfoDao;
  }

  @Override
  public Collection<NicEntity> listNics(String vspId, Version version, String componentId) {
    Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null));

    if (!nics.isEmpty()) {
      Map<String, String> networksNameById = listNetworksNameById(vspId, version);
      nics.forEach(nicEntity -> {
        Nic nic = nicEntity.getNicCompositionData();
        nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
        nicEntity.setNicCompositionData(nic);
      });
    }
    return nics;
  }

  private Map<String, String> listNetworksNameById(String vspId, Version version) {
    Collection<NetworkEntity> networks = networkManager.listNetworks(vspId, version);
    return networks.stream().collect(Collectors.toMap(NetworkEntity::getId,
        networkEntity -> networkEntity.getNetworkCompositionData().getName()));
  }

  @Override
  public NicEntity createNic(NicEntity nic) {
    NicEntity createdNic;
    if (!vspInfoDao.isManual(nic.getVspId(), nic.getVersion())) {
      ErrorCode onboardingMethodUpdateErrorCode = NotSupportedHeatOnboardMethodErrorBuilder
          .getAddNicNotSupportedHeatOnboardMethodErrorBuilder();
      throw new CoreException(onboardingMethodUpdateErrorCode);
    } else {
      validateNic(nic);
      createdNic = compositionEntityDataManager.createNic(nic);
    }
    return createdNic;
  }


  private void validateNic(NicEntity nic) {
    Collection<NicEntity> listNics = listNics(nic.getVspId(), nic.getVersion(), nic
        .getComponentId());
    String networkId = nic.getNicCompositionData().getNetworkId();
    NetworkType networkType = nic.getNicCompositionData().getNetworkType();
    String networkDescription = nic.getNicCompositionData().getNetworkDescription();

    if (!nic.getNicCompositionData().getName()
            .matches(VendorSoftwareProductConstants.NAME_PATTERN)) {
      ErrorCode errorCode = NicErrorBuilder
              .getNicNameFormatErrorBuilder(VendorSoftwareProductConstants.NAME_PATTERN);
      throw new CoreException(errorCode);
    }

    validateNics(nic, listNics);

    if (networkType.equals(NetworkType.Internal)) {
      validateInternalNetworkType(nic, networkId, networkDescription);

    } else if (networkType.equals(NetworkType.External)
            && !(networkId == null || networkId.isEmpty())) {
        final ErrorCode nicNetworkIdNotAllowedExternalNetworkErrorBuilder =
            new NicNetworkIdNotAllowedExternalNetworkErrorBuilder().build();
        throw new CoreException(nicNetworkIdNotAllowedExternalNetworkErrorBuilder);
    }
  }

  private void validateInternalNetworkType(NicEntity nic, String networkId,
                                           String networkDescription) {
    if (!(networkId == null || networkId.isEmpty())) {
          networkManager.getNetwork(nic.getVspId(), nic.getVersion(), networkId);
    }

    if (!(networkDescription == null || networkDescription.isEmpty())) {
      final ErrorCode nicNetworkDescriptionErrorBuilder =
          NicInternalNetworkErrorBuilder.getNetworkDescriptionInternalNetworkErrorBuilder();
      throw new CoreException(nicNetworkDescriptionErrorBuilder);
    }
  }

  private void validateNics(NicEntity nic, Collection<NicEntity> listNics) {
    listNics.forEach(nicEntity -> {
      Nic nicdata = nicEntity.getNicCompositionData();
      if (nic.getNicCompositionData().getName().equalsIgnoreCase(nicdata.getName())) {
        final ErrorCode duplicateNicInComponentErrorBuilder =
            new DuplicateNicInComponentErrorBuilder(nic.getNicCompositionData().getName(),
                nic.getComponentId()).build();
        throw new CoreException(duplicateNicInComponentErrorBuilder);
      }

    });
  }

  @Override
  public CompositionEntityResponse<Nic> getNic(String vspId, Version version, String componentId,
                                               String nicId) {
    NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
    Nic nic = nicEntity.getNicCompositionData();

    NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
    schemaInput.setManual(vspInfoDao.isManual(vspId, version));
    schemaInput.setNic(nic);
    Map<String, String> networksNameById = listNetworksNameById(vspId, version);
    nic.setNetworkName(networksNameById.get(nic.getNetworkId()));
    schemaInput.setNetworkIds(networksNameById.keySet());

    CompositionEntityResponse<Nic> response = new CompositionEntityResponse<>();
    response.setId(nicId);
    response.setData(nic);
    response.setSchema(getNicCompositionSchema(schemaInput));
    return response;
  }


  private NicEntity getValidatedNic(String vspId, Version version, String componentId,
                                    String nicId) {
    NicEntity retrieved = nicDao.get(new NicEntity(vspId, version, componentId, nicId));
    VersioningUtil
        .validateEntityExistence(retrieved, new NicEntity(vspId, version, componentId, nicId),
            VspDetails.ENTITY_TYPE);
    return retrieved;
  }

  @Override
  public void deleteNic(String vspId, Version version, String componentId, String nicId) {
    if (!vspInfoDao.isManual(vspId, version)) {
      final ErrorCode deleteNicErrorBuilder =
          DeleteNicErrorBuilder.getDeleteNicForHeatOnboardedVspErrorBuilder();
      throw new CoreException(deleteNicErrorBuilder);
    }

    NicEntity nicEntity = getValidatedNic(vspId, version, componentId, nicId);
    nicDao.delete(nicEntity);
  }

  @Override
  public CompositionEntityValidationData updateNic(NicEntity nic) {
    NicEntity retrieved =
        getValidatedNic(nic.getVspId(), nic.getVersion(), nic.getComponentId(), nic.getId());

    NicCompositionSchemaInput schemaInput = new NicCompositionSchemaInput();
    schemaInput.setManual(vspInfoDao.isManual(nic.getVspId(), nic.getVersion()));
    schemaInput.setNic(retrieved.getNicCompositionData());

    CompositionEntityValidationData validationData = compositionEntityDataManager
        .validateEntity(nic, SchemaTemplateContext.composition, schemaInput);
    if (CollectionUtils.isEmpty(validationData.getErrors())) {
      nicDao.update(nic);
    }
    return validationData;
  }

  @Override
  public QuestionnaireResponse getNicQuestionnaire(String vspId, Version version,
                                                   String componentId, String nicId) {
    QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse();
    NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version, componentId, nicId);
    VersioningUtil.validateEntityExistence(nicQuestionnaire,
        new NicEntity(vspId, version, componentId, nicId), VspDetails.ENTITY_TYPE);

    questionnaireResponse.setData(nicQuestionnaire.getQuestionnaireData());
    questionnaireResponse.setSchema(getNicQuestionnaireSchema(null));
    return questionnaireResponse;
  }

  @Override
  public void updateNicQuestionnaire(String vspId, Version version, String componentId,
                                     String nicId, String questionnaireData) {
    getNic(vspId, version, componentId, nicId);

    nicDao.updateQuestionnaireData(vspId, version, componentId, nicId, questionnaireData);
  }

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

  protected String getNicCompositionSchema(NicCompositionSchemaInput schemaInput) {
    return SchemaGenerator
        .generate(SchemaTemplateContext.composition, CompositionEntityType.nic, schemaInput);
  }
}