aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/crud/dao/champ/ChampDao.java
blob: f2178972b9f000849f0ed420948f0195f3ccf49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
/**
 * ============LICENSE_START=======================================================
 * Gizmo
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.crud.dao.champ;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.onap.aai.champcore.ChampGraph;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.exceptions.ChampMarshallingException;
import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
import org.onap.aai.champcore.exceptions.ChampTransactionException;
import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
import org.onap.aai.champcore.model.ChampObject;
import org.onap.aai.champcore.model.ChampRelationship;
import org.onap.aai.champcore.model.fluent.object.ObjectBuildOrPropertiesStep;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.crud.dao.GraphDao;
import org.onap.crud.entity.Edge;
import org.onap.crud.entity.Vertex;
import org.onap.crud.exception.CrudException;
import org.onap.crud.logging.CrudServiceMsgs;

/**
 * This is the integration layer between the CRUD API service and the low level
 * Champ library for graph database interaction.
 */
public class ChampDao implements GraphDao {

  public static final String CONFIG_STORAGE_BACKEND = "storage.backend";
  public static final String CONFIG_STORAGE_BACKEND_DB = "storage.backend.db";
  public static final String STORAGE_HBASE_DB = "hbase";
  public static final String STORAGE_CASSANDRA_DB = "cassandra";
  public static final String CONFIG_STORAGE_HOSTNAMES = "storage.hostnames";
  public static final String CONFIG_STORAGE_PORT = "storage.port";
  public static final String CONFIG_HBASE_ZNODE_PARENT = "storage.hbase.ext.zookeeper.znode.parent";
  public static final String CONFIG_GRAPH_NAME = "graph.name";
  public static final String GRAPH_UNQ_INSTANCE_ID_SUFFIX = "graph.unique-instance-id-suffix";

  public static final String CONFIG_EVENT_STREAM_PUBLISHER = "event.stream.publisher";
  public static final String CONFIG_EVENT_STREAM_NUM_PUBLISHERS = "event.stream.num-publishers";

  private static Map<String, ChampTransaction> transactions = new ConcurrentHashMap<String, ChampTransaction>();
  public static final String DEFAULT_GRAPH_NAME = "default_graph";

  private enum GraphType {
    IN_MEMORY, TITAN, DSE
  }

  /**
   * Instance of the API used for interacting with the Champ library.
   */
  private ChampGraph champApi = null;

