c2408308de61733b338938c4eab353773fcda721
[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.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;
43
44 import java.util.Collection;
45
46 public class NetworkManagerImpl implements NetworkManager {
47   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
48
49   private final NetworkDao networkDao;
50   private final CompositionEntityDataManager compositionEntityDataManager;
51   private final VendorSoftwareProductInfoDao VSPInfoDao;
52
53   private static final  String VSP_ID = "VSP id";
54   private static final String VSP_ID_NETWORK_ID = "VSP id, network id";
55
56   public NetworkManagerImpl(NetworkDao networkDao,
57                             CompositionEntityDataManager compositionEntityDataManager,
58                             VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao) {
59     this.networkDao = networkDao;
60     this.compositionEntityDataManager = compositionEntityDataManager;
61     this.VSPInfoDao = vendorSoftwareProductInfoDao;
62   }
63
64   @Override
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);
68
69     return networkDao.list(new NetworkEntity(vspId, version, null));
70   }
71
72   @Override
73   public NetworkEntity createNetwork(NetworkEntity network) {
74     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID, network.getVspId());
75
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())
82               .build());
83     }
84
85     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, network.getVspId());
86
87     return null;
88   }
89
90   @Override
91   public CompositionEntityValidationData updateNetwork(NetworkEntity network) {
92     MDC_DATA_DEBUG_MESSAGE
93         .debugEntryMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
94
95     NetworkEntity retrieved = getValidatedNetwork(network.getVspId(), network.getVersion(), network.getId());
96
97     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
98     schemaInput.setManual(!VSPInfoDao.isManual(network.getVspId(), network.getVersion()));
99     schemaInput.setNetwork(retrieved.getNetworkCompositionData());
100
101     CompositionEntityValidationData validationData = compositionEntityDataManager
102         .validateEntity(network, SchemaTemplateContext.composition, schemaInput);
103     if (CollectionUtils.isEmpty(validationData.getErrors())) {
104       networkDao.update(network);
105     }
106
107     MDC_DATA_DEBUG_MESSAGE
108         .debugExitMessage(VSP_ID_NETWORK_ID, network.getVspId(), network.getId());
109
110     return validationData;
111   }
112
113   @Override
114   public CompositionEntityResponse<Network> getNetwork(String vspId, Version version,
115                                                        String networkId) {
116     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
117
118     NetworkEntity networkEntity = getValidatedNetwork(vspId, version, networkId);
119     Network network = networkEntity.getNetworkCompositionData();
120
121     NetworkCompositionSchemaInput schemaInput = new NetworkCompositionSchemaInput();
122     schemaInput.setManual(!VSPInfoDao.isManual(vspId, version));
123     schemaInput.setNetwork(network);
124
125     CompositionEntityResponse<Network> response = new CompositionEntityResponse<>();
126     response.setId(networkId);
127     response.setData(network);
128     response.setSchema(getCompositionSchema(schemaInput));
129
130     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
131
132     return response;
133   }
134
135
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);
140     return retrieved;
141   }
142
143   @Override
144   public void deleteNetwork(String vspId, Version version, String networkId) {
145     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_NETWORK_ID, vspId, networkId);
146
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());
153     }
154
155     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_NETWORK_ID, vspId, networkId);
156   }
157
158
159   protected String getCompositionSchema(NetworkCompositionSchemaInput schemaInput) {
160     return SchemaGenerator
161         .generate(SchemaTemplateContext.composition, CompositionEntityType.network, schemaInput);
162   }
163 }