From 36b5671af2c3eec5ca81663382c4ca2898f79e55 Mon Sep 17 00:00:00 2001 From: Adrian Batos-Parac Date: Mon, 30 Apr 2018 16:15:45 -0400 Subject: Fix provenance capabilities Fixes the issue where provenance was not properly being propagated to the Gallifrey server. Change-Id: Icc5f4fd3499a04aa710511879fc2d827d2bcc618 Issue-ID: AAI-797 Signed-off-by: abatos --- src/chameleon/aai_processor.clj | 68 +++++++++++++++++++++++++++-------------- src/chameleon/config.clj | 6 +++- src/chameleon/event.clj | 4 +-- src/chameleon/handler.clj | 10 +++--- src/chameleon/route.clj | 10 +++--- src/chameleon/txform.clj | 19 ------------ 6 files changed, 61 insertions(+), 56 deletions(-) delete mode 100644 src/chameleon/txform.clj diff --git a/src/chameleon/aai_processor.clj b/src/chameleon/aai_processor.clj index 7025c31..d1a7d25 100644 --- a/src/chameleon/aai_processor.clj +++ b/src/chameleon/aai_processor.clj @@ -1,8 +1,25 @@ (ns chameleon.aai-processor - (:require [chameleon.txform :refer :all] - [chameleon.route :refer :all] - [cheshire.core :refer :all] - [clojure.set :refer :all])) + (: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" @@ -25,19 +42,23 @@ (let [resource-type (get-in body ["properties" "_type"]) id (body "_id") type (get-in body ["properties" "type"]) - properties (body "properties") - entity-response { - "id" id - "type" type - "properties" (dissoc properties "_type" "type") - }] + properties (body "properties")] (if (= resource-type "entity") ; Transform into an entity type (let [relationships (body "relationships")] - (assoc entity-response - "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))))) - entity-response))) + { + "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" @@ -53,12 +74,13 @@ entity (map-keywords (parse-type txpayload)) key (:key entity) properties (assoc (:properties entity) :type (:type entity)) - truth-time (:truth-time entity) - entity-assertion {:meta {:key key - :operation operation - :time {:t-t truth-time}}}] - (assert-gallifrey gallifrey-host "aai" (name entity-type) (if (= entity-type :entity) - (assoc entity-assertion :body (generate-string {:properties properties})) - (assoc entity-assertion :body (generate-string (conj {:properties properties} - {:source (rename-keys (:source entity) {"key" "id"})} - {:target (rename-keys (:target entity) {"key" "id"})}))))))) + 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"})}))))))) diff --git a/src/chameleon/config.clj b/src/chameleon/config.clj index 4982f4a..bd99096 100644 --- a/src/chameleon/config.clj +++ b/src/chameleon/config.clj @@ -10,7 +10,11 @@ [:aai :processor] from-spike) :gallifrey-host (:gallifrey-host app-config)} :chameleon/handler - {:gallifrey-host (:gallifrey-host app-config)} + {:gallifrey-host (:gallifrey-host app-config) + :gallifrey-transformer from-gallifrey} + :chameleon/aai-processor + {:provenance-attr "last-mod-source-of-truth" + :truth-attr "truth-time"} :chameleon/http-server {:port (:http-port app-config) :handler (ig/ref :chameleon/handler)}}] diff --git a/src/chameleon/event.clj b/src/chameleon/event.clj index 2b32d26..c4bec8e 100644 --- a/src/chameleon/event.clj +++ b/src/chameleon/event.clj @@ -1,7 +1,5 @@ (ns chameleon.event - (:require [chameleon.txform] - [chameleon.route] - [integrant.core :as ig] + (:require [integrant.core :as ig] [clojure.string :refer [starts-with?]]) (:import [org.onap.aai.event.client DMaaPEventConsumer])) diff --git a/src/chameleon/handler.clj b/src/chameleon/handler.clj index 2a4c0f7..fd97bf1 100644 --- a/src/chameleon/handler.clj +++ b/src/chameleon/handler.clj @@ -1,6 +1,5 @@ (ns chameleon.handler (:require [chameleon.route :as c-route] - [chameleon.aai-processor] [utilis.map :refer [map-vals compact]] [liberator.core :refer [defresource]] [compojure.core :refer [GET PUT PATCH ANY defroutes]] @@ -16,13 +15,16 @@ (declare handler) (defonce ^:private g-host (atom nil)) +(defonce ^:private g-transformer nil) -(defmethod ig/init-key :chameleon/handler [_ {:keys [gallifrey-host]}] +(defmethod ig/init-key :chameleon/handler [_ {:keys [gallifrey-host gallifrey-transformer]}] (reset! g-host gallifrey-host) + (def g-transformer gallifrey-transformer) handler) (defmethod ig/halt-key! :chameleon/handler [_ _] - (reset! g-host nil)) + (reset! g-host nil) + (def g-transformer nil)) (declare serialize de-serialize) @@ -39,7 +41,7 @@ :body json/parse-string (dissoc "_meta") - (chameleon.aai-processor/from-gallifrey))}))) + (g-transformer))}))) :existed? (fn [ctx] (when-let [status (-> (c-route/query @g-host id type (-> ctx :request diff --git a/src/chameleon/route.clj b/src/chameleon/route.clj index 30a2199..e8f3a3c 100644 --- a/src/chameleon/route.clj +++ b/src/chameleon/route.clj @@ -59,14 +59,12 @@ (defn assert-gallifrey [host actor type payload] "Propagates an assertion to Gallifrey based off of an event payload coming in from the event service." (let [{:keys [meta body]} payload - {:keys [key operation time]} meta - time-map (filter val time)] - (println time-map) + {:keys [key operation time]} meta] (println operation " " type " with key " key) (interpret-response key (case operation "CREATE" - (assert-create host actor type key body time-map) + (assert-create host actor type key body time) "UPDATE" - (assert-update host actor type key body time-map) + (assert-update host actor type key body time) "DELETE" - (assert-delete host actor type key time-map))))) + (assert-delete host actor type key time))))) diff --git a/src/chameleon/txform.clj b/src/chameleon/txform.clj deleted file mode 100644 index 95f357f..0000000 --- a/src/chameleon/txform.clj +++ /dev/null @@ -1,19 +0,0 @@ -(ns chameleon.txform - (:require [cheshire.core :refer :all] - [clojure.string :as str])) - -(defn map-keywords - "Maps all string based keys to keywords" - [kmap] - (into {} (for [[key value] kmap] [(keyword key) value]))) - -(defn flatten-key - "Maps a parent-child pair to a period separated keyword" - [parent child] - (keyword (str (name parent) "." (name child)))) - -(defn flatten-entry - "Flattens a nested map entry to a period separated keyword entry" - [map key] - (reduce #(assoc %1 (flatten-key key (first %2)) (second %2)) - {} (seq (key map)))) -- cgit 1.2.3-korg