  private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName());

  /**
   * Creates a new instance of the ChampDao.
   *
   * @param champGraph
   *          - Concrete implementation of the graph dao layer
   */
  public ChampDao(ChampGraph champGraph) {
    this.champApi = champGraph;
  }

  @Override
  public Vertex getVertex(String id) throws CrudException {

    try {

      if (logger.isDebugEnabled()) {
        logger.debug("getVertex with id: " + id);
      }

      long idAsLong = Long.parseLong(id);

      Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong);

      String nodeType = org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName();
      if (retrievedVertex.isPresent() && retrievedVertex.get().getProperties().get(nodeType) != null) {
        return vertexFromChampObject(retrievedVertex.get(),
            retrievedVertex.get().getProperties().get(nodeType).toString());
      } else {

        // We didn't find a vertex with the supplied id, so just throw an
        // exception.
        throw new CrudException("No vertex with id " + id + " found in graph",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

    } catch (ChampUnmarshallingException | ChampTransactionException e) {

      // Something went wrong - throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public Vertex getVertex(String id, String type) throws CrudException {

    try {

      if (logger.isDebugEnabled()) {
        logger.debug("getVertex with id: " + id);
      }

      long idAsLong = Long.parseLong(id);

      // Request the vertex from the graph db.
      Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong);

      // Did we find it?
      if (retrievedVertex.isPresent()
          && retrievedVertex.get().getProperties()
              .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) != null
          && retrievedVertex.get().getProperties()
              .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString()
              .equalsIgnoreCase(type)) {

        // Yup, convert it to a Vector object and return it.
        return vertexFromChampObject(retrievedVertex.get(), type);

      } else {

        // We didn't find a vertex with the supplied id, so just throw an
        // exception.
        throw new CrudException("No vertex with id " + id + " found in graph",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

    } catch (ChampUnmarshallingException | ChampTransactionException e) {

      // Something went wrong - throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public List<Edge> getVertexEdges(String id) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug("get Edges incident to vertex with id: " + id + " from graph");
    }

    try {
      long idAsLong = Long.parseLong(id); // GDF - what to do about id???

      // Request the vertex from the graph db.
      Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong);

      // Did we find it?
      if (retrievedVertex.isPresent()) {

        // Query the Champ library for the edges which are incident to the
        // specified
        // vertex.
        Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get());

        // Build an edge list from the result stream.
        List<Edge> edges = new ArrayList<Edge>();
        relationships.forEach(r -> edges.add(edgeFromChampRelationship(r)));

        return edges;

      } else {

        // We couldn't find the specified vertex, so throw an exception.
        throw new CrudException("No vertex with id " + id + " found in graph",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

    } catch (ChampUnmarshallingException e) {

      // Something went wrong, so throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);

    } catch (ChampObjectNotExistsException e) {

      // We couldn't find the specified vertex, so throw an exception.
      throw new CrudException("No vertex with id " + id + " found in graph",
          javax.ws.rs.core.Response.Status.NOT_FOUND);
    } catch (ChampTransactionException e) {
      throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public Vertex addVertex(String type, Map<String, Object> properties) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties));
    }

    // Add the aai_node_type so that AAI can read the data created by gizmo
    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);

    // Create an object to represent our vertex in the format expected by the
    // Champ library.
    ChampObject objectToCreate = buildChampObject(type, properties);

    try {

      // Ask the Champ library to store our vertex, placing the returned object
      // into a
      // list so that we can easily put that into our result object.
      return vertexFromChampObject(champApi.storeObject(objectToCreate), type);

    } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException
        | ChampTransactionException e) {

      // Something went wrong - throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties));
    }
    // Add the aai_node_type so that AAI can read the data created by gizmo
    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);

    try {
      // Now, build the updated version of the Champ Object...
      ChampObject updateObject = buildChampObject(id, type, properties);
      // ...and send it to the Champ library.
      return vertexFromChampObject(champApi.replaceObject(updateObject), type);

    } catch (ChampObjectNotExistsException e) {
      throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND);
    } catch (NumberFormatException | ChampMarshallingException | ChampSchemaViolationException e) {
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    } catch (ChampTransactionException e) {
      throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

  }

  @Override
  public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug("Retrieve vertices with type label: " + type + " which map query parameters: "
          + propertiesMapToString(filter));
    }

    filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);

    Stream<ChampObject> retrievedVertices;
    try {
      retrievedVertices = champApi.queryObjects(filter);

    } catch (ChampTransactionException e) {
      throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

    List<Vertex> vertices = retrievedVertices.map(v -> vertexFromChampObject(v, type)).collect(Collectors.toList());

    if (logger.isDebugEnabled()) {
      logger.debug("Resulting vertex list: " + retrievedVertices);
    }

    // ...and return it to the caller.
    return vertices;
  }

  private Object getRelKey(String id) {
    Object key = id;
    // convert into Long if applicable . TODO : revisit in story NUC-304
    try {
      key = Long.parseLong(id);
    } catch (NumberFormatException e) {
      // The id isn't a Long, leave it as a string
    }

    return key;
  }

  @Override
  public Edge getEdge(String id, String type) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug("Get edge with id: " + id);
    }

    try {

      // Request the edge from the graph db.
      Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id));

      // Did we find it?
      if (relationship.isPresent() && relationship.get().getType().equals(type)) {

        // Yup - return the result.
        return edgeFromChampRelationship(relationship.get());

      } else {

        // We didn't find an edge with the supplied id, so throw an exception.
        throw new CrudException("No edge with id " + id + " found in graph",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

    } catch (ChampUnmarshallingException | ChampTransactionException e) {

      // Something went wrong, so throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException {

    // For now, assume source and target are straight ids...
    try {

      Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get()));
      if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) {
        throw new CrudException(
            "Error creating edge - source vertex with id " + source + " does not exist in graph data base",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get()));
      if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) {
        throw new CrudException(
            "Error creating edge - target vertex with id " + target + " does not exist in graph data base",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      // Now, create the ChampRelationship object for our edge and store it in
      // the graph database.
      return edgeFromChampRelationship(champApi.storeRelationship(
          new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build()));

    } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException
        | ChampRelationshipNotExistsException | ChampUnmarshallingException | NumberFormatException
        | ChampTransactionException e) {

      throw new CrudException("Error creating edge: " + e.getMessage(),
          javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException {

    filter.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), type);

    Stream<ChampRelationship> retrievedRelationships;
    try {
      retrievedRelationships = champApi.queryRelationships(filter);

    } catch (ChampTransactionException e) {
      throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

    // Process the result stream from the Champ library into an Edge list,
    // keeping only
    // edges of the specified type.
    List<Edge> edges = retrievedRelationships.map(r -> edgeFromChampRelationship(r)).collect(Collectors.toList());

    return edges;
  }

  @Override
  public Edge updateEdge(Edge edge) throws CrudException {

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties()));
    }

    try {
      // Now, build the updated version of the Champ Relationship...
      ChampRelationship updateRelationship = new ChampRelationship.Builder(
          buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(),
              edge.getSource().getProperties()),
          buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(),
              edge.getTarget().getProperties()),
          edge.getType()).key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build();
      // ...and send it to the Champ library.
      return edgeFromChampRelationship(champApi.replaceRelationship(updateRelationship));

    } catch (ChampRelationshipNotExistsException ex) {
      throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND);
    } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException
        | ChampSchemaViolationException | ChampTransactionException ex) {

      throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public void deleteVertex(String id, String type) throws CrudException {

    try {

      // First, retrieve the vertex that we intend to delete.
      Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id));

      // Did we find it?
      if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) {
        throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

      // Now, verify that there are no edges incident to the vertex (they must
      // be deleted
      // first if so).
      Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get());

      if (relationships.count() > 0) {
        throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      // Finally, we can attempt to delete our vertex.
      champApi.deleteObject(Long.parseLong(id));

    } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException
        | ChampTransactionException e) {

      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public void deleteEdge(String id, String type) throws CrudException {

    try {

      // First, retrieve the edge that we want to delete.
      Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id));

      // Did we find it?
      if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) {
        throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

      // Now we can delete the edge.
      champApi.deleteRelationship(relationshipToDelete.get());

    } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException
        | ChampTransactionException e) {

      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  /**
   * This helper method generates a string representation of a properties map
   * for logging purposes.
   *
   * @param properties
   *          - The properties map to be converted.
   * @return - The log statement friendly conversion of the properties map.
   */
  private String propertiesMapToString(Map<String, Object> properties) {

    StringBuilder sb = new StringBuilder();
    sb.append("{");

    for (String key : properties.keySet()) {
      sb.append("(").append(key).append(" -> ").append(properties.get(key)).append(") ");
    }

    sb.append("}");

    return sb.toString();
  }

  /**
   * This helper method constructs a {@link ChampObject} suitable for passing to
   * the Champ library.
   *
   * @param type
   *          - The type to assign to our ChampObject
   * @param properties
   *          - The set of properties to assign to our ChampObject
   * @return - A populated ChampObject
   */
  private ChampObject buildChampObject(String type, Map<String, Object> properties) {

    ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withoutKey();

    for (String key : properties.keySet()) {
      objectInProgress.withProperty(key, properties.get(key));
    }
    return objectInProgress.build();
  }

  /**
   * This helper method constructs a {@link ChampObject} suitable for passing to
   * the Champ library.
   *
   * @param id
   *          - Unique identifier for this object.
   * @param type
   *          - The type to assign to our ChampObject
   * @param properties
   *          - The set of properties to assign to our ChampObject
   * @return - A populated ChampObject
   */
  private ChampObject buildChampObject(String id, String type, Map<String, Object> properties) {

    ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withKey(Long.parseLong(id));

    for (String key : properties.keySet()) {
      objectInProgress.withProperty(key, properties.get(key));
    }
    return objectInProgress.build();
  }

  private Vertex vertexFromChampObject(ChampObject champObject, String type) {

    // Get the identifier for this vertex from the Champ object.
    Object id = champObject.getKey().orElse("");

    // Start building our {@link Vertex} object.
    Vertex.Builder vertexBuilder = new Vertex.Builder(type);
    vertexBuilder.id(id.toString());

    // Convert the properties associated with the Champ object into the form
    // expected for
    // a Vertex object.
    for (String key : champObject.getProperties().keySet()) {
      vertexBuilder.property(key, champObject.getProperties().get(key));
    }

    // ...and return it.
    return vertexBuilder.build();
  }

  /**
   * This helper method converts a {@link ChampRelationship} from the Champ
   * library into an equivalent {@link Edge} object that is understood by the
   * CRUD Service.
   *
   * @param relationship
   *          - The ChampRelationship object to be converted.
   * @return - An Edge object corresponding to the supplied ChampRelationship
   */
  private Edge edgeFromChampRelationship(ChampRelationship relationship) {

    // Populate the edge's id, if available.
    Object relationshipId = relationship.getKey().orElse("");

    Edge.Builder edgeBuilder = new Edge.Builder(relationship.getType()).id(relationshipId.toString());
    edgeBuilder.source(vertexFromChampObject(relationship.getSource(),
        relationship.getSource().getProperties()
            .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null
                ? relationship.getSource().getType()
                : relationship.getSource().getProperties()
                    .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString()));
    edgeBuilder.target(vertexFromChampObject(relationship.getTarget(),
        relationship.getTarget().getProperties()
            .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null
                ? relationship.getTarget().getType()
                : relationship.getTarget().getProperties()
                    .get(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString()));

    for (String key : relationship.getProperties().keySet()) {
      edgeBuilder.property(key, relationship.getProperties().get(key).toString());
    }

    return edgeBuilder.build();
  }

  /**
   * Performs any necessary shut down operations when the DAO is no longer
   * needed.
   */
  public void close() {

    if (champApi != null) {

      logger.info(CrudServiceMsgs.STOPPING_CHAMP_DAO);

      champApi.shutdown();
    }
  }

  @Override
  public String openTransaction() {

    ChampTransaction transaction = champApi.openTransaction();

    transactions.put(transaction.id(), transaction);
    logger.info(CrudServiceMsgs.TRANSACTION, "Stored transaction " + transaction.id() + " in hashmap");
    logger.info(CrudServiceMsgs.TRANSACTION, "Hash map contents:");
    for (String key : transactions.keySet()) {
      logger.info(CrudServiceMsgs.TRANSACTION, key);
    }
    return transaction.id();
  }

  @Override
  public void commitTransaction(String id) throws CrudException {

    try {
      champApi.commitTransaction(getTransaction(id));
    } catch (ChampTransactionException e) {
      throw new CrudException("Error while commiting transaction " + id,
          javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
    transactions.remove(id);
  }

  @Override
  public void rollbackTransaction(String id) throws CrudException {

    try {
      champApi.rollbackTransaction(getTransaction(id));
    } catch (ChampTransactionException e) {
      throw new CrudException("Error while transaction rollback " + id,
          javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
    transactions.remove(id);
  }

  private ChampTransaction getTransaction(String id) throws CrudException {

    logger.info(CrudServiceMsgs.TRANSACTION, "Looking up transaction " + id);
    if (transactions.containsKey(id)) {
      logger.info(CrudServiceMsgs.TRANSACTION, "Found it!");
      return (transactions.get(id));
    } else {
      logger.info(CrudServiceMsgs.TRANSACTION, "Didn't find transaction id " + id + ".  Hash map contains: ");
      for (String key : transactions.keySet()) {
        logger.info(CrudServiceMsgs.TRANSACTION, key);
      }
      throw new CrudException("No open transaction with id: " + id, javax.ws.rs.core.Response.Status.NOT_FOUND);
    }
  }

  @Override
  public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException {
    if (logger.isDebugEnabled()) {
      logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties));
    }

    // Add the aai_node_type so that AAI can read the data created by gizmo
    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);

    // Create an object to represent our vertex in the format expected by the
    // Champ library.
    ChampObject objectToCreate = buildChampObject(type, properties);

    try {

      // Ask the Champ library to store our vertex, placing the returned object
      // into a
      // list so that we can easily put that into our result object.
      return vertexFromChampObject(champApi.storeObject(objectToCreate, Optional.of(getTransaction(txId))), type);

    } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException
        | ChampTransactionException e) {

      // Something went wrong - throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId)
      throws CrudException {
    // For now, assume source and target are straight ids...
    try {

      Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get()),
          Optional.of(getTransaction(txId)));
      if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) {
        throw new CrudException(
            "Error creating edge - source vertex with id " + source + " does not exist in graph data base",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get()),
          Optional.of(getTransaction(txId)));
      if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) {
        throw new CrudException(
            "Error creating edge - target vertex with id " + target + " does not exist in graph data base",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      // Now, create the ChampRelationship object for our edge and store it in
      // the graph database.
      return edgeFromChampRelationship(champApi.storeRelationship(
          new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build(),
          Optional.of(getTransaction(txId))));

    } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException
        | ChampTransactionException | ChampRelationshipNotExistsException | ChampUnmarshallingException e) {

      throw new CrudException("Error creating edge: " + e.getMessage(),
          javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

  }

  @Override
  public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException {
    if (logger.isDebugEnabled()) {
      logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties));
    }
    // Add the aai_node_type so that AAI can read the data created by gizmo
    properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);

    try {
      // Now, build the updated version of the Champ Object...
      ChampObject updateObject = buildChampObject(id, type, properties);
      // ...and send it to the Champ library.
      return vertexFromChampObject(champApi.replaceObject(updateObject, Optional.of(getTransaction(txId))), type);

    } catch (ChampObjectNotExistsException e) {
      throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND);
    } catch (NumberFormatException | ChampMarshallingException | ChampTransactionException
        | ChampSchemaViolationException e) {
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public boolean transactionExists(String id) throws CrudException {
    return transactions.containsKey(id);
  }

  @Override
  public void deleteVertex(String id, String type, String txId) throws CrudException {
    try {

      // First, retrieve the vertex that we intend to delete.
      Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id),
          Optional.of(getTransaction(txId)));

      // Did we find it?
      if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) {
        throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

      // Now, verify that there are no edges incident to the vertex (they must
      // be deleted
      // first if so).
      Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get(),
          Optional.of(getTransaction(txId)));

      if (relationships.count() > 0) {
        throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.",
            javax.ws.rs.core.Response.Status.BAD_REQUEST);
      }

      // Finally, we can attempt to delete our vertex.
      champApi.deleteObject(Long.parseLong(id), Optional.of(getTransaction(txId)));

    } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException
        | ChampTransactionException e) {

      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

  }

  @Override
  public Edge updateEdge(Edge edge, String txId) throws CrudException {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties()));
    }

    try {
      // Now, build the updated version of the Champ Relationship...
      ChampRelationship updateRelationship = new ChampRelationship.Builder(
          buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(),
              edge.getSource().getProperties()),
          buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(),
              edge.getTarget().getProperties()),
          edge.getType()).key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build();
      // ...and send it to the Champ library.
      return edgeFromChampRelationship(
          champApi.replaceRelationship(updateRelationship, Optional.of(getTransaction(txId))));

    } catch (ChampRelationshipNotExistsException ex) {
      throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND);
    } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException
        | ChampSchemaViolationException | ChampTransactionException ex) {

      throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

  @Override
  public void deleteEdge(String id, String type, String txId) throws CrudException {
    try {

      // First, retrieve the edge that we want to delete.
      Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id),
          Optional.of(getTransaction(txId)));

      // Did we find it?
      if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) {
        throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

      // Now we can delete the edge.
      champApi.deleteRelationship(relationshipToDelete.get(), Optional.of(getTransaction(txId)));

    } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException
        | ChampTransactionException e) {

      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }

  }

  @Override
  public Edge getEdge(String id, String type, String txId) throws CrudException {
    if (logger.isDebugEnabled()) {
      logger.debug("Get edge with id: " + id);
    }

    try {

      // Request the edge from the graph db.
      Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id),
          Optional.of(getTransaction(txId)));

      // Did we find it?
      if (relationship.isPresent() && relationship.get().getType().equals(type)) {

        // Yup - return the result.
        return edgeFromChampRelationship(relationship.get());

      } else {

        // We didn't find an edge with the supplied id, so throw an exception.
        throw new CrudException("No edge with id " + id + " found in graph",
            javax.ws.rs.core.Response.Status.NOT_FOUND);
      }

    } catch (ChampUnmarshallingException | ChampTransactionException e) {

      // Something went wrong, so throw an exception.
      throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
    }
  }

}