aboutsummaryrefslogtreecommitdiffstats
path: root/src/chameleon/handler.clj
diff options
context:
space:
mode:
authorAdrian Batos-Parac <adrian.batos-parac@amdocs.com>2018-02-22 14:43:42 -0500
committerAdrian Batos-Parac <adrian.batos-parac@amdocs.com>2018-02-22 14:46:57 -0500
commite023325d3e76a71ac795ebbdb74f5a89756040a7 (patch)
tree63b2816a829c785f3c0329c3ea57ebd3c4f2937d /src/chameleon/handler.clj
parent2610df2fc29a9d16bb869f652d4d21caeae4c154 (diff)
Initial Commit of Chameleon
Commit the initial set of code for the Chameleon offering to ONAP Change-Id: Ia58bd49eafc0a3702c17c9cab34d666ed1627ba5 Issue-ID: AAI-797 Signed-off-by: Adrian Batos-Parac <adrian.batos-parac@amdocs.com>
Diffstat (limited to 'src/chameleon/handler.clj')
-rw-r--r--src/chameleon/handler.clj61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/chameleon/handler.clj b/src/chameleon/handler.clj
new file mode 100644
index 0000000..7a4a5fe
--- /dev/null
+++ b/src/chameleon/handler.clj
@@ -0,0 +1,61 @@
+(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]]
+ [compojure.route :refer [resources]]
+ [ring.util.response :refer [resource-response content-type]]
+ [ring.middleware.defaults :refer [wrap-defaults api-defaults]]
+ [ring.middleware.anti-forgery :refer [wrap-anti-forgery]]
+ [ring.middleware.session :refer [wrap-session]]
+ [cheshire.core :as json]
+ [clj-time.format :as tf]
+ [integrant.core :as ig]))
+
+(declare handler)
+
+(defonce ^:private g-host (atom nil))
+
+(defmethod ig/init-key :chameleon/handler [_ {:keys [gallifrey-host]}]
+ (reset! g-host gallifrey-host)
+ handler)
+
+(defmethod ig/halt-key! :chameleon/handler [_ _]
+ (reset! g-host nil))
+
+(declare serialize de-serialize)
+
+(defresource entity-endpoint [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)))]
+ (when (= (:status resource) 200)
+ {::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)]
+ (= status 410)))
+ :handle-ok ::resource)
+
+(defroutes app-routes
+ (GET "/entity/:id" [id] (entity-endpoint id))
+ (resources "/"))
+
+(def handler
+ (-> app-routes
+ (wrap-defaults api-defaults)))
+
+
+;;; Implementation
+
+(defn- serialize
+ [e]
+ (compact
+ (update e :_meta #(map-vals
+ (fn [m]
+ (map-vals str m)) %))))
+
+(defn- de-serialize
+ [e]
+ e)