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