-
Notifications
You must be signed in to change notification settings - Fork 20
/
requirement-assignment.go
204 lines (168 loc) · 7.05 KB
/
requirement-assignment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package tosca_v2_0
import (
"fmt"
"github.com/tliron/kutil/ard"
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/normal"
)
//
// RequirementAssignment
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.8.2
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.8.2
// [TOSCA-Simple-Profile-YAML-v1.1] @ 3.7.2
// [TOSCA-Simple-Profile-YAML-v1.0] @ 3.7.2
//
type RequirementAssignment struct {
*Entity `name:"requirement"`
Name string
TargetCapabilityNameOrTypeName *string `read:"capability"`
TargetNodeTemplateNameOrTypeName *string `read:"node"`
TargetNodeFilter *NodeFilter `read:"node_filter,NodeFilter"`
Relationship *RelationshipAssignment `read:"relationship,RelationshipAssignment"`
Occurrences *RangeEntity `read:"occurrences,RangeEntity"` // introduced in TOSCA 1.3
TargetCapabilityType *CapabilityType `lookup:"capability,?TargetCapabilityNameOrTypeName" json:"-" yaml:"-"`
TargetNodeTemplate *NodeTemplate `lookup:"node,TargetNodeTemplateNameOrTypeName" json:"-" yaml:"-"`
TargetNodeType *NodeType `lookup:"node,TargetNodeTemplateNameOrTypeName" json:"-" yaml:"-"`
}
func NewRequirementAssignment(context *tosca.Context) *RequirementAssignment {
return &RequirementAssignment{
Entity: NewEntity(context),
Name: context.Name,
}
}
// tosca.Reader signature
func ReadRequirementAssignment(context *tosca.Context) tosca.EntityPtr {
self := NewRequirementAssignment(context)
if context.Is(ard.TypeMap) {
// Long notation
context.ValidateUnsupportedFields(context.ReadFields(self))
} else if context.ValidateType(ard.TypeMap, ard.TypeString) {
// Short notation
self.TargetNodeTemplateNameOrTypeName = context.FieldChild("node", context.Data).ReadString()
}
return self
}
func NewDefaultRequirementAssignment(index int, definition *RequirementDefinition, context *tosca.Context) *RequirementAssignment {
context = context.SequencedListChild(index, definition.Name, nil)
context.Name = definition.Name
self := NewRequirementAssignment(context)
self.TargetNodeTemplateNameOrTypeName = definition.TargetNodeTypeName
self.TargetNodeType = definition.TargetNodeType
self.TargetCapabilityNameOrTypeName = definition.TargetCapabilityTypeName
self.TargetCapabilityType = definition.TargetCapabilityType
return self
}
func (self *RequirementAssignment) GetDefinition(nodeTemplate *NodeTemplate) (*RequirementDefinition, bool) {
if nodeTemplate.NodeType == nil {
return nil, false
}
definition, ok := nodeTemplate.NodeType.RequirementDefinitions[self.Name]
return definition, ok
}
func (self *RequirementAssignment) Normalize(nodeTemplate *NodeTemplate, normalNodeTemplate *normal.NodeTemplate) *normal.Requirement {
normalRequirement := normalNodeTemplate.NewRequirement(self.Name, normal.NewLocationForContext(self.Context))
if self.TargetCapabilityType != nil {
name := tosca.GetCanonicalName(self.TargetCapabilityType)
normalRequirement.CapabilityTypeName = &name
} else if self.TargetCapabilityNameOrTypeName != nil {
normalRequirement.CapabilityName = self.TargetCapabilityNameOrTypeName
}
if self.TargetNodeTemplate != nil {
normalRequirement.NodeTemplate, _ = normalNodeTemplate.ServiceTemplate.NodeTemplates[self.TargetNodeTemplate.Name]
}
if self.TargetNodeType != nil {
name := tosca.GetCanonicalName(self.TargetNodeType)
normalRequirement.NodeTypeName = &name
}
if nodeTemplate.RequirementTargetsNodeFilter != nil {
nodeTemplate.RequirementTargetsNodeFilter.Normalize(normalRequirement)
}
if self.TargetNodeFilter != nil {
self.TargetNodeFilter.Normalize(normalRequirement)
}
if self.Relationship != nil {
if definition, ok := self.GetDefinition(nodeTemplate); ok {
self.Relationship.Normalize(definition.RelationshipDefinition, normalRequirement.NewRelationship())
} else {
self.Relationship.Normalize(nil, normalRequirement.NewRelationship())
}
}
return normalRequirement
}
//
// RequirementAssignments
//
type RequirementAssignments []*RequirementAssignment
func (self *RequirementAssignments) Render(definitions RequirementDefinitions, context *tosca.Context) {
// TODO: currently have no idea what to do with "occurrences" keyword in the requirement
// assignment, because we interpret "occurrences" in the definition to mean how many times
// it would be assigned
for key, definition := range definitions {
if definition.Occurrences == nil {
// The TOSCA spec says that occurrences has an "implied default of [1,1]"
// Our interpretation is that we should automatically add a single assignment if none was specified
found := false
for _, assignment := range *self {
if assignment.Name == key {
found = true
break
}
}
if !found {
*self = append(*self, NewDefaultRequirementAssignment(len(*self), definition, context))
}
} else {
// Check occurrences
var count uint64
for _, assignment := range *self {
if assignment.Name == key {
count++
}
}
if !definition.Occurrences.Range.InRange(count) {
context.ReportNotInRange(fmt.Sprintf("number of requirement %q assignments", definition.Name), count, definition.Occurrences.Range.Lower, definition.Occurrences.Range.Upper)
}
}
}
for _, assignment := range *self {
if definition, ok := definitions[assignment.Name]; ok {
if assignment.TargetCapabilityNameOrTypeName == nil {
assignment.TargetCapabilityNameOrTypeName = definition.TargetCapabilityTypeName
}
if assignment.TargetCapabilityType == nil {
assignment.TargetCapabilityType = definition.TargetCapabilityType
}
if assignment.TargetNodeTemplateNameOrTypeName == nil {
assignment.TargetNodeTemplateNameOrTypeName = definition.TargetNodeTypeName
}
if assignment.TargetNodeType == nil {
assignment.TargetNodeType = definition.TargetNodeType
}
if definition.RelationshipDefinition != nil {
if assignment.Relationship == nil {
assignment.Relationship = definition.RelationshipDefinition.NewDefaultAssignment(assignment.Context.FieldChild("relationship", nil))
}
if assignment.Relationship.RelationshipTemplateNameOrTypeName == nil {
// Note: the definition can only specify a relationship type, not a relationship template
assignment.Relationship.RelationshipTemplateNameOrTypeName = definition.RelationshipDefinition.RelationshipTypeName
}
if (assignment.Relationship.RelationshipType == nil) && (assignment.Relationship.RelationshipTemplate == nil) {
// Note: we are careful not set the relationship type if the assignment uses a relationship template
assignment.Relationship.RelationshipType = definition.RelationshipDefinition.RelationshipType
}
}
if assignment.Relationship != nil {
assignment.Relationship.Render(definition.RelationshipDefinition)
}
} else {
assignment.Context.ReportUndeclared("requirement")
}
}
}
func (self RequirementAssignments) Normalize(nodeTemplate *NodeTemplate, normalNodeTemplate *normal.NodeTemplate) {
for _, requirement := range self {
requirement.Normalize(nodeTemplate, normalNodeTemplate)
}
}