summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Forsyth <jf2512@att.com>2018-04-19 13:22:42 +0000
committerGerrit Code Review <gerrit@onap.org>2018-04-19 13:22:42 +0000
commit8e52114cf9289dd85c8838bf0eb8c24f5e2c7f99 (patch)
tree9810e6db0a6f8dc167bd23ca3db4a24f15939490 /src
parenta99f8b47866dbdd0afd5a563ef9f8896501f62e6 (diff)
parentf796373d7de201c0a46f06b3155b1734b9872bb3 (diff)
Merge "Fix graphwalk capabilities"
Diffstat (limited to 'src')
-rw-r--r--src/chameleon/aai_processor.clj58
-rw-r--r--src/chameleon/event.clj9
-rw-r--r--src/chameleon/handler.clj22
-rw-r--r--src/chameleon/route.clj41
4 files changed, 92 insertions, 38 deletions
diff --git a/src/chameleon/aai_processor.clj b/src/chameleon/aai_processor.clj
index c709ed1..7025c31 100644
--- a/src/chameleon/aai_processor.clj
+++ b/src/chameleon/aai_processor.clj
@@ -1,26 +1,64 @@
(ns chameleon.aai-processor
(:require [chameleon.txform :refer :all]
[chameleon.route :refer :all]
- [cheshire.core :refer :all]))
+ [cheshire.core :refer :all]
+ [clojure.set :refer :all]))
+
+(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]
- (->> body
- (map (fn [[k v]] [(clojure.string/split k #"\.") v]))
- ((fn [x] (reduce #(assoc-in %1 (first %2) (second %2) ) {} x)))))
+ (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")
+ }]
+ (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)))
(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)
- :vertex
+ :entity
:relationship)
- entity (map-keywords (entity-type txpayload))
+ entity (map-keywords (parse-type txpayload))
key (:key entity)
- properties (assoc (:properties entity) :type (:type entity))]
- (assert-gallifrey gallifrey-host "aai" (if (= entity-type :vertex)
- {:meta {:key key :operation operation} :body (generate-string properties)}
- {:meta {:key key :operation operation} :body (generate-string (conj properties (flatten-entry entity :source) (flatten-entry entity :target)))}))))
+ 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"})})))))))
diff --git a/src/chameleon/event.clj b/src/chameleon/event.clj
index 8201acb..2b32d26 100644
--- a/src/chameleon/event.clj
+++ b/src/chameleon/event.clj
@@ -1,7 +1,8 @@
(ns chameleon.event
(:require [chameleon.txform]
[chameleon.route]
- [integrant.core :as ig])
+ [integrant.core :as ig]
+ [clojure.string :refer [starts-with?]])
(:import [org.onap.aai.event.client DMaaPEventConsumer]))
(defmethod ig/init-key :chameleon/event
@@ -13,6 +14,8 @@
(let [it (.iterator (.consume event-processor))]
(println "Polling...")
(while (.hasNext it)
- (let [event (.next it)]
- (processor gallifrey-host event))))))))
+ (try (let [event (.next it)]
+ (if (not (starts-with? event "DMAAP")) ;Temporarily added for current version of dmaap client
+ (processor gallifrey-host event)))
+ (catch Exception e (println (str "Unexpected exception during processing: " (.getMessage e)))))))))))
))
diff --git a/src/chameleon/handler.clj b/src/chameleon/handler.clj
index 7a4a5fe..2a4c0f7 100644
--- a/src/chameleon/handler.clj
+++ b/src/chameleon/handler.clj
@@ -26,20 +26,32 @@
(declare serialize de-serialize)
-(defresource entity-endpoint [id]
+(defresource resource-endpoint [type id]
:allowed-methods [:get]
:available-media-types ["application/json"]
:exists? (fn [ctx]
- (let [resource (-> (c-route/query @g-host id (-> ctx :request :params :t-k)))]
+ (let [resource (c-route/query @g-host id type (-> ctx
+ :request
+ :params
+ (select-keys [:t-t :t-k])))] ; Only pass through the allowable set of keys
(when (= (:status resource) 200)
- {::resource (-> resource :body json/parse-string (dissoc "_meta") (chameleon.aai-processor/from-gallifrey))})))
+ {::resource (-> resource
+ :body
+ json/parse-string
+ (dissoc "_meta")
+ (chameleon.aai-processor/from-gallifrey))})))
:existed? (fn [ctx]
- (when-let [status (-> (c-route/query @g-host id (-> ctx :request :params :t-k)) :status)]
+ (when-let [status (-> (c-route/query @g-host id type (-> ctx
+ :request
+ :params
+ (select-keys [:t-t :t-k]))) ;Only pass through the allowable set of keys
+ :status)]
(= status 410)))
:handle-ok ::resource)
(defroutes app-routes
- (GET "/entity/:id" [id] (entity-endpoint id))
+ (GET "/entity/:id" [id] (resource-endpoint "entity" id))
+ (GET "/relationship/:id" [id] (resource-endpoint "relationship" id))
(resources "/"))
(def handler
diff --git a/src/chameleon/route.clj b/src/chameleon/route.clj
index 2b30f42..30a2199 100644
--- a/src/chameleon/route.clj
+++ b/src/chameleon/route.clj
@@ -12,23 +12,22 @@
"Retrieve an entity referenced by id at the provided host. Optionally provide
a time 't-k' that defines a query based on when the system knew about the
state of the entity."
- [host key & [k]]
+ [host key type & [gallifrey-params]]
@(kitclient/request {
- :url (str "https://" host "/entity/" key)
+ :url (str "https://" host "/" type "/" key)
:method :get
- :query-params (if-let [t-k k] {"t-k" t-k})
+ :query-params gallifrey-params
:insecure? true
:keepalive 300
- :timeout 1000}))
+ :timeout 20000}))
(defn assert-create
"Creates an entity in Gallifrey with an initial set of assertions coming from the provided payload"
- [host actor key payload]
- (print "Final: " payload)
+ [host actor type key payload & [time-dimensions]]
(kitclient/request {
- :url (str "https://" host "/entity/" key)
+ :url (str "https://" host "/" type "/" key)
:method :put
- :query-params {"actor" actor "create" "true"}
+ :query-params (into {"actor" actor "create" "true"} time-dimensions)
:body payload
:insecure? true
:keepalive 300
@@ -36,11 +35,11 @@
(defn assert-update
"Update an entity in Gallifrey with a set of assertions coming from the provided payload"
- [host actor key payload]
+ [host actor type key payload & [time-dimensions]]
(kitclient/request {
- :url (str "https://" host "/entity/" key)
+ :url (str "https://" host "/" type "/" key)
:method :put
- :query-params {"actor" actor "changes-only" "true"}
+ :query-params (into {"actor" actor "changes-only" "true"} time-dimensions)
:body payload
:insecure? true
:keepalive 300
@@ -48,24 +47,26 @@
(defn assert-delete
"Assert a deletion for an entity in Gallifrey based on the provided key."
- [host actor key]
+ [host actor type key & [time-dimensions]]
(kitclient/request {
- :url (str "https://" host "/entity/" key)
+ :url (str "https://" host "/" type "/" key)
:method :delete
- :query-params {"actor" actor}
+ :query-params (into {"actor" actor} time-dimensions)
:insecure? true
:keepalive 300
:timeout 1000}))
-(defn assert-gallifrey [host actor payload]
+(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]} meta]
- (println operation " entity with key " key)
+ {:keys [key operation time]} meta
+ time-map (filter val time)]
+ (println time-map)
+ (println operation " " type " with key " key)
(interpret-response key (case operation
"CREATE"
- (assert-create host actor key body)
+ (assert-create host actor type key body time-map)
"UPDATE"
- (assert-update host actor key body)
+ (assert-update host actor type key body time-map)
"DELETE"
- (assert-delete host actor key)))))
+ (assert-delete host actor type key time-map)))))