+}
+
+rule {
+ name 'SDNC-AAI-attribute-comparison'
+ category 'Attribute Mismatch'
+ description 'Verify that all attributes in SDN-C are the same as in AAI'
+ errorText 'Error found with attribute "{0}"; SDN-C value does not match AAI value "{1}"'
+ severity 'ERROR'
+ attributes 'sdncItems', 'aaiItems'
+ validate '''
+ Closure<java.util.Map> getAttributes = { parsedData ->
+ java.util.Map attributeMap = new java.util.HashMap()
+
+ def isAttributeDataQualityOk = { attribute ->
+ attribute.findResult{ k, v -> if(k.equals("dataQuality") ) {return v.get("status")}}.equals("ok")
+ }
+
+ def addToMap = { attrKey, attrValue ->
+ java.util.Set values = attributeMap.get("$attrKey")
+ if(values == null) {
+ values = new java.util.HashSet()
+ attributeMap.put("$attrKey", values)
+ }
+ values.add("$attrValue")
+ }
+
+ def addAttributeToMap = { attribute ->
+ if(isAttributeDataQualityOk(attribute)) {
+ String key, value
+ attribute.each { k, v ->
+ if(k.equals("name")) {key = "$v"}
+ if(k.equals("value")) {value = "$v"}
+ }
+ addToMap("$key", "$value")
+ }
+ }
+
+ def processKeyValue = { key, value ->
+ if(value instanceof java.util.ArrayList) {
+ if(key.equals("attributeList")) {
+ value.each {
+ addAttributeToMap(it)
+ }
+ }
+ } else if(!(value instanceof groovy.json.internal.LazyMap)) {
+ // only add key-value attributes, skip the rest
+ addToMap("$key", "$value")
+ }
+ }
+
+ if(parsedData instanceof java.util.ArrayList) {
+ parsedData.each {
+ it.each { key, value -> processKeyValue(key, value) }
+ }
+ } else {
+ parsedData.each { key, value -> processKeyValue(key, value) }
+ }
+ return attributeMap
+ }
+
+ def slurper = new groovy.json.JsonSlurper()
+ java.util.Map sdnc = getAttributes(slurper.parseText(sdncItems.toString()))
+ java.util.Map aai = getAttributes(slurper.parseText(aaiItems.toString()))
+
+ boolean result = true
+ List<String> details = new ArrayList<>();
+ sdnc.any{ sdncKey, sdncValueList ->
+ def aaiValueList = aai.get("$sdncKey")
+ aaiValueList.each{ aaiValue ->
+ if(!sdncValueList.any{ it == "$aaiValue" }) {
+ result = false
+ details.add("$sdncKey")
+ details.add("$aaiValue")
+ }
+ }
+ if(result == false) {
+ // break out of 'any' loop
+ return true
+ }
+ }
+ return new Tuple2(result, details)
+ '''
+}
+
+
+rule {
+ name 'SDNC-NDCB-attribute-comparison'
+ category 'Attribute Mismatch'
+ description 'Verify that all attributes in SDN-C are the same as in Network Discovery'
+ errorText 'Error found with attribute "{0}"; SDN-C value does not match Network-Discovery value "{1}"'
+ severity 'ERROR'
+ attributes 'sdncItems', 'ndcbItems'
+ validate '''
+ Closure<java.util.Map> getAttributes = { parsedData ->
+ java.util.Map attributeMap = new java.util.HashMap()
+
+ def isAttributeDataQualityOk = { attribute ->
+ attribute.findResult{ k, v -> if(k.equals("dataQuality") ) {return v.get("status")}}.equals("ok")
+ }
+
+ def addToMap = { attrKey, attrValue ->
+ java.util.Set values = attributeMap.get("$attrKey")
+ if(values == null) {
+ values = new java.util.HashSet()
+ attributeMap.put("$attrKey", values)
+ }
+ values.add("$attrValue")
+ }
+
+ def addAttributeToMap = { attribute ->
+ if(isAttributeDataQualityOk(attribute)) {
+ String key, value
+ attribute.each { k, v ->
+ if(k.equals("name")) {key = "$v"}
+ if(k.equals("value")) {value = "$v"}
+ }
+ addToMap("$key", "$value")
+ }
+ }
+
+ def processKeyValue = { key, value ->
+ if(value instanceof java.util.ArrayList) {
+ if(key.equals("attributeList")) {
+ value.each {
+ addAttributeToMap(it)
+ }
+ }
+ } else if(!(value instanceof groovy.json.internal.LazyMap)) {
+ // only add key-value attributes, skip the rest
+ addToMap("$key", "$value")
+ }
+ }
+
+ if(parsedData instanceof java.util.ArrayList) {
+ parsedData.each {
+ it.each { key, value -> processKeyValue(key, value) }
+ }
+ } else {
+ parsedData.each { key, value -> processKeyValue(key, value) }
+ }
+ return attributeMap
+ }
+
+ def slurper = new groovy.json.JsonSlurper()
+ java.util.Map sdnc = getAttributes(slurper.parseText(sdncItems.toString()))
+ java.util.Map ndcb = getAttributes(slurper.parseText(ndcbItems.toString()))
+
+ boolean result = true
+ List<String> details = new ArrayList<>();
+ sdnc.any{ sdncKey, sdncValueList ->
+ def ndcbValueList = ndcb.get("$sdncKey")
+ ndcbValueList.each{ ndcbValue ->
+ if(!sdncValueList.any{ it == "$ndcbValue" }) {
+ result = false
+ details.add("$sdncKey")
+ details.add("$ndcbValue")
+ }
+ }
+ if(result == false) {
+ // break out of 'any' loop
+ return true
+ }
+ }
+ return new Tuple2(result, details)
+ '''
+}
+
+/*
+ * The data-dictionary rule below can be used with this useRule clause:
+ * useRule {
+ * name 'Data-Dictionary validate VF type'
+ * attributes 'context-list.ndcb.vnfList[*].vfModuleList[*].networkList[*].type'
+ * }
+ */
+rule {
+ name 'Data-Dictionary validate VF type'
+ category 'INVALID_VALUE'
+ description 'Validate all VF type values against data-dictionary'
+ errorText 'VF type [{0}] failed data-dictionary validation: {1}'
+ severity 'ERROR'
+ attributes 'typeList'
+ validate '''
+ boolean success = true
+ List<String> details = new ArrayList<>()
+ typeList.any {
+ if(!success) {
+ // break out of 'any' loop
+ return false
+ }
+ def result = org.onap.aai.validation.ruledriven.rule.builtin.DataDictionary.validate("instance", "vfModuleNetworkType", "type", "$it")
+ if(!result.isEmpty()) {
+ success = false
+ details.add("$it")
+ details.add("$result")
+ }
+ }
+ return new Tuple2(success, details)
+ '''
+}