Upgrade dependencies to address CLM scan
[dcaegen2/platform/servicechange-handler.git] / src / sch / parse.clj
1 ; ============LICENSE_START=======================================================
2 ; org.onap.dcae
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
9 ;
10 ;      http://www.apache.org/licenses/LICENSE-2.0
11 ;
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=========================================================
18 ;
19 ; ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 (ns sch.parse
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]]
26             )
27   (:import (org.openecomp.sdc.utils ArtifactTypeEnum))
28   (:gen-class))
29
30
31 ; Abstraction to parse the ASDC distribution notification of change events and 
32 ; transforms them into DCAE service type requests
33
34 (defn get-dcae-artifact-types
35   "Returns lazy-seq of string representations of ArtifactTypeEnums that DCAE-related"
36   []
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))))
41     ))
42
43 (defn dcae-artifact?
44   "Checks to see if the artifact is a DCAE artifact"
45   [artifact]
46   (let [supported-dcae-artifact-types (get-dcae-artifact-types)]
47     (true? (some #(= (:artifactType artifact) %) supported-dcae-artifact-types))
48     ))
49
50
51 (defn dcae-artifact-inventory-blueprint?
52   "Check to see if the artifact is an inventory blueprint"
53   [artifact]
54   (= (:artifactType artifact) "DCAE_INVENTORY_BLUEPRINT"))
55
56
57 (defn get-service-locations
58   "Gets service locations for a given blueprint
59
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:
62
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)
69                              target-artifacts)
70         location-jsons (filter #(and
71                                   (= (:artifactName %) artifact-name)
72                                   (contains? % :locations)) inventory-jsons)]
73     (flatten (map :locations location-jsons))
74     ))
75
76 (defn generate-dcae-service-type-requests
77   "Generates DCAE service type requests from ASDC change event
78
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.
82
83   `get-blueprint-func` is function that takes the `artifactURL` and retrieves
84   the DCAE blueprint artifact.
85
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?
89          service-location nil
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) }]
94
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) }]
102
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
107                            ; UUID.
108                            (assoc :typeName (:artifactName %)
109                                   :typeVersion (Integer. (:artifactVersion %))
110                                   :blueprintTemplate (get-blueprint-func (:artifactURL %))
111                                   :serviceLocations (get-locations-func resource-change-event
112                                                                         (:artifactName %))
113                                   
114                                   )
115                            )
116                       dcae-artifacts)
117                  ))]
118
119        (flatten (map #(generate-for-resource %) (:resources service-change-event)))
120        ))))
121
122
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))
132         ]
133     (first artifact-metadata)))