aboutsummaryrefslogtreecommitdiffstats
path: root/src/chameleon/aai_processor.clj
blob: d1a7d25e2fdbb9b59d56676ed219c6717954e196 (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
(ns chameleon.aai-processor
  (:require
   [chameleon.route :refer :all]
   [cheshire.core :refer :all]
   [integrant.core :as ig]
   [clojure.set :refer :all]))

(defonce ^:private p-attr (atom nil))
(defonce ^:private t-attr (atom nil))

(defmethod ig/init-key :chameleon/aai-processor  [_ {:keys [provenance-attr truth-attr]}]
  (reset! p-attr provenance-attr)
  (reset! t-attr truth-attr))

(defmethod ig/halt-key! :chameleon/aai-processor [_ _]
  (reset! p-attr nil)
  (reset! t-attr nil))

(defn- map-keywords
  "Maps all string based keys to keywords"
  [kmap]
  (into {} (for [[key value] kmap] [(keyword key) value])))

(defn- gen-trim-relationship
  "Generates a trimmed down version of the relationship containing only the id, type, url, and target"
  [relationship]
  (let [id (relationship"_id")
        type (get-in relationship ["properties" "type"])
        src-id (get-in relationship ["source" "id"])
        src-type (get-in relationship ["source" "type"])
        target-id (get-in relationship ["target" "id"])
        target-type (get-in relationship ["target" "type"])]
    {"id" id
     "type" type
     "source" {"id" src-id "type" src-type}
     "target" {"id" target-id "type" target-type}})
  )

(defn from-gallifrey
  "Transforms Gallifrey response payloads into a format consumable by AAI-centric clients"
  [body]
  (let [resource-type (get-in body ["properties" "_type"])
        id (body "_id")
        type (get-in body ["properties" "type"])
        properties (body "properties")]
    (if (= resource-type "entity")
                                        ; Transform into an entity type
      (let [relationships (body "relationships")]
        {
         "id" id
         "type" type
         "properties" (dissoc properties "_type" "type")
         "in" (into [] (map gen-trim-relationship (filter #(= (get-in % ["target" "id"]) id) relationships)))
         "out" (into [] (map gen-trim-relationship (filter #(= (get-in % ["source" "id"]) id) relationships)))
         })
                                        ; Transform into a relationship type
      {
       "id" id
       "type" type
       "properties" (dissoc properties "_type" "type")
       })))

(defn from-spike
  "Transforms Spike-based event payloads to a format accepted by Gallifrey for vertices and relationships"
  [gallifrey-host payload]
  (let [txpayload (map-keywords (parse-string payload))
        operation (:operation txpayload)
        parse-type (if (contains? txpayload :vertex)
                     :vertex
                     :relationship)
        entity-type (if (contains? txpayload :vertex)
                      :entity
                      :relationship)
        entity (map-keywords (parse-type txpayload))
        key (:key entity)
        properties (assoc (:properties entity) :type (:type entity))
        truth-time (if (not (nil?  (get properties @t-attr))) {:t-t (get properties @t-attr)})
        assertion  {:meta {:key key
                           :operation operation
                           :time truth-time}}
        provenance (get properties @p-attr "aai")]
    (assert-gallifrey gallifrey-host provenance (name entity-type) (if (= entity-type :entity)
                                                                     (assoc assertion  :body (generate-string {:properties properties}))
                                                                     (assoc assertion :body (generate-string (conj {:properties properties}
                                                                                                                   {:source  (rename-keys (:source entity) {"key" "id"})}
                                                                                                                   {:target (rename-keys (:target entity) {"key" "id"})})))))))