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