53a7f3900146fa0c8555bce92e91eb357518167a
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
32 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
33 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
34 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
35 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
36 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
37 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
40 import org.openecomp.sdc.be.model.ArtifactDefinition;
41 import org.openecomp.sdc.be.model.InterfaceDefinition;
42 import org.openecomp.sdc.be.model.Operation;
43 import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.resources.data.ArtifactData;
46 import org.openecomp.sdc.be.resources.data.InterfaceData;
47 import org.openecomp.sdc.be.resources.data.OperationData;
48 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.stereotype.Component;
52
53 import fj.data.Either;
54
55 @Component("interface-operation")
56 public class InterfaceLifecycleOperation implements IInterfaceLifecycleOperation {
57
58         private static Logger log = LoggerFactory.getLogger(InterfaceLifecycleOperation.class.getName());
59
60         public InterfaceLifecycleOperation() {
61                 super();
62         }
63
64         @javax.annotation.Resource
65         private ArtifactOperation artifactOperation;
66
67         @javax.annotation.Resource
68         private TitanGenericDao titanGenericDao;
69
70         @Override
71         public Either<InterfaceDefinition, StorageOperationStatus> addInterfaceToResource(InterfaceDefinition interf, String resourceId, String interfaceName, boolean inTransaction) {
72
73                 return createInterfaceOnResource(interf, resourceId, interfaceName, true, inTransaction);
74
75         }
76
77         private Either<OperationData, TitanOperationStatus> addOperationToGraph(InterfaceDefinition interf, String opName, Operation op, InterfaceData interfaceData) {
78
79                 op.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId((String) interfaceData.getUniqueId(), opName));
80                 OperationData operationData = new OperationData(op);
81
82                 log.debug("Before adding operation to graph {}", operationData);
83                 Either<OperationData, TitanOperationStatus> createOpNodeResult = titanGenericDao.createNode(operationData, OperationData.class);
84                 log.debug("After adding operation to graph {}", operationData);
85
86                 if (createOpNodeResult.isRight()) {
87                         TitanOperationStatus opStatus = createOpNodeResult.right().value();
88                         log.error("Failed to add operation {} to graph. status is {}", opName, opStatus);
89                         return Either.right(opStatus);
90                 }
91
92                 Map<String, Object> props = new HashMap<String, Object>();
93                 props.put(GraphPropertiesDictionary.NAME.getProperty(), opName);
94                 Either<GraphRelation, TitanOperationStatus> createRelResult = titanGenericDao.createRelation(interfaceData, operationData, GraphEdgeLabels.INTERFACE_OPERATION, props);
95
96                 if (createRelResult.isRight()) {
97                         TitanOperationStatus operationStatus = createOpNodeResult.right().value();
98                         log.error("Failed to associate operation {} to property {} in graph. status is {}", interfaceData.getUniqueId(), opName, operationStatus);
99
100                         return Either.right(operationStatus);
101                 }
102
103                 return Either.left(createOpNodeResult.left().value());
104
105         }
106
107         private InterfaceDefinition convertInterfaceDataToInterfaceDefinition(InterfaceData interfaceData) {
108
109                 log.debug("The object returned after create interface is {}", interfaceData);
110
111                 InterfaceDefinition interfaceDefResult = new InterfaceDefinition(interfaceData.getInterfaceDataDefinition());
112
113                 return interfaceDefResult;
114
115         }
116
117         private Operation convertOperationDataToOperation(OperationData operationData) {
118
119                 log.debug("The object returned after create operation is {}", operationData);
120
121                 Operation operationDefResult = new Operation(operationData.getOperationDataDefinition());
122
123                 return operationDefResult;
124
125         }
126
127         private Either<InterfaceData, TitanOperationStatus> addInterfaceToGraph(InterfaceDefinition interfaceInfo, String interfaceName, String resourceId) {
128
129                 InterfaceData interfaceData = new InterfaceData(interfaceInfo);
130
131                 ResourceMetadataData resourceData = new ResourceMetadataData();
132                 resourceData.getMetadataDataDefinition().setUniqueId(resourceId);
133
134                 String interfaceNameSplitted = getShortInterfaceName(interfaceInfo);
135
136                 interfaceInfo.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, interfaceNameSplitted));
137
138                 Either<InterfaceData, TitanOperationStatus> existInterface = titanGenericDao.getNode(interfaceData.getUniqueIdKey(), interfaceData.getUniqueId(), InterfaceData.class);
139
140                 if (existInterface.isRight()) {
141
142                         return createInterfaceNodeAndRelation(interfaceNameSplitted, resourceId, interfaceData, resourceData);
143                 } else {
144                         log.debug("Interface {} already exist", interfaceData.getUniqueId());
145                         return Either.right(TitanOperationStatus.ALREADY_EXIST);
146                 }
147         }
148
149         private Either<InterfaceData, TitanOperationStatus> createInterfaceNodeAndRelation(String interfaceName, String resourceId, InterfaceData interfaceData, ResourceMetadataData resourceData) {
150                 log.debug("Before adding interface to graph {}", interfaceData);
151                 Either<InterfaceData, TitanOperationStatus> createNodeResult = titanGenericDao.createNode(interfaceData, InterfaceData.class);
152                 log.debug("After adding property to graph {}", interfaceData);
153
154                 if (createNodeResult.isRight()) {
155                         TitanOperationStatus operationStatus = createNodeResult.right().value();
156                         log.error("Failed to add interface {} to graph. status is {}", interfaceName, operationStatus);
157                         return Either.right(operationStatus);
158                 }
159
160                 Map<String, Object> props = new HashMap<String, Object>();
161                 props.put(GraphPropertiesDictionary.NAME.getProperty(), interfaceName);
162                 Either<GraphRelation, TitanOperationStatus> createRelResult = titanGenericDao.createRelation(resourceData, interfaceData, GraphEdgeLabels.INTERFACE, props);
163                 if (createRelResult.isRight()) {
164                         TitanOperationStatus operationStatus = createNodeResult.right().value();
165                         log.error("Failed to associate resource {} to property {} in graph. status is {}", resourceId, interfaceName, operationStatus);
166
167                         return Either.right(operationStatus);
168                 }
169
170                 return Either.left(createNodeResult.left().value());
171         }
172
173         private Either<OperationData, TitanOperationStatus> createOperationNodeAndRelation(String operationName, OperationData operationData, InterfaceData interfaceData) {
174                 log.debug("Before adding operation to graph {}", operationData);
175                 Either<OperationData, TitanOperationStatus> createNodeResult = titanGenericDao.createNode(operationData, OperationData.class);
176                 log.debug("After adding operation to graph {}", interfaceData);
177
178                 if (createNodeResult.isRight()) {
179                         TitanOperationStatus operationStatus = createNodeResult.right().value();
180                         log.error("Failed to add interfoperationce {} to graph. status is {}", operationName, operationStatus);
181                         return Either.right(operationStatus);
182                 }
183
184                 Map<String, Object> props = new HashMap<String, Object>();
185                 props.put(GraphPropertiesDictionary.NAME.getProperty(), operationName);
186                 Either<GraphRelation, TitanOperationStatus> createRelResult = titanGenericDao.createRelation(interfaceData, operationData, GraphEdgeLabels.INTERFACE_OPERATION, props);
187                 if (createRelResult.isRight()) {
188                         TitanOperationStatus operationStatus = createNodeResult.right().value();
189                         log.error("Failed to associate operation {} to interface {} in graph. status is {}", operationName, interfaceData.getUniqueId(), operationStatus);
190
191                         return Either.right(operationStatus);
192                 }
193
194                 return Either.left(createNodeResult.left().value());
195         }
196
197         // @Override
198         // public Either<InterfaceDefinition, StorageOperationStatus> getInterface(
199         // String interfaceId) {
200         //
201         // /*
202         // * Either<InterfaceData, TitanOperationStatus> getResult =
203         // * this.titanGenericDao
204         // * .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Interface),
205         // * interfaceId, InterfaceData.class); if (getResult.isLeft()) {
206         // * InterfaceData propertyData = getResult.left().value(); return
207         // * Either.left(convertPropertyDataToPropertyDefinition(propertyData)); }
208         // * else { TitanOperationStatus titanStatus = getResult.right().value();
209         // * log.debug("Node with id " + propertyId +
210         // * " was not found in the graph. status: " + titanStatus);
211         // * StorageOperationStatus storageOperationStatus =
212         // * DaoStatusConverter.convertTitanStatusToStorageStatus(titanStatus);
213         // * return Either.right(storageOperationStatus); }
214         // */
215         // return null;
216         // }
217
218         // @Override
219         // public Either<InterfaceDefinition, StorageOperationStatus> getInterface(
220         // String interfaceId, boolean inTransaction) {
221         // // TODO Auto-generated method stub
222         // return null;
223         // }
224
225         @Override
226         public Either<Map<String, InterfaceDefinition>, StorageOperationStatus> getAllInterfacesOfResource(String resourceIdn, boolean recursively) {
227                 return getAllInterfacesOfResource(resourceIdn, recursively, false);
228         }
229
230         @Override
231         public Either<Map<String, InterfaceDefinition>, StorageOperationStatus> getAllInterfacesOfResource(String resourceId, boolean recursively, boolean inTransaction) {
232
233                 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> result = null;
234                 Map<String, InterfaceDefinition> interfaces = new HashMap<String, InterfaceDefinition>();
235                 try {
236                         if ((resourceId == null) || resourceId.isEmpty()) {
237                                 log.error("resourceId is empty");
238                                 result = Either.right(StorageOperationStatus.INVALID_ID);
239                                 return result;
240                         }
241
242                         TitanOperationStatus findInterfacesRes = TitanOperationStatus.GENERAL_ERROR;
243                         if (recursively) {
244                                 findInterfacesRes = findAllInterfacesRecursively(resourceId, interfaces);
245                         } else {
246                                 findInterfacesRes = findAllInterfacesNotRecursively(resourceId, interfaces);
247                         }
248                         if (!findInterfacesRes.equals(TitanOperationStatus.OK)) {
249                                 log.error("Failed to get all interfaces of resource {}. status is {}", resourceId, findInterfacesRes);
250                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(findInterfacesRes));
251                                 return result;
252                         }
253                         result = Either.left(interfaces);
254                         return result;
255                 } finally {
256                         if (false == inTransaction) {
257                                 if (result == null || result.isRight()) {
258                                         log.error("Going to execute rollback on graph.");
259                                         titanGenericDao.rollback();
260                                 } else {
261                                         log.debug("Going to execute commit on graph.");
262                                         titanGenericDao.commit();
263                                 }
264                         }
265                 }
266         }
267
268         private TitanOperationStatus findAllInterfacesNotRecursively(String resourceId, Map<String, InterfaceDefinition> interfaces) {
269
270                 Either<List<ImmutablePair<InterfaceData, GraphEdge>>, TitanOperationStatus> interfaceNodes = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.INTERFACE,
271                                 NodeTypeEnum.Interface, InterfaceData.class);
272
273                 if (interfaceNodes.isRight()) {
274                         TitanOperationStatus status = interfaceNodes.right().value();
275                         if (status != TitanOperationStatus.NOT_FOUND) {
276                                 return status;
277                         }
278                 } else {
279                         List<ImmutablePair<InterfaceData, GraphEdge>> interfaceList = interfaceNodes.left().value();
280                         if (interfaceList != null) {
281                                 for (ImmutablePair<InterfaceData, GraphEdge> interfacePair : interfaceList) {
282                                         String interfaceUniqueId = (String) interfacePair.getKey().getUniqueId();
283                                         Either<String, TitanOperationStatus> interfaceNameRes = getPropertyValueFromEdge(interfacePair.getValue(), GraphPropertiesDictionary.NAME);
284                                         if (interfaceNameRes.isRight()) {
285                                                 log.error("The requirement name is missing on the edge of requirement {}", interfaceUniqueId);
286                                                 return interfaceNameRes.right().value();
287                                         }
288                                         String interfaceName = interfaceNameRes.left().value();
289                                         Either<InterfaceDefinition, TitanOperationStatus> interfaceDefRes = getNonRecursiveInterface(interfacePair.getKey());
290                                         if (interfaceDefRes.isRight()) {
291                                                 TitanOperationStatus status = interfaceDefRes.right().value();
292                                                 log.error("Failed to get interface actions of interface {}", interfaceUniqueId);
293                                                 return status;
294                                         }
295
296                                         InterfaceDefinition interfaceDefinition = interfaceDefRes.left().value();
297                                         if (true == interfaces.containsKey(interfaceName)) {
298                                                 log.debug("The interface {} was already defined in dervied resource. add not overriden operations", interfaceName);
299                                                 InterfaceDefinition existInterface = interfaces.get(interfaceName);
300                                                 addMissingOperationsToInterface(interfaceDefinition, existInterface);
301                                         } else {
302                                                 interfaces.put(interfaceName, interfaceDefinition);
303                                         }
304
305                                 }
306                         }
307                 }
308                 return TitanOperationStatus.OK;
309         }
310
311         public TitanOperationStatus findAllInterfacesRecursively(String resourceId, Map<String, InterfaceDefinition> interfaces) {
312
313                 TitanOperationStatus findAllInterfacesNotRecursively = findAllInterfacesNotRecursively(resourceId, interfaces);
314                 if (!findAllInterfacesNotRecursively.equals(TitanOperationStatus.OK)) {
315                         log.error("failed to get interfaces for resource {}. status is {}", resourceId, findAllInterfacesNotRecursively);
316                 }
317
318                 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, TitanOperationStatus> parentNodes = titanGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
319                                 ResourceMetadataData.class);
320
321                 if (parentNodes.isRight()) {
322                         TitanOperationStatus parentNodesStatus = parentNodes.right().value();
323                         if (parentNodesStatus == TitanOperationStatus.NOT_FOUND) {
324                                 log.debug("Finish to lookup for parnet interfaces");
325                                 return TitanOperationStatus.OK;
326                         } else {
327                                 log.error("Failed to find parent interfaces of resource {}. status is {}", resourceId, parentNodesStatus);
328                                 return parentNodesStatus;
329                         }
330                 }
331                 ImmutablePair<ResourceMetadataData, GraphEdge> parnetNodePair = parentNodes.left().value();
332                 String parentUniqueId = parnetNodePair.getKey().getMetadataDataDefinition().getUniqueId();
333                 TitanOperationStatus addParentIntStatus = findAllInterfacesRecursively(parentUniqueId, interfaces);
334
335                 if (addParentIntStatus != TitanOperationStatus.OK) {
336                         log.error("Failed to fetch all interfaces of resource {}", parentUniqueId);
337                         return addParentIntStatus;
338                 }
339
340                 return TitanOperationStatus.OK;
341         }
342
343         private Either<String, TitanOperationStatus> getPropertyValueFromEdge(GraphEdge edge, GraphPropertiesDictionary property) {
344                 Map<String, Object> edgeProps = edge.getProperties();
345                 String interfaceName = null;
346                 if (edgeProps != null) {
347                         interfaceName = (String) edgeProps.get(property.getProperty());
348                         if (interfaceName == null) {
349                                 return Either.right(TitanOperationStatus.INVALID_ELEMENT);
350                         }
351                 } else {
352                         return Either.right(TitanOperationStatus.INVALID_ELEMENT);
353                 }
354                 return Either.left(interfaceName);
355         }
356
357         private Either<InterfaceDefinition, TitanOperationStatus> getNonRecursiveInterface(InterfaceData interfaceData) {
358
359                 log.debug("Going to fetch the operations associate to interface {}", interfaceData.getUniqueId());
360                 InterfaceDefinition interfaceDefinition = new InterfaceDefinition(interfaceData.getInterfaceDataDefinition());
361
362                 String interfaceId = interfaceData.getUniqueId();
363                 Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> operationsRes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), interfaceId, GraphEdgeLabels.INTERFACE_OPERATION,
364                                 NodeTypeEnum.InterfaceOperation, OperationData.class);
365
366                 if (operationsRes.isRight()) {
367                         TitanOperationStatus status = operationsRes.right().value();
368                         if (status != TitanOperationStatus.NOT_FOUND) {
369                                 return Either.right(status);
370                         } else {
371                                 return Either.left(interfaceDefinition);
372                         }
373                 }
374
375                 List<ImmutablePair<OperationData, GraphEdge>> operationList = operationsRes.left().value();
376                 if (operationList != null && !operationList.isEmpty()) {
377                         for (ImmutablePair<OperationData, GraphEdge> operationPair : operationList) {
378                                 Operation operation = new Operation(operationPair.getKey().getOperationDataDefinition());
379                                 Either<String, TitanOperationStatus> operationNameRes = getPropertyValueFromEdge(operationPair.getValue(), GraphPropertiesDictionary.NAME);
380                                 if (operationNameRes.isRight()) {
381                                         log.error("The operation name is missing on the edge of operation {}", operationPair.getKey().getUniqueId());
382                                         return Either.right(operationNameRes.right().value());
383                                 }
384                                 String operationName = operationNameRes.left().value();
385                                 findOperationImplementation(operation);
386                                 interfaceDefinition.getOperations().put(operationName, operation);
387                         }
388                 }
389
390                 return Either.left(interfaceDefinition);
391         }
392
393         private StorageOperationStatus findOperationImplementation(Operation operation) {
394
395                 String operationId = operation.getUniqueId();
396                 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> artifactsRes = artifactOperation.getArtifacts(operationId, NodeTypeEnum.InterfaceOperation, true);
397                 if (artifactsRes.isRight() || artifactsRes.left().value() == null) {
398                         log.error("failed to get artifact from graph for operation id {}. status is {}", operationId, artifactsRes.right().value());
399                         return artifactsRes.right().value();
400                 } else {
401                         Map<String, ArtifactDefinition> artifacts = artifactsRes.left().value();
402                         Iterator<String> iter = artifacts.keySet().iterator();
403
404                         if (iter.hasNext()) {
405                                 operation.setImplementation(artifacts.get(iter.next()));
406                         }
407                 }
408                 return StorageOperationStatus.OK;
409         }
410
411         private StorageOperationStatus addMissingOperationsToInterface(InterfaceDefinition interfaceDefinition, InterfaceDefinition existInterface) {
412                 Map<String, Operation> existOperations = existInterface.getOperationsMap();
413                 Map<String, Operation> operations = interfaceDefinition.getOperationsMap();
414                 if (operations != null && !operations.isEmpty()) {
415                         Set<Entry<String, Operation>> operationsSet = operations.entrySet();
416                         for (Entry<String, Operation> operation : operationsSet) {
417                                 if (!existOperations.containsKey(operation.getKey())) {
418                                         existOperations.put(operation.getKey(), operation.getValue());
419                                 }
420                         }
421                 }
422                 return StorageOperationStatus.OK;
423         }
424
425         @Override
426         public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String resourceId, String interfaceName, String operationName, Operation interf) {
427
428                 return updateInterfaceOperation(resourceId, interfaceName, operationName, interf, false);
429         }
430
431         @Override
432         public Either<Operation, StorageOperationStatus> updateInterfaceOperation(String resourceId, String interfaceName, String operationName, Operation operation, boolean inTransaction) {
433                 Either<Operation, StorageOperationStatus> status = updateOperationOnGraph(operation, resourceId, interfaceName, operationName);
434
435                 /*
436                  * if (status.isRight()) { if (false == inTransaction) { titanGenericDao.rollback(); } 
437                  * log.error("Failed to update operation {} of interfaceName {} of resource {}", operationName, interfaceName, resourceId); 
438                  * return
439                  * Either.right(DaoStatusConverter .convertTitanStatusToStorageStatus(status.right().value())); } else { if (false == inTransaction) { titanGenericDao.commit(); } OperationData operationData = status.left().value();
440                  * 
441                  * Operation operationDefResult = convertOperationDataToOperation(operationData);
442                  * 
443                  * 
444                  * log.debug("The returned OperationDefintion is {}", operationDefResult); return Either.left(operationDefResult); }
445                  */
446                 return status;
447         }
448
449         private Either<Operation, StorageOperationStatus> updateOperationOnGraph(Operation operation, String resourceId, String interfaceName, String operationName) {
450
451                 Either<List<ImmutablePair<InterfaceData, GraphEdge>>, TitanOperationStatus> childrenNodes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), resourceId, GraphEdgeLabels.INTERFACE, NodeTypeEnum.Interface,
452                                 InterfaceData.class);
453
454                 if (childrenNodes.isRight()) {
455                         /*
456                          * InterfaceDefinition intDef = new InterfaceDefinition(); intDef.setType(interfaceName); Map<String, Operation> opMap = new HashMap<String, Operation>(); opMap.put(operationName, operation); intDef.setOperations(opMap);
457                          * Either<InterfaceDefinition, StorageOperationStatus> statusRes = this .createInterfaceOnResource(intDef, resourceId, interfaceName, true); if (statusRes.isRight()) return Either.right(statusRes.right().value()); else {
458                          * InterfaceDefinition newDef = statusRes.left().value(); Operation res = newDef.getOperations().get(operationName); return Either.left(res); }
459                          */
460                         return updateOperationFromParentNode(operation, resourceId, interfaceName, operationName);
461
462                 } else {
463                         return updateExistingOperation(resourceId, operation, interfaceName, operationName, childrenNodes);
464
465                 }
466
467         }
468
469         private Either<Operation, StorageOperationStatus> updateExistingOperation(String resourceId, Operation operation, String interfaceName, String operationName,
470                         Either<List<ImmutablePair<InterfaceData, GraphEdge>>, TitanOperationStatus> childrenNodes) {
471                 Operation newOperation = null;
472                 StorageOperationStatus storageOperationStatus = StorageOperationStatus.GENERAL_ERROR;
473
474                 for (ImmutablePair<InterfaceData, GraphEdge> interfaceDataNode : childrenNodes.left().value()) {
475
476                         GraphEdge interfaceEdge = interfaceDataNode.getRight();
477                         Map<String, Object> interfaceEdgeProp = interfaceEdge.getProperties();
478                         InterfaceData interfaceData = interfaceDataNode.getKey();
479
480                         if (interfaceEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(interfaceName)) {
481                                 Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> operationRes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) interfaceDataNode.getLeft().getUniqueId(),
482                                                 GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class);
483                                 if (operationRes.isRight()) {
484                                         log.error("Failed to find operation  {} on interface {}", operationName, interfaceName);
485                                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationRes.right().value()));
486                                 } else {
487                                         List<ImmutablePair<OperationData, GraphEdge>> operations = operationRes.left().value();
488                                         for (ImmutablePair<OperationData, GraphEdge> operationPairEdge : operations) {
489                                                 GraphEdge opEdge = operationPairEdge.getRight();
490                                                 OperationData opData = operationPairEdge.getLeft();
491                                                 Map<String, Object> opEdgeProp = opEdge.getProperties();
492                                                 if (opEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(operationName)) {
493                                                         ArtifactDefinition artifact = operation.getImplementationArtifact();
494                                                         Either<ImmutablePair<ArtifactData, GraphEdge>, TitanOperationStatus> artifactRes = titanGenericDao.getChild(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) opData.getUniqueId(), GraphEdgeLabels.ARTIFACT_REF,
495                                                                         NodeTypeEnum.ArtifactRef, ArtifactData.class);
496                                                         Either<ArtifactDefinition, StorageOperationStatus> artStatus;
497                                                         if (artifactRes.isRight()) {
498                                                                 artStatus = artifactOperation.addArifactToComponent(artifact, (String) operationPairEdge.getLeft().getUniqueId(), NodeTypeEnum.InterfaceOperation, true, true);
499                                                         } else {
500                                                                 artStatus = artifactOperation.updateArifactOnResource(artifact, (String) operationPairEdge.getLeft().getUniqueId(), (String) artifactRes.left().value().getLeft().getUniqueId(), NodeTypeEnum.InterfaceOperation, true);
501                                                         }
502                                                         if (artStatus.isRight()) {
503                                                                 titanGenericDao.rollback();
504                                                                 log.error("Failed to add artifact {} to interface {}", operationName, interfaceName);
505                                                                 return Either.right(artStatus.right().value());
506                                                         } else {
507                                                                 newOperation = this.convertOperationDataToOperation(opData);
508                                                                 newOperation.setImplementation(artStatus.left().value());
509
510                                                         }
511
512                                                 }
513
514                                         }
515                                         if (newOperation == null) {
516                                                 Either<InterfaceData, TitanOperationStatus> parentInterfaceStatus = findInterfaceOnParentNode(resourceId, interfaceName);
517                                                 if (parentInterfaceStatus.isRight()) {
518                                                         log.debug("Interface {} not exist", interfaceName);
519                                                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentInterfaceStatus.right().value()));
520                                                 }
521
522                                                 InterfaceData parentInterfaceData = parentInterfaceStatus.left().value();
523                                                 Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> opRes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) parentInterfaceData.getUniqueId(),
524                                                                 GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class);
525                                                 if (opRes.isRight()) {
526                                                         log.error("Failed to find operation  {} on interface {}", operationName, interfaceName);
527                                                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationRes.right().value()));
528
529                                                 } else {
530                                                         List<ImmutablePair<OperationData, GraphEdge>> parentOperations = opRes.left().value();
531                                                         for (ImmutablePair<OperationData, GraphEdge> operationPairEdge : parentOperations) {
532                                                                 GraphEdge opEdge = operationPairEdge.getRight();
533                                                                 OperationData opData = operationPairEdge.getLeft();
534                                                                 Map<String, Object> opEdgeProp = opEdge.getProperties();
535                                                                 if (opEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(operationName)) {
536                                                                         return copyAndCreateNewOperation(operation, interfaceName, operationName, null, interfaceData, operationRes, opData);
537                                                                 }
538                                                         }
539                                                 }
540
541                                         }
542
543                                 }
544
545                         } else {
546                                 // not found
547                                 storageOperationStatus = StorageOperationStatus.ARTIFACT_NOT_FOUND;
548                         }
549
550                 }
551                 if (newOperation == null)
552                         return Either.right(storageOperationStatus);
553                 else
554                         return Either.left(newOperation);
555         }
556
557         private Either<Operation, StorageOperationStatus> copyAndCreateNewOperation(Operation operation, String interfaceName, String operationName, Operation newOperation, InterfaceData interfaceData,
558                         Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> operationRes, OperationData opData) {
559                 OperationDataDefinition opDataInfo = opData.getOperationDataDefinition();
560                 OperationDataDefinition newOperationInfo = new OperationDataDefinition(opDataInfo);
561                 newOperationInfo.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(interfaceData.getUniqueId(), operationName.toLowerCase()));
562                 OperationData newopData = new OperationData(newOperationInfo);
563                 Either<OperationData, TitanOperationStatus> operationStatus = createOperationNodeAndRelation(operationName, newopData, interfaceData);
564                 if (operationStatus.isRight()) {
565                         log.error("Failed to create operation  {} on interface {}", operationName, interfaceName);
566                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationRes.right().value()));
567                 }
568                 ArtifactDefinition artifact = operation.getImplementationArtifact();
569                 if (artifact != null) {
570                         Either<ArtifactDefinition, StorageOperationStatus> artStatus = artifactOperation.addArifactToComponent(artifact, (String) operationStatus.left().value().getUniqueId(), NodeTypeEnum.InterfaceOperation, true, true);
571                         if (artStatus.isRight()) {
572                                 titanGenericDao.rollback();
573                                 log.error("Failed to add artifact {} to interface {}", operationName, interfaceName);
574                         } else {
575                                 newOperation = this.convertOperationDataToOperation(opData);
576                                 newOperation.setImplementation(artStatus.left().value());
577
578                         }
579                 }
580                 return Either.left(newOperation);
581         }
582
583         private Either<Operation, StorageOperationStatus> updateOperationFromParentNode(Operation operation, String resourceId, String interfaceName, String operationName) {
584                 // Operation newOperation = null;
585                 ResourceMetadataData resourceData = new ResourceMetadataData();
586                 resourceData.getMetadataDataDefinition().setUniqueId(resourceId);
587                 Either<InterfaceData, TitanOperationStatus> parentInterfaceStatus = findInterfaceOnParentNode(resourceId, interfaceName);
588                 if (parentInterfaceStatus.isRight()) {
589                         log.debug("Interface {} not exist", interfaceName);
590                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(parentInterfaceStatus.right().value()));
591                 }
592
593                 InterfaceData interfaceData = parentInterfaceStatus.left().value();
594                 InterfaceDataDefinition intDataDefinition = interfaceData.getInterfaceDataDefinition();
595                 InterfaceDataDefinition newInterfaceInfo = new InterfaceDataDefinition(intDataDefinition);
596
597                 String interfaceNameSplitted = getShortInterfaceName(intDataDefinition);
598
599                 newInterfaceInfo.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(resourceId, interfaceNameSplitted));
600                 InterfaceData updatedInterfaceData = new InterfaceData(newInterfaceInfo);
601                 Either<InterfaceData, TitanOperationStatus> createStatus = createInterfaceNodeAndRelation(interfaceName, resourceId, updatedInterfaceData, resourceData);
602                 if (createStatus.isRight()) {
603                         log.debug("failed to create interface node  {} on resource  {}", interfaceName,  resourceId);
604                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(createStatus.right().value()));
605                 }
606
607                 InterfaceData newInterfaceNode = createStatus.left().value();
608                 Either<GraphRelation, TitanOperationStatus> createRelResult = titanGenericDao.createRelation(newInterfaceNode, interfaceData, GraphEdgeLabels.DERIVED_FROM, null);
609                 if (createRelResult.isRight()) {
610                         TitanOperationStatus operationStatus = createRelResult.right().value();
611                         log.error("Failed to associate interface {} to interface {} in graph. status is {}", interfaceData.getUniqueId(), newInterfaceNode.getUniqueId(),  operationStatus);
612
613                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationStatus));
614                 }
615                 Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> operationRes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) interfaceData.getUniqueId(),
616                                 GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class);
617                 if (operationRes.isRight()) {
618                         log.error("Failed to find operation  {} on interface {}", operationName, interfaceName);
619                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationRes.right().value()));
620
621                 } else {
622                         List<ImmutablePair<OperationData, GraphEdge>> operations = operationRes.left().value();
623                         for (ImmutablePair<OperationData, GraphEdge> operationPairEdge : operations) {
624                                 GraphEdge opEdge = operationPairEdge.getRight();
625                                 OperationData opData = operationPairEdge.getLeft();
626                                 Map<String, Object> opEdgeProp = opEdge.getProperties();
627                                 if (opEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(operationName)) {
628
629                                         return copyAndCreateNewOperation(operation, interfaceName, operationName, null, // changed
630                                                                                                                                                                                                         // from
631                                                                                                                                                                                                         // newOperation
632                                                         newInterfaceNode, operationRes, opData);
633
634                                 }
635                         }
636                 }
637                 // if(newOperation == null)
638                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
639                 // else
640                 // return Either.left(newOperation);
641         }
642
643         private Either<InterfaceData, TitanOperationStatus> findInterfaceOnParentNode(String resourceId, String interfaceName) {
644
645                 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, TitanOperationStatus> parentRes = titanGenericDao.getChild(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
646                                 ResourceMetadataData.class);
647                 if (parentRes.isRight()) {
648                         log.debug("interface {} not found ", interfaceName);
649                         return Either.right(parentRes.right().value());
650                 }
651                 ImmutablePair<ResourceMetadataData, GraphEdge> parenNode = parentRes.left().value();
652
653                 Either<List<ImmutablePair<InterfaceData, GraphEdge>>, TitanOperationStatus> childrenNodes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), parenNode.getKey().getMetadataDataDefinition().getUniqueId(),
654                                 GraphEdgeLabels.INTERFACE, NodeTypeEnum.Interface, InterfaceData.class);
655                 if (childrenNodes.isRight()) {
656                         return findInterfaceOnParentNode(parenNode.getKey().getMetadataDataDefinition().getUniqueId(), interfaceName);
657
658                 } else {
659                         for (ImmutablePair<InterfaceData, GraphEdge> interfaceDataNode : childrenNodes.left().value()) {
660
661                                 GraphEdge interfaceEdge = interfaceDataNode.getRight();
662                                 Map<String, Object> interfaceEdgeProp = interfaceEdge.getProperties();
663
664                                 if (interfaceEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(interfaceName)) {
665                                         return Either.left(interfaceDataNode.getKey());
666                                 }
667
668                         }
669                         return findInterfaceOnParentNode(parenNode.getKey().getMetadataDataDefinition().getUniqueId(), interfaceName);
670                 }
671
672         }
673
674         @Override
675         public Either<InterfaceDefinition, StorageOperationStatus> createInterfaceOnResource(InterfaceDefinition interf, String resourceId, String interfaceName, boolean failIfExist, boolean inTransaction) {
676
677                 Either<InterfaceData, TitanOperationStatus> status = addInterfaceToGraph(interf, interfaceName, resourceId);
678
679                 if (status.isRight()) {
680                         titanGenericDao.rollback();
681                         log.error("Failed to add interface {} to resource {}", interfaceName, resourceId);
682                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status.right().value()));
683                 } else {
684
685                         if (false == inTransaction) {
686                                 titanGenericDao.commit();
687                         }
688                         InterfaceData interfaceData = status.left().value();
689
690                         InterfaceDefinition interfaceDefResult = convertInterfaceDataToInterfaceDefinition(interfaceData);
691                         Map<String, Operation> operations = interf.getOperationsMap();
692                         if (operations != null && !operations.isEmpty()) {
693                                 Set<String> opNames = operations.keySet();
694                                 Map<String, Operation> newOperations = new HashMap<String, Operation>();
695                                 for (String operationName : opNames) {
696
697                                         Operation op = operations.get(operationName);
698                                         Either<OperationData, TitanOperationStatus> opStatus = addOperationToGraph(interf, operationName, op, interfaceData);
699                                         if (status.isRight()) {
700                                                 titanGenericDao.rollback();
701                                                 log.error("Failed to add operation {} to interface {}", operationName, interfaceName);
702                                         } else if (status.isLeft()) {
703                                                 if (false == inTransaction) {
704                                                         titanGenericDao.commit();
705                                                 }
706                                                 OperationData opData = opStatus.left().value();
707                                                 Operation newOperation = this.convertOperationDataToOperation(opData);
708
709                                                 ArtifactDefinition art = op.getImplementationArtifact();
710                                                 if (art != null) {
711                                                         Either<ArtifactDefinition, StorageOperationStatus> artRes = artifactOperation.addArifactToComponent(art, (String) opData.getUniqueId(), NodeTypeEnum.InterfaceOperation, failIfExist, true);
712                                                         if (artRes.isRight()) {
713                                                                 titanGenericDao.rollback();
714                                                                 log.error("Failed to add artifact {} to interface {}", operationName, interfaceName);
715                                                         } else {
716                                                                 newOperation.setImplementation(artRes.left().value());
717                                                         }
718                                                         newOperations.put(operationName, newOperation);
719                                                 }
720                                         }
721                                 }
722                                 interfaceDefResult.setOperationsMap(newOperations);
723                         }
724                         log.debug("The returned InterfaceDefintion is {}", interfaceDefResult);
725                         return Either.left(interfaceDefResult);
726                 }
727
728         }
729
730         @Override
731         public Either<Operation, StorageOperationStatus> deleteInterfaceOperation(String resourceId, String interfaceName, String operationId, boolean inTransaction) {
732
733                 Either<Operation, TitanOperationStatus> status = removeOperationOnGraph(resourceId, interfaceName, operationId);
734                 if (status.isRight()) {
735                         if (false == inTransaction) {
736                                 titanGenericDao.rollback();
737                         }
738                         log.error("Failed to delete operation {} of interface {} resource {}", operationId, interfaceName, resourceId);
739                         return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status.right().value()));
740                 } else {
741                         if (false == inTransaction) {
742                                 titanGenericDao.commit();
743                         }
744
745                         Operation opDefResult = status.left().value();// convertOperationDataToOperation(operationData);
746                         log.debug("The returned Operation is {}", opDefResult);
747                         return Either.left(opDefResult);
748                 }
749
750         }
751
752         private Either<Operation, TitanOperationStatus> removeOperationOnGraph(String resourceId, String interfaceName, String operationId) {
753                 log.debug("Before deleting operation from graph {}", operationId);
754
755                 Either<List<ImmutablePair<InterfaceData, GraphEdge>>, TitanOperationStatus> childrenNodes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), resourceId, GraphEdgeLabels.INTERFACE, NodeTypeEnum.Interface,
756                                 InterfaceData.class);
757
758                 if (childrenNodes.isRight()) {
759                         log.debug("Not found interface {}", interfaceName);
760                         return Either.right(childrenNodes.right().value());
761                 }
762                 OperationData opData = null;
763                 for (ImmutablePair<InterfaceData, GraphEdge> interfaceDataNode : childrenNodes.left().value()) {
764
765                         GraphEdge interfaceEdge = interfaceDataNode.getRight();
766                         Map<String, Object> interfaceEdgeProp = interfaceEdge.getProperties();
767
768                         String interfaceSplitedName = splitType(interfaceName);
769
770                         if (interfaceEdgeProp.get(GraphPropertiesDictionary.NAME.getProperty()).equals(interfaceSplitedName)) {
771                                 Either<List<ImmutablePair<OperationData, GraphEdge>>, TitanOperationStatus> operationRes = titanGenericDao.getChildrenNodes(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) interfaceDataNode.getLeft().getUniqueId(),
772                                                 GraphEdgeLabels.INTERFACE_OPERATION, NodeTypeEnum.InterfaceOperation, OperationData.class);
773                                 if (operationRes.isRight()) {
774                                         log.error("Failed to find operation {} on interface {}", operationId, interfaceName);
775                                         return Either.right(operationRes.right().value());
776                                 }
777                                 List<ImmutablePair<OperationData, GraphEdge>> operations = operationRes.left().value();
778
779                                 for (ImmutablePair<OperationData, GraphEdge> operationPairEdge : operations) {
780
781                                         opData = operationPairEdge.getLeft();
782                                         if (opData.getUniqueId().equals(operationId)) {
783
784                                                 Either<ImmutablePair<ArtifactData, GraphEdge>, TitanOperationStatus> artifactRes = titanGenericDao.getChild(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), (String) operationPairEdge.getLeft().getUniqueId(),
785                                                                 GraphEdgeLabels.ARTIFACT_REF, NodeTypeEnum.ArtifactRef, ArtifactData.class);
786                                                 Either<ArtifactDefinition, StorageOperationStatus> arStatus = null;
787                                                 if (artifactRes.isLeft()) {
788                                                         ArtifactData arData = artifactRes.left().value().getKey();
789                                                         arStatus = artifactOperation.removeArifactFromResource((String) operationPairEdge.getLeft().getUniqueId(), (String) arData.getUniqueId(), NodeTypeEnum.InterfaceOperation, true, true);
790                                                         if (arStatus.isRight()) {
791                                                                 log.debug("failed to delete artifact {}", arData.getUniqueId());
792                                                                 return Either.right(TitanOperationStatus.INVALID_ID);
793                                                         }
794                                                 }
795                                                 Either<OperationData, TitanOperationStatus> deleteOpStatus = titanGenericDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.InterfaceOperation), opData.getUniqueId(), OperationData.class);
796                                                 if (deleteOpStatus.isRight()) {
797                                                         log.debug("failed to delete operation {}", opData.getUniqueId());
798                                                         return Either.right(TitanOperationStatus.INVALID_ID);
799                                                 }
800                                                 opData = deleteOpStatus.left().value();
801                                                 Operation operation = new Operation(opData.getOperationDataDefinition());
802                                                 if (arStatus != null) {
803                                                         operation.setImplementation(arStatus.left().value());
804                                                 }
805                                                 if (operations.size() <= 1) {
806                                                         Either<InterfaceData, TitanOperationStatus> deleteInterfaceStatus = titanGenericDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Interface), interfaceDataNode.left.getUniqueId(), InterfaceData.class);
807                                                         if (deleteInterfaceStatus.isRight()) {
808                                                                 log.debug("failed to delete interface {}", interfaceDataNode.left.getUniqueId());
809                                                                 return Either.right(TitanOperationStatus.INVALID_ID);
810                                                         }
811
812                                                 }
813
814                                                 return Either.left(operation);
815
816                                         }
817                                 }
818                         }
819                 }
820
821                 log.debug("Not found operation {}", interfaceName);
822                 return Either.right(TitanOperationStatus.INVALID_ID);
823         }
824
825         private String splitType(String interfaceName) {
826                 String interfaceSplittedName;
827                 String[] packageName = interfaceName.split("\\.");
828
829                 if (packageName.length == 0) {
830                         interfaceSplittedName = interfaceName;
831                 } else {
832                         interfaceSplittedName = packageName[packageName.length - 1];
833                 }
834
835                 return interfaceSplittedName.toLowerCase();
836         }
837
838         /**
839          * FOR TEST ONLY
840          * 
841          * @param titanGenericDao
842          */
843         public void setTitanGenericDao(TitanGenericDao titanGenericDao) {
844                 this.titanGenericDao = titanGenericDao;
845         }
846
847         public void setArtifactOperation(ArtifactOperation artifactOperation) {
848                 this.artifactOperation = artifactOperation;
849         }
850
851         @Override
852         public Either<InterfaceDefinition, StorageOperationStatus> createInterfaceType(InterfaceDefinition interf, boolean inTransaction) {
853                 Either<InterfaceDefinition, StorageOperationStatus> result = null;
854                 try {
855
856                         InterfaceData interfaceData = new InterfaceData(interf);
857                         interf.setUniqueId(interf.getType().toLowerCase());
858
859                         Either<InterfaceData, TitanOperationStatus> existInterface = titanGenericDao.getNode(interfaceData.getUniqueIdKey(), interfaceData.getUniqueId(), InterfaceData.class);
860
861                         if (existInterface.isLeft()) {
862                                 // already exist
863                                 log.debug("Interface type already exist {}", interfaceData);
864                                 result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS);
865                                 return result;
866                         }
867
868                         log.debug("Before adding interface type to graph {}", interfaceData);
869                         Either<InterfaceData, TitanOperationStatus> createNodeResult = titanGenericDao.createNode(interfaceData, InterfaceData.class);
870                         log.debug("After adding property type to graph {}", interfaceData);
871
872                         if (createNodeResult.isRight()) {
873                                 TitanOperationStatus operationStatus = createNodeResult.right().value();
874                                 log.error("Failed to add interface {} to graph. status is {}", interf.getType(), operationStatus);
875                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(operationStatus));
876                                 return result;
877                         }
878
879                         InterfaceDefinition interfaceDefResult = convertInterfaceDataToInterfaceDefinition(interfaceData);
880                         Map<String, Operation> operations = interf.getOperationsMap();
881
882                         if (operations != null && !operations.isEmpty()) {
883                                 Map<String, Operation> newOperations = new HashMap<String, Operation>();
884
885                                 for (Map.Entry<String, Operation> operation : operations.entrySet()) {
886                                         Either<OperationData, TitanOperationStatus> opStatus = addOperationToGraph(interf, operation.getKey(), operation.getValue(), interfaceData);
887                                         if (opStatus.isRight()) {
888                                                 titanGenericDao.rollback();
889                                                 log.error("Failed to add operation {} to interface {}", operation.getKey(), interf.getType());
890
891                                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(opStatus.right().value()));
892                                                 return result;
893                                         } else {
894                                                 OperationData opData = opStatus.left().value();
895                                                 Operation newOperation = this.convertOperationDataToOperation(opData);
896                                                 newOperations.put(operation.getKey(), newOperation);
897                                         }
898                                 }
899                                 interfaceDefResult.setOperationsMap(newOperations);
900                         }
901                         result = Either.left(interfaceDefResult);
902                         return result;
903                 } finally {
904                         if (false == inTransaction) {
905                                 if (result == null || result.isRight()) {
906                                         log.error("Going to execute rollback on graph.");
907                                         titanGenericDao.rollback();
908                                 } else {
909                                         log.debug("Going to execute commit on graph.");
910                                         titanGenericDao.commit();
911                                 }
912                         }
913                 }
914
915         }
916
917         @Override
918         public Either<InterfaceDefinition, StorageOperationStatus> getInterface(String interfaceId) {
919                 Either<InterfaceData, TitanOperationStatus> getResult = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Interface), interfaceId, InterfaceData.class);
920                 if (getResult.isLeft()) {
921                         InterfaceData interfaceData = getResult.left().value();
922                         return Either.left(convertInterfaceDataToInterfaceDefinition(interfaceData));
923                 } else {
924                         TitanOperationStatus titanStatus = getResult.right().value();
925                         log.debug("Node with id {} was not found in the graph. status: {}", interfaceId, titanStatus);
926                         StorageOperationStatus storageOperationStatus = DaoStatusConverter.convertTitanStatusToStorageStatus(titanStatus);
927                         return Either.right(storageOperationStatus);
928                 }
929         }
930
931         public String getShortInterfaceName(InterfaceDataDefinition interfaceDefinition) {
932                 String[] packageName = interfaceDefinition.getType().split("\\.");
933                 String interfaceName;
934                 if (packageName.length == 0) {
935                         interfaceName = interfaceDefinition.getType();
936                 } else {
937                         interfaceName = packageName[packageName.length - 1];
938                 }
939                 return interfaceName.toLowerCase();
940         }
941
942         /** 
943          * 
944          */
945         public Either<InterfaceDefinition, StorageOperationStatus> createInterfaceType(InterfaceDefinition interf) {
946                 return createInterfaceType(interf, false);
947         }
948
949 }