2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
23 import com.thinkaurelius.titan.graphdb.query.TitanPredicate;
24 import fj.data.Either;
25 import org.openecomp.sdc.be.config.BeEcompErrorManager;
26 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
27 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
28 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
29 import org.openecomp.sdc.be.datatypes.elements.PolicyTypeDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
34 import org.openecomp.sdc.be.model.operations.api.IPolicyTypeOperation;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.resources.data.PolicyTypeData;
37 import org.openecomp.sdc.be.resources.data.PropertyData;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
43 import java.util.stream.Collectors;
45 import static org.openecomp.sdc.be.dao.titan.TitanUtils.buildNotInPredicate;
47 @Component("policy-type-operation")
48 public class PolicyTypeOperation extends AbstractOperation implements IPolicyTypeOperation {
50 private static final Logger log = Logger.getLogger(PolicyTypeOperation.class.getName());
51 private static final String CREATE_FLOW_CONTEXT = "CreatePolicyType";
52 private static final String GET_FLOW_CONTEXT = "GetPolicyType";
55 private PropertyOperation propertyOperation;
57 private DerivedFromOperation derivedFromOperation;
59 private OperationUtils operationUtils;
62 public Either<PolicyTypeDefinition, StorageOperationStatus> getLatestPolicyTypeByType(String type) {
63 Map<String, Object> mapCriteria = new HashMap<>();
64 mapCriteria.put(GraphPropertiesDictionary.TYPE.getProperty(), type);
65 mapCriteria.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
66 return getPolicyTypeByCriteria(type, mapCriteria);
70 public Either<PolicyTypeDefinition, StorageOperationStatus> addPolicyType(PolicyTypeDefinition policyTypeDef) {
71 Either<PolicyTypeDefinition, StorageOperationStatus> result;
72 Either<PolicyTypeData, StorageOperationStatus> eitherStatus = addPolicyTypeToGraph(policyTypeDef);
73 if (eitherStatus.isRight()) {
74 BeEcompErrorManager.getInstance().logBeFailedCreateNodeError(CREATE_FLOW_CONTEXT, policyTypeDef.getType(), eitherStatus.right().value().name());
75 result = Either.right(eitherStatus.right().value());
77 PolicyTypeData policyTypeData = eitherStatus.left().value();
78 String uniqueId = policyTypeData.getUniqueId();
79 Either<PolicyTypeDefinition, StorageOperationStatus> policyTypeRes = this.getPolicyTypeByUid(uniqueId);
81 if (policyTypeRes.isRight()) {
82 BeEcompErrorManager.getInstance().logBeFailedRetrieveNodeError(GET_FLOW_CONTEXT, policyTypeDef.getType(), eitherStatus.right().value().name());
84 result = policyTypeRes;
90 public Either<PolicyTypeDefinition, StorageOperationStatus> updatePolicyType(PolicyTypeDefinition updatedPolicyType, PolicyTypeDefinition currPolicyType) {
91 log.debug("updating policy type {}", updatedPolicyType.getType());
92 return updatePolicyTypeOnGraph(updatedPolicyType, currPolicyType);
96 public List<PolicyTypeDefinition> getAllPolicyTypes(Set<String> excludedPolicyTypes) {
97 Map<String, Map.Entry<TitanPredicate, Object>> predicateCriteria = buildNotInPredicate(GraphPropertiesDictionary.TYPE.getProperty(), excludedPolicyTypes);
98 return titanGenericDao.getByCriteriaWithPredicate(NodeTypeEnum.PolicyType, predicateCriteria, PolicyTypeData.class)
100 .map(this::convertPolicyTypesToDefinition)
102 .on(operationUtils::onTitanOperationFailure);
105 private List<PolicyTypeDefinition> convertPolicyTypesToDefinition(List<PolicyTypeData> policiesTypes) {
106 return policiesTypes.stream().map(type -> new PolicyTypeDefinition(type.getPolicyTypeDataDefinition())).collect(Collectors.toList());
110 private Either<PolicyTypeData, StorageOperationStatus> addPolicyTypeToGraph(PolicyTypeDefinition policyTypeDef) {
111 log.debug("Got policy type {}", policyTypeDef);
113 String ptUniqueId = UniqueIdBuilder.buildPolicyTypeUid(policyTypeDef.getType(), policyTypeDef.getVersion(), "policytype");
114 PolicyTypeData policyTypeData = buildPolicyTypeData(policyTypeDef, ptUniqueId);
115 log.debug("Before adding policy type to graph. policyTypeData = {}", policyTypeData);
116 Either<PolicyTypeData, TitanOperationStatus> eitherPolicyTypeData = titanGenericDao.createNode(policyTypeData, PolicyTypeData.class);
117 log.debug("After adding policy type to graph. status is = {}", eitherPolicyTypeData);
118 if (eitherPolicyTypeData.isRight()) {
119 TitanOperationStatus operationStatus = eitherPolicyTypeData.right().value();
120 log.error("Failed to add policy type {} to graph. status is {}", policyTypeDef.getType(), operationStatus);
121 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationStatus));
123 List<PropertyDefinition> properties = policyTypeDef.getProperties();
124 Either<Map<String, PropertyData>, TitanOperationStatus> addPropertiesToPolicyType = propertyOperation.addPropertiesToElementType(ptUniqueId, NodeTypeEnum.PolicyType, properties);
125 if (addPropertiesToPolicyType.isRight()) {
126 log.error("Failed add properties {} to policy {}", properties, policyTypeDef.getType());
127 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(addPropertiesToPolicyType.right().value()));
129 return addDerivedFromRelation(policyTypeDef, ptUniqueId)
131 .map(updatedDerivedFrom -> eitherPolicyTypeData.left().value());
134 private Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeByCriteria(String type, Map<String, Object> properties) {
135 Either<PolicyTypeDefinition, StorageOperationStatus> result;
136 if (type == null || type.isEmpty()) {
137 log.error("type is empty");
138 result = Either.right(StorageOperationStatus.INVALID_ID);
142 Either<List<PolicyTypeData>, TitanOperationStatus> eitherPolicyData = titanGenericDao.getByCriteria(NodeTypeEnum.PolicyType, properties, PolicyTypeData.class);
143 if (eitherPolicyData.isRight()) {
144 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(eitherPolicyData.right().value()));
146 PolicyTypeDataDefinition dataDefinition = eitherPolicyData.left().value().stream().map(PolicyTypeData::getPolicyTypeDataDefinition).findFirst().get();
147 result = getPolicyTypeByUid(dataDefinition.getUniqueId());
153 private Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeByUid(String uniqueId) {
154 log.debug("#getPolicyTypeByUid - fetching policy type with id {}", uniqueId);
155 return titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.PolicyType), uniqueId, PolicyTypeData.class)
157 .map(DaoStatusConverter::convertTitanStatusToStorageStatus)
159 .bind(policyType -> createPolicyTypeDefinition(uniqueId, policyType));
162 private Either<PolicyTypeDefinition, StorageOperationStatus> createPolicyTypeDefinition(String uniqueId, PolicyTypeData policyTypeNode) {
163 PolicyTypeDefinition policyType = new PolicyTypeDefinition(policyTypeNode.getPolicyTypeDataDefinition());
164 return fillDerivedFrom(uniqueId, policyType)
166 .map(derivedFrom -> fillProperties(uniqueId, policyType, derivedFrom))
168 .map(props -> policyType);
171 private Either<List<PropertyDefinition>, StorageOperationStatus> fillProperties(String uniqueId, PolicyTypeDefinition policyType, PolicyTypeData derivedFromNode) {
172 log.debug("#fillProperties - fetching all properties for policy type {}", policyType.getType());
173 return propertyOperation.findPropertiesOfNode(NodeTypeEnum.PolicyType, uniqueId)
175 .bind(this::handlePolicyTypeHasNoProperties)
177 .bind(propsMap -> fillDerivedFromProperties(policyType, derivedFromNode, new ArrayList<>(propsMap.values())));
180 private Either<List<PropertyDefinition>, StorageOperationStatus> fillDerivedFromProperties(PolicyTypeDefinition policyType, PolicyTypeData derivedFromNode, List<PropertyDefinition> policyTypeDirectProperties) {
181 if (derivedFromNode == null) {
182 policyType.setProperties(policyTypeDirectProperties);
183 return Either.left(policyTypeDirectProperties);
185 log.debug("#fillDerivedFromProperties - fetching all properties of derived from chain for policy type {}", policyType.getType());
186 return propertyOperation.getAllPropertiesRec(derivedFromNode.getUniqueId(), NodeTypeEnum.PolicyType, PolicyTypeData.class)
188 .map(derivedFromProps -> {policyTypeDirectProperties.addAll(derivedFromProps); return policyTypeDirectProperties;})
190 .map(allProps -> {policyType.setProperties(allProps);return allProps;});
193 private Either<PolicyTypeData, StorageOperationStatus> fillDerivedFrom(String uniqueId, PolicyTypeDefinition policyType) {
194 log.debug("#fillDerivedFrom - fetching policy type {} derived node", policyType.getType());
195 return derivedFromOperation.getDerivedFromChild(uniqueId, NodeTypeEnum.PolicyType, PolicyTypeData.class)
197 .bind(this::handleDerivedFromNotExist)
199 .map(derivedFrom -> setDerivedFrom(policyType, derivedFrom));
203 private Either<PolicyTypeData, StorageOperationStatus> handleDerivedFromNotExist(StorageOperationStatus err) {
204 if (err == StorageOperationStatus.NOT_FOUND) {
205 return Either.left(null);
207 return Either.right(err);
210 Either<Map<String, PropertyDefinition>, StorageOperationStatus> handlePolicyTypeHasNoProperties(TitanOperationStatus err) {
211 if (err == TitanOperationStatus.NOT_FOUND) {
212 return Either.left(new HashMap<>());
214 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(err));
217 private PolicyTypeData setDerivedFrom(PolicyTypeDefinition policyTypeDefinition, PolicyTypeData derivedFrom) {
218 if (derivedFrom != null) {
219 policyTypeDefinition.setDerivedFrom(derivedFrom.getPolicyTypeDataDefinition().getType());
224 private PolicyTypeData buildPolicyTypeData(PolicyTypeDefinition policyTypeDefinition, String ptUniqueId) {
226 PolicyTypeData policyTypeData = new PolicyTypeData(policyTypeDefinition);
228 policyTypeData.getPolicyTypeDataDefinition().setUniqueId(ptUniqueId);
229 Long creationDate = policyTypeData.getPolicyTypeDataDefinition().getCreationTime();
230 if (creationDate == null) {
231 creationDate = System.currentTimeMillis();
234 policyTypeData.getPolicyTypeDataDefinition().setCreationTime(creationDate);
235 policyTypeData.getPolicyTypeDataDefinition().setModificationTime(creationDate);
236 return policyTypeData;
239 private Either<PolicyTypeDefinition, StorageOperationStatus> updatePolicyTypeOnGraph(PolicyTypeDefinition updatedPolicyType, PolicyTypeDefinition currPolicyType) {
240 updatePolicyTypeData(updatedPolicyType, currPolicyType);
241 return titanGenericDao.updateNode(new PolicyTypeData(updatedPolicyType), PolicyTypeData.class)
243 .map(DaoStatusConverter::convertTitanStatusToStorageStatus)
245 .bind(updatedNode -> updatePolicyProperties(updatedPolicyType.getUniqueId(), updatedPolicyType.getProperties()))
247 .bind(updatedProperties -> updatePolicyDerivedFrom(updatedPolicyType, currPolicyType.getDerivedFrom()))
249 .map(updatedDerivedFrom -> updatedPolicyType);
252 private Either<Map<String, PropertyData>, StorageOperationStatus> updatePolicyProperties(String policyId, List<PropertyDefinition> properties) {
253 log.debug("#updatePolicyProperties - updating policy type properties for policy type with id {}", policyId);
254 return propertyOperation.deletePropertiesAssociatedToNode(NodeTypeEnum.PolicyType, policyId)
256 .bind(deleteProps -> addPropertiesToPolicy(policyId, properties));
259 private Either<GraphRelation, StorageOperationStatus> updatePolicyDerivedFrom(PolicyTypeDefinition updatedPolicyType, String currDerivedFromPolicyType) {
260 String policyTypeId = updatedPolicyType.getUniqueId();
261 log.debug("#updatePolicyDerivedFrom - updating policy derived from relation for policy type with id {}. old derived type {}. new derived type {}", policyTypeId, currDerivedFromPolicyType, updatedPolicyType.getDerivedFrom());
262 StorageOperationStatus deleteDerivedRelationStatus = deleteDerivedFromPolicyType(policyTypeId, currDerivedFromPolicyType);
263 if (deleteDerivedRelationStatus != StorageOperationStatus.OK) {
264 return Either.right(deleteDerivedRelationStatus);
266 return addDerivedFromRelation(updatedPolicyType, policyTypeId);
269 private Either<GraphRelation, StorageOperationStatus> addDerivedFromRelation(PolicyTypeDataDefinition policyTypeDef, String ptUniqueId) {
270 String derivedFrom = policyTypeDef.getDerivedFrom();
271 if (derivedFrom == null) {
272 return Either.left(null);
274 log.debug("#addDerivedFromRelationBefore - adding derived from relation between policy type {} to its parent {}", policyTypeDef.getType(), derivedFrom);
275 return this.getLatestPolicyTypeByType(derivedFrom)
277 .bind(derivedFromPolicy -> derivedFromOperation.addDerivedFromRelation(ptUniqueId, derivedFromPolicy.getUniqueId(), NodeTypeEnum.PolicyType));
280 private StorageOperationStatus deleteDerivedFromPolicyType(String policyTypeId, String derivedFromType) {
281 if (derivedFromType == null) {
282 return StorageOperationStatus.OK;
284 log.debug("#deleteDerivedFromPolicyType - deleting derivedFrom relation for policy type with id {} and its derived type {}", policyTypeId, derivedFromType);
285 return getLatestPolicyTypeByType(derivedFromType)
286 .either(derivedFromNode -> derivedFromOperation.removeDerivedFromRelation(policyTypeId, derivedFromNode.getUniqueId(), NodeTypeEnum.PolicyType),
290 private Either<Map<String, PropertyData>, StorageOperationStatus> addPropertiesToPolicy(String policyTypeId, List<PropertyDefinition> properties) {
291 log.debug("#addPropertiesToPolicy - adding policy type properties for policy type with id {}", policyTypeId);
292 return propertyOperation.addPropertiesToElementType(policyTypeId, NodeTypeEnum.PolicyType, properties)
294 .map(DaoStatusConverter::convertTitanStatusToStorageStatus);
297 private void updatePolicyTypeData(PolicyTypeDefinition updatedTypeDefinition, PolicyTypeDefinition currTypeDefinition) {
298 updatedTypeDefinition.setUniqueId(currTypeDefinition.getUniqueId());
299 updatedTypeDefinition.setCreationTime(currTypeDefinition.getCreationTime());
300 updatedTypeDefinition.setModificationTime(System.currentTimeMillis());