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