Initial submission for validation service
[aai/validation.git] / bundleconfig / etc / rules / aai-event / entity-vserver.groovy
1 /*
2  * ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18
19 entity {
20         type 'vserver'
21         validation {
22                 useRule {name 'vserver is related to 0 or 1 image' }
23                 useRule {name 'vserver is related to 0 or 1 flavor' }
24                 useRule {name 'vserver is related to 1 pserver' }
25                 useRule {name 'vserver is related to vpe and vserver-name matches naming convention' }
26                 useRule {name 'vserver is related to vce and vserver-name matches naming convention' }
27                 useRule {
28                         name 'vserver related to TRINITY image and generic-vnf.vnf-name matches naming convention'
29                         attributes 'relationship-list.relationship[*]'
30                 }
31                 useRule {
32                         name  'vserver is related to a TRINITY image and vserver-name matches naming convention'
33                         attributes 'relationship-list.relationship[*]', 'vserver-name'
34                 }
35                 useRule {
36                         name  'vserver is related to a vnf (vce or newvce or vpe or generic-vnf)'
37                         attributes 'relationship-list.relationship[*].related-to'
38                 }
39         }
40 }
41
42 rule {
43         name        'vserver related to TRINITY image and generic-vnf.vnf-name matches naming convention'
44         category    'INVALID_NAME'
45         description 'Validates that if vserver is related to an image named TRINITY, then the related generic-vnf name matches naming convention'
46         errorText   'Invalid name - if vserver is related to an image named TRINITY, then the related generic-vnf name must match xxxxnnnnv (where x = character and n = number)'
47         severity    'MINOR'
48         attributes  'relationships'
49         validate    '''
50                      def getStringProperty = { jsonObject, propertyName -> jsonObject.get(propertyName)?.getAsString() } 
51
52                                          vnf_name = relationships.findAll    { getStringProperty(it, "related-to") == "generic-vnf" }
53                                              .findAll    { it."related-to-property" != null }
54                                              .collect    { it."related-to-property".get(0) }
55                                                                                  .find       { getStringProperty(it, "property-key") == "generic-vnf.vnf-name" }
56                                                                                      .findResult { getStringProperty(it, "property-value") }
57
58                                          relatedToTrinity = relationships.findAll    { getStringProperty(it, "related-to") == "image" }
59                                                                                                          .findAll    { it."related-to-property" != null }
60                                                                                              .collect    { it."related-to-property".get(0) }
61                                                                                                  .findAll    { getStringProperty(it, "property-key") == "image.image-name" }
62                                                                                              .find       { getStringProperty(it, "property-value")?.startsWith("TRINITY") }
63
64                      // If (and only if) related to TRINITY then check the generic-vnf name
65                      return !relatedToTrinity || vnf_name?.matches("[a-z]{4}[0-9]{4}v")
66                      '''
67 }
68
69 rule {
70         name        'vserver is related to a vnf (vce or newvce or vpe or generic-vnf)'
71         category    'MISSING_REL'
72         description 'Validates that a vserver is related to a vnf (vce or newvce or vpe or generic-vnf)'
73         errorText   'Missing relationship - a vserver must be related to a vnf (vce or newvce or vpe or generic-vnf)'
74         severity    'MINOR'
75         attributes  'related-to'
76         validate    'related-to != null && (related-to.contains("vce") || related-to.contains("newvce") || related-to.contains("vpe") || related-to.contains("generic-vnf"))'
77 }
78
79 rule {
80         name        'vserver is related to 1 pserver'
81         category    'MISSING_REL'
82         description 'Validates that a vserver is related to 1 pserver (and not more than 1 pserver)'
83         errorText   'Missing relationship - vserver must be related to 1 pserver'
84         severity    'MINOR'
85         attributes  'relationship-list.relationship[*].related-to'
86         validate    'related-to != null && related-to.count("pserver") == 1'
87 }
88
89 rule {
90         name        'vserver is related to 0 or 1 image'
91         category    'MISSING_REL'
92         description 'Validates that a vserver is either not related to an image or related to only 1 image'
93         errorText   'Missing relationship - vserver must be related to 0 or 1 image'
94         severity    'MINOR'
95         attributes  'relationship-list.relationship[*].related-to'
96         validate    'related-to == null || related-to.count("image") <= 1'
97 }
98
99 rule {
100         name        'vserver is related to 0 or 1 flavor'
101         category    'MISSING_REL'
102         description 'Validates that a vserver is either not related to a flavor or related to only 1 flavor'
103         errorText   'Missing relationship - vserver must be related to 0 or 1 flavor'
104         severity    'MINOR'
105         attributes  'relationship-list.relationship[*].related-to'
106         validate    'related-to == null || related-to.count("flavor") <= 1'
107 }
108
109 rule {
110         name        'vserver is related to vpe and vserver-name matches naming convention'
111         category    'INVALID_NAME'
112         description 'Validates that if a vserver is related to a vpe then vserver-name must contain me6'
113         errorText   'Invalid name - if vserver is related to vpe then vserver-name must contain me6'
114         severity    'MINOR'
115         attributes  'relationship-list.relationship[*].related-to', 'vserver-name'
116         validate    '!related-to.contains("vpe") || vserver-name =~ "me6"'
117 }
118
119 rule {
120         name        'vserver is related to vce and vserver-name matches naming convention'
121         category    'INVALID_NAME'
122         description 'Validates that if a vserver is related to a vce then vserver-name must match naming convention'
123         errorText   'Invalid name - if vserver is related to vce then vserver-name must match xxxxxxxxvbcnnceb (where x = alphanumeric and n = numeric)'
124         severity    'MINOR'
125         attributes  'relationship-list.relationship[*].related-to', 'vserver-name'
126         validate    '!related-to.contains("vce") || vserver-name =~ "[a-z0-9]{8}vbc[0-9]{2}ceb"'
127 }
128
129
130
131 rule {
132         name          'vserver is related to a TRINITY image and vserver-name matches naming convention'
133         category      'INVALID_NAME'
134         description   'Validates that if vserver is related to an image named TRINITY, then the vserver name matches naming convention'
135         errorText     'Invalid name - if vserver is related to an image named TRINITY, then the vserver name must match xxxxnnnnvmnnn (where x = character and n = number)'
136         severity      'MINOR'
137         attributes    'relationships', 'vservername'
138         validate      '''
139                   def getStringProperty = { jsonObject, propertyName -> jsonObject.get(propertyName)?.getAsString() } 
140
141                                   relatedToTrinity = relationships.findAll    { getStringProperty(it, "related-to") == "image" }
142                                                                                                   .findAll    { it."related-to-property" != null }
143                                                                                                   .collect    { it."related-to-property".get(0) }
144                                                                                           .findAll    { getStringProperty(it, "property-key") == "image.image-name" }
145                                                                                       .find       { getStringProperty(it, "property-value").startsWith("TRINITY") }
146
147                       // If (and only if) related to TRINITY then check the vserver name
148                       return !relatedToTrinity || vservername != null && vservername ==~ "[a-z]{4}[0-9]{4}vm[0-9]{3}"
149                       '''
150 }
151