2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * 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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.datatypes.error.ErrorLevel;
22 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
23 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
24 import org.openecomp.sdc.logging.types.LoggerConstants;
25 import org.openecomp.sdc.logging.types.LoggerErrorCode;
26 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
27 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
32 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
33 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NetworkCompositionSchemaInput;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
41 import org.openecomp.sdc.versioning.VersioningUtil;
42 import org.openecomp.sdc.versioning.dao.types.Version;
44 import java.util.Collection;
46 public class NetworkManagerImpl implements NetworkManager {
47 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
49 private final NetworkDao networkDao;
50 private final CompositionEntityDataManager compositionEntityDataManager;
51 private final VendorSoftwareProductInfoDao VSPInfoDao;
53 private static final String VSP_ID = "VSP id";
54 private static final String VSP_ID_NETWORK_ID = "VSP id, network id";
56 public NetworkManagerImpl(NetworkDao networkDao,
57 CompositionEntityDataManager compositionEntityDataManager,
58 VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao) {
59 this.networkDao = networkDao;
60 this.compositionEntityDataManager = compositionEntityDataManager;
61 this.VSPInfoDao = vendorSoftwareProductInfoDao;
65 public Collection<NetworkEntity> listNetworks(String vspId, Version version) {
66 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
67 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
69 return networkDao.list(new NetworkEntity(vspId, version, null));
73 public NetworkEntity createNetwork(NetworkEntity network) {
74 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, network.getVspId());
76 if (!VSPInfoDao.isManual(network.getVspId(), network.getVersion())) {
77 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
78 LoggerTragetServiceName.CREATE_NETWORK, ErrorLevel.ERROR.name(),
79 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create network");
80 throw new CoreException(
81 new CompositionEditNotAllowedErrorBuilder(network.getVspId(), network.getVersion())
85 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, network.getVspId());
91 public CompositionEntityValidationData updateNetwork(NetworkEntity network) {
92 MDC_DATA_DEBUG_MESSAGE
93 .debugEntryMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
95 NetworkEntity retrieved = getValidatedNetwork(network.getVspId(), network.getVersion(), network.getId());
97 NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
98 schemaInput.setManual(!VSPInfoDao.isManual(network.getVspId(), network.getVersion()));
99 schemaInput.setNetwork(retrieved.getNetworkCompositionData());
101 CompositionEntityValidationData validationData = compositionEntityDataManager
102 .validateEntity(network, SchemaTemplateContext.composition, schemaInput);
103 if (CollectionUtils.isEmpty(validationData.getErrors())) {
104 networkDao.update(network);
107 MDC_DATA_DEBUG_MESSAGE
108 .debugExitMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
110 return validationData;
114 public CompositionEntityResponse<Network> getNetwork(String vspId, Version version,
116 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
118 NetworkEntity networkEntity = getValidatedNetwork(vspId, version, networkId);
119 Network network = networkEntity.getNetworkCompositionData();
121 NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
122 schemaInput.setManual(!VSPInfoDao.isManual(vspId, version));
123 schemaInput.setNetwork(network);
125 CompositionEntityResponse<Network> response = new CompositionEntityResponse<>();
126 response.setId(networkId);
127 response.setData(network);
128 response.setSchema(getCompositionSchema(schemaInput));
130 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
136 private NetworkEntity getValidatedNetwork(String vspId, Version version, String networkId) {
137 NetworkEntity retrieved = networkDao.get(new NetworkEntity(vspId, version, networkId));
138 VersioningUtil.validateEntityExistence(retrieved, new NetworkEntity(vspId, version, networkId),
139 VspDetails.ENTITY_TYPE);
144 public void deleteNetwork(String vspId, Version version, String networkId) {
145 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
147 if (!VSPInfoDao.isManual(vspId, version)) {
148 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
149 LoggerTragetServiceName.DELETE_NETWORK, ErrorLevel.ERROR.name(),
150 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete network");
151 throw new CoreException(
152 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
155 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
159 protected String getCompositionSchema(NetworkCompositionSchemaInput schemaInput) {
160 return SchemaGenerator
161 .generate(SchemaTemplateContext.composition, CompositionEntityType.network, schemaInput);