86d604da7c3957349461d80ad443567d3eb9e50e
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
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;
44
45 import java.util.Collection;
46
47 public class NetworkManagerImpl implements NetworkManager {
48   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
49
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";
56
57   public NetworkManagerImpl(NetworkDao networkDao,
58                             CompositionEntityDataManager compositionEntityDataManager) {
59     this.networkDao = networkDao;
60     this.compositionEntityDataManager = compositionEntityDataManager;
61   }
62
63   @Override
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);
67
68     return networkDao.list(new NetworkEntity(vspId, version, null));
69   }
70
71   @Override
72   public NetworkEntity createNetwork(NetworkEntity network) {
73     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, network.getVspId());
74
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())
81               .build());
82     }
83
84     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, network.getVspId());
85
86     return null;
87   }
88
89   @Override
90   public CompositionEntityValidationData updateNetwork(NetworkEntity network) {
91     MDC_DATA_DEBUG_MESSAGE
92         .debugEntryMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
93
94     NetworkEntity retrieved = getValidatedNetwork(network.getVspId(), network.getVersion(), network.getId());
95
96     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
97     schemaInput.setManual(!VSP_INFO_DAO.isManual(network.getVspId(), network.getVersion()));
98     schemaInput.setNetwork(retrieved.getNetworkCompositionData());
99
100     CompositionEntityValidationData validationData = compositionEntityDataManager
101         .validateEntity(network, SchemaTemplateContext.composition, schemaInput);
102     if (CollectionUtils.isEmpty(validationData.getErrors())) {
103       networkDao.update(network);
104     }
105
106     MDC_DATA_DEBUG_MESSAGE
107         .debugExitMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
108
109     return validationData;
110   }
111
112   @Override
113   public CompositionEntityResponse<Network> getNetwork(String vspId, Version version,
114                                                        String networkId) {
115     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
116
117     NetworkEntity networkEntity = getValidatedNetwork(vspId, version, networkId);
118     Network network = networkEntity.getNetworkCompositionData();
119
120     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
121     schemaInput.setManual(!VSP_INFO_DAO.isManual(vspId, version));
122     schemaInput.setNetwork(network);
123
124     CompositionEntityResponse<Network> response = new CompositionEntityResponse<>();
125     response.setId(networkId);
126     response.setData(network);
127     response.setSchema(getCompositionSchema(schemaInput));
128
129     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
130
131     return response;
132   }
133
134
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);
139     return retrieved;
140   }
141
142   @Override
143   public void deleteNetwork(String vspId, Version version, String networkId) {
144     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
145
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());
152     }
153
154     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
155   }
156
157
158   protected String getCompositionSchema(NetworkCompositionSchemaInput schemaInput) {
159     return SchemaGenerator
160         .generate(SchemaTemplateContext.composition, CompositionEntityType.network, schemaInput);
161   }
162 }