1 ; ============LICENSE_START=======================================================
3 ; ================================================================================
4 ; Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 ; ================================================================================
6 ; Licensed under the Apache License, Version 2.0 (the "License");
7 ; you may not use this file except in compliance with the License.
8 ; You may obtain a copy of the License at
10 ; http://www.apache.org/licenses/LICENSE-2.0
12 ; Unless required by applicable law or agreed to in writing, software
13 ; distributed under the License is distributed on an "AS IS" BASIS,
14 ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ; See the License for the specific language governing permissions and
16 ; limitations under the License.
17 ; ============LICENSE_END=========================================================
19 ; ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 (:require [clojure.java.io :refer :all]
23 [taoensso.timbre :as timbre :refer [info error]]
24 [sch.asdc-client :refer [construct-service-path]]
25 [cheshire.core :refer [parse-string]]
27 (:import (org.openecomp.sdc.utils ArtifactTypeEnum))
31 ; Abstraction to parse the ASDC distribution notification of change events and
32 ; transforms them into DCAE service type requests
34 (defn get-dcae-artifact-types
35 "Returns lazy-seq of string representations of ArtifactTypeEnums that DCAE-related"
37 (letfn [(dcae-artifact-type? [artifact-type]
38 (boolean (re-find #"DCAE_" artifact-type)))]
39 (filter dcae-artifact-type?
40 (map #(.name %) (seq (. ArtifactTypeEnum values))))
44 "Checks to see if the artifact is a DCAE artifact"
46 (let [supported-dcae-artifact-types (get-dcae-artifact-types)]
47 (true? (some #(= (:artifactType artifact) %) supported-dcae-artifact-types))
51 (defn dcae-artifact-inventory-blueprint?
52 "Check to see if the artifact is an inventory blueprint"
54 (= (:artifactType artifact) "DCAE_INVENTORY_BLUEPRINT"))
57 (defn get-service-locations
58 "Gets service locations for a given blueprint
60 The service location information is attached as a separate artifact. This function
61 is responsible for finding the matching locations JSON artifact that is of the form:
63 { \"artifactName\": <artifact name of the blueprint artifact>,
64 \"locations\": <list of location strings> }"
65 [get-artifact-func resource-metadata artifact-name]
66 (let [target-artifacts (filter #(= (:artifactType %) "DCAE_INVENTORY_JSON")
67 (:artifacts resource-metadata))
68 inventory-jsons (map #(parse-string (get-artifact-func (:artifactURL %)) true)
70 location-jsons (filter #(and
71 (= (:artifactName %) artifact-name)
72 (contains? % :locations)) inventory-jsons)]
73 (flatten (map :locations location-jsons))
76 (defn generate-dcae-service-type-requests
77 "Generates DCAE service type requests from ASDC change event
79 The ASDC change event is a nested structure. The single arity of this method
80 handles at the service level of the event. The two arity of this method handles
81 at the resource level of the event.
83 `get-blueprint-func` is function that takes the `artifactURL` and retrieves
84 the DCAE blueprint artifact.
86 Returns a list of DCAE service type requests"
87 ([get-blueprint-func get-locations-func service-change-event]
88 (let [; TODO: Where do I get this from?
90 service-id (:invariantUUID service-change-event)
91 service-part { :asdcServiceId service-id
92 :asdcServiceURL (construct-service-path service-id)
93 :owner (:lastUpdaterFullName service-change-event) }]
95 ; Given the resource part, create dcae service type requests
96 (letfn [(generate-for-resource
97 [resource-change-event]
98 (let [dcae-artifacts (filter dcae-artifact-inventory-blueprint?
99 (:artifacts resource-change-event))
100 resource-part { :asdcResourceId
101 (:resourceInvariantUUID resource-change-event) }]
103 (map #(-> service-part
104 (merge resource-part)
105 ; WATCH! Using artifactName over artifactUUID because artifactUUID
106 ; is variant between versions. ASDC folks should be adding invariant
108 (assoc :typeName (:artifactName %)
109 :typeVersion (Integer. (:artifactVersion %))
110 :blueprintTemplate (get-blueprint-func (:artifactURL %))
111 :serviceLocations (get-locations-func resource-change-event
119 (flatten (map #(generate-for-resource %) (:resources service-change-event)))
123 (defn pick-out-artifact
124 "Given dcae service type, fetch complementary asdc artifact"
125 [service-metadata request]
126 (let [target-resource (:asdcResourceId request)
127 resource-metadata (first (filter #(= (:resourceInvariantUUID %) target-resource)
128 (:resources service-metadata)))
129 target-artifact (:typeName request)
130 artifact-metadata (filter #(= (:artifactName %) target-artifact)
131 (:artifacts resource-metadata))
133 (first artifact-metadata)))