Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / CapabilitiesOperation.java
1 /*
2  * Copyright © 2016-2018 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 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
17
18 import fj.data.Either;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import org.apache.commons.collections.MapUtils;
24 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
25 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
26 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
28 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
34 import org.openecomp.sdc.be.model.CapabilityDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentParametersView;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
39 import org.openecomp.sdc.be.model.operations.StorageException;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @org.springframework.stereotype.Component("capabilities-operation")
45 public class CapabilitiesOperation extends BaseOperation {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(CapabilitiesOperation.class);
48
49     private static ListCapabilityDataDefinition convertToListCapabilityDataDefinition(List<CapabilityDefinition> capabilities) {
50         List<CapabilityDataDefinition> capabilityDefinitions = new ArrayList<>(capabilities);
51         return new ListCapabilityDataDefinition(capabilityDefinitions);
52     }
53
54     public Either<List<CapabilityDefinition>, StorageOperationStatus> addCapabilities(String componentId,
55                                                                                       List<CapabilityDefinition> capabilityDefinitions) {
56         return addOrUpdateCapabilities(componentId, capabilityDefinitions, false);
57     }
58
59     public Either<List<CapabilityDefinition>, StorageOperationStatus> updateCapabilities(String componentId,
60                                                                                          List<CapabilityDefinition> capabilityDefinitions) {
61         return addOrUpdateCapabilities(componentId, capabilityDefinitions, true);
62     }
63
64     private Either<List<CapabilityDefinition>, StorageOperationStatus> addOrUpdateCapabilities(String componentId,
65                                                                                                List<CapabilityDefinition> capabilityDefinitions,
66                                                                                                boolean isUpdateAction) {
67         StorageOperationStatus statusRes = performUpdateToscaAction(isUpdateAction, componentId,
68             Collections.singletonList(convertToListCapabilityDataDefinition(capabilityDefinitions)));
69         if (!statusRes.equals(StorageOperationStatus.OK)) {
70             LOGGER.error("Failed to find the parent capability of capability type {}." + " status is {}", componentId, statusRes);
71             return Either.right(statusRes);
72         }
73         return Either.left(capabilityDefinitions);
74     }
75
76     public StorageOperationStatus deleteCapabilities(Component component, String capabilityIdToDelete) {
77         return deleteToscaDataElements(component.getUniqueId(), EdgeLabelEnum.CAPABILITIES, Collections.singletonList(capabilityIdToDelete));
78     }
79
80     public StorageOperationStatus deleteCapabilityProperties(Component component, String capabilityPropIdToDelete) {
81         return deleteToscaDataElements(component.getUniqueId(), EdgeLabelEnum.CAPABILITIES_PROPERTIES,
82             Collections.singletonList(capabilityPropIdToDelete));
83     }
84
85     private StorageOperationStatus performUpdateToscaAction(boolean isUpdate, String componentId, List<ListCapabilityDataDefinition> toscaDataList) {
86         if (isUpdate) {
87             return updateToscaDataOfToscaElement(componentId, EdgeLabelEnum.CAPABILITIES, VertexTypeEnum.CAPABILITIES, toscaDataList,
88                 JsonPresentationFields.TYPE);
89         } else {
90             return addToscaDataToToscaElement(componentId, EdgeLabelEnum.CAPABILITIES, VertexTypeEnum.CAPABILITIES, toscaDataList,
91                 JsonPresentationFields.TYPE);
92         }
93     }
94
95     private StorageOperationStatus createOrUpdateCapabilityProperties(String componentId, TopologyTemplate toscaElement,
96                                                                       Map<String, MapPropertiesDataDefinition> propertiesMap) {
97         GraphVertex toscaElementV = janusGraphDao.getVertexById(componentId, JsonParseFlagEnum.NoParse).left().on(this::throwStorageException);
98         Map<String, MapPropertiesDataDefinition> capabilitiesProperties = toscaElement.getCapabilitiesProperties();
99         if (MapUtils.isNotEmpty(capabilitiesProperties)) {
100             capabilitiesProperties.forEach((key, val) -> {
101                 Map<String, PropertyDataDefinition> mapToscaDataDefinition = val.getMapToscaDataDefinition();
102                 mapToscaDataDefinition.forEach((key1, val1) -> {
103                     propertiesMap.forEach((propKey, propVal) -> {
104                         Map<String, PropertyDataDefinition> propValMapToscaDataDefinition = propVal.getMapToscaDataDefinition();
105                         propValMapToscaDataDefinition.forEach((propKey1, propVal1) -> {
106                             if (propKey1.equals(key1) && val1.getUniqueId().equals(propVal1.getUniqueId())) {
107                                 ToscaDataDefinition.mergeDataMaps(mapToscaDataDefinition, propValMapToscaDataDefinition);
108                             }
109                         });
110                     });
111                 });
112             });
113             ToscaDataDefinition.mergeDataMaps(propertiesMap, capabilitiesProperties);
114         }
115         return topologyTemplateOperation
116             .updateFullToscaData(toscaElementV, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, propertiesMap);
117     }
118
119     public StorageOperationStatus createOrUpdateCapabilityProperties(String componentId, Map<String, MapPropertiesDataDefinition> propertiesMap) {
120         StorageOperationStatus propertiesStatusRes = null;
121         if (MapUtils.isNotEmpty(propertiesMap)) {
122             propertiesStatusRes = createOrUpdateCapabilityProperties(componentId, getTopologyTemplate(componentId), propertiesMap);
123         }
124         return propertiesStatusRes;
125     }
126
127     private TopologyTemplate getTopologyTemplate(String componentId) {
128         return (TopologyTemplate) topologyTemplateOperation.getToscaElement(componentId, getFilterComponentWithCapProperties()).left()
129             .on(this::throwStorageException);
130     }
131
132     private ComponentParametersView getFilterComponentWithCapProperties() {
133         ComponentParametersView filter = new ComponentParametersView();
134         filter.setIgnoreCapabiltyProperties(false);
135         return filter;
136     }
137
138     private ToscaElement throwStorageException(StorageOperationStatus status) {
139         throw new StorageException(status);
140     }
141 }