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.VendorSoftwareProductInfoDaoFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
33 import org.openecomp.sdc.vendorsoftwareproduct.errors.CompositionEditNotAllowedErrorBuilder;
34 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.NetworkCompositionSchemaInput;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
42 import org.openecomp.sdc.versioning.VersioningUtil;
43 import org.openecomp.sdc.versioning.dao.types.Version;
45 import java.util.Collection;
47 public class NetworkManagerImpl implements NetworkManager {
48 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
50 private final NetworkDao networkDao;
51 private final CompositionEntityDataManager compositionEntityDataManager;
52 private static final VendorSoftwareProductInfoDao VSP_INFO_DAO
53 = VendorSoftwareProductInfoDaoFactory.getInstance().createInterface();
54 private static final String VSP_ID = "VSP id";
55 private static final String VSP_ID_NETWORK_ID = "VSP id, network id";
57 public NetworkManagerImpl(NetworkDao networkDao,
58 CompositionEntityDataManager compositionEntityDataManager) {
59 this.networkDao = networkDao;
60 this.compositionEntityDataManager = compositionEntityDataManager;
64 public Collection<NetworkEntity> listNetworks(String vspId, Version version) {
65 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, vspId);
66 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
68 return networkDao.list(new NetworkEntity(vspId, version, null));
72 public NetworkEntity createNetwork(NetworkEntity network) {
73 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, network.getVspId());
75 if (!VSP_INFO_DAO.isManual(network.getVspId(), network.getVersion())) {
76 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
77 LoggerTragetServiceName.CREATE_NETWORK, ErrorLevel.ERROR.name(),
78 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't create network");
79 throw new CoreException(
80 new CompositionEditNotAllowedErrorBuilder(network.getVspId(), network.getVersion())
84 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, network.getVspId());
90 public CompositionEntityValidationData updateNetwork(NetworkEntity network) {
91 MDC_DATA_DEBUG_MESSAGE
92 .debugEntryMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
94 NetworkEntity retrieved = getValidatedNetwork(network.getVspId(), network.getVersion(), network.getId());
96 NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
97 schemaInput.setManual(!VSP_INFO_DAO.isManual(network.getVspId(), network.getVersion()));
98 schemaInput.setNetwork(retrieved.getNetworkCompositionData());
100 CompositionEntityValidationData validationData = compositionEntityDataManager
101 .validateEntity(network, SchemaTemplateContext.composition, schemaInput);
102 if (CollectionUtils.isEmpty(validationData.getErrors())) {
103 networkDao.update(network);
106 MDC_DATA_DEBUG_MESSAGE
107 .debugExitMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
109 return validationData;
113 public CompositionEntityResponse<Network> getNetwork(String vspId, Version version,
115 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
117 NetworkEntity networkEntity = getValidatedNetwork(vspId, version, networkId);
118 Network network = networkEntity.getNetworkCompositionData();
120 NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
121 schemaInput.setManual(!VSP_INFO_DAO.isManual(vspId, version));
122 schemaInput.setNetwork(network);
124 CompositionEntityResponse<Network> response = new CompositionEntityResponse<>();
125 response.setId(networkId);
126 response.setData(network);
127 response.setSchema(getCompositionSchema(schemaInput));
129 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
135 private NetworkEntity getValidatedNetwork(String vspId, Version version, String networkId) {
136 NetworkEntity retrieved = networkDao.get(new NetworkEntity(vspId, version, networkId));
137 VersioningUtil.validateEntityExistence(retrieved, new NetworkEntity(vspId, version, networkId),
138 VspDetails.ENTITY_TYPE);
143 public void deleteNetwork(String vspId, Version version, String networkId) {
144 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
146 if (!VSP_INFO_DAO.isManual(vspId, version)) {
147 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
148 LoggerTragetServiceName.DELETE_NETWORK, ErrorLevel.ERROR.name(),
149 LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't delete network");
150 throw new CoreException(
151 new CompositionEditNotAllowedErrorBuilder(vspId, version).build());
154 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
158 protected String getCompositionSchema(NetworkCompositionSchemaInput schemaInput) {
159 return SchemaGenerator
160 .generate(SchemaTemplateContext.composition, CompositionEntityType.network, schemaInput);