-
Notifications
You must be signed in to change notification settings - Fork 20
/
substitution-mappings.go
201 lines (174 loc) · 7.7 KB
/
substitution-mappings.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
package tosca_v2_0
import (
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/normal"
)
//
// SubstitutionMappings
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.8.13, 2.10, 2.11, 2.12
// [TOSCA-Simple-Profile-YAML-v1.2] @ 3.8.12, 2.10, 2.11
// [TOSCA-Simple-Profile-YAML-v1.1] @ 2.10, 2.11
// [TOSCA-Simple-Profile-YAML-v1.0] @ 2.10, 2.11
//
type SubstitutionMappings struct {
*Entity `name:"substitution mappings"`
NodeTypeName *string `read:"node_type" require:""`
CapabilityMappings CapabilityMappings `read:"capabilities,CapabilityMapping"`
RequirementMappings RequirementMappings `read:"requirements,RequirementMapping"`
PropertyMappings PropertyMappings `read:"properties,PropertyMapping"` // introduced in TOSCA 1.2
AttributeMappings AttributeMappings `read:"attributes,AttributeMapping"` // introduced in TOSCA 1.3
InterfaceMappings InterfaceMappings `read:"interfaces,InterfaceMapping"` // introduced in TOSCA 1.2
SubstitutionFilter NodeFilter `read:"substitution_filter,NodeFilter"` // introduced in TOSCA 1.3
NodeType *NodeType `lookup:"node_type,NodeTypeName" json:"-" yaml:"-"`
}
func NewSubstitutionMappings(context *tosca.Context) *SubstitutionMappings {
return &SubstitutionMappings{
Entity: NewEntity(context),
CapabilityMappings: make(CapabilityMappings),
RequirementMappings: make(RequirementMappings),
PropertyMappings: make(PropertyMappings),
AttributeMappings: make(AttributeMappings),
InterfaceMappings: make(InterfaceMappings),
}
}
// tosca.Reader signature
func ReadSubstitutionMappings(context *tosca.Context) tosca.EntityPtr {
if context.HasQuirk(tosca.QuirkSubstitutionMappingsRequirementsList) {
context.SetReadTag("RequirementMappings", "requirements,{}RequirementMapping")
}
self := NewSubstitutionMappings(context)
context.ValidateUnsupportedFields(context.ReadFields(self))
return self
}
func (self *SubstitutionMappings) IsRequirementMapped(nodeTemplate *NodeTemplate, requirementName string) bool {
for _, mapping := range self.RequirementMappings {
if mapping.NodeTemplate == nodeTemplate {
if (mapping.RequirementName != nil) && (*mapping.RequirementName == requirementName) {
return true
}
}
}
return false
}
func (self *SubstitutionMappings) Render(inputDefinitions ParameterDefinitions) {
logRender.Debug("substitution mappings")
if self.NodeType == nil {
return
}
for name, mapping := range self.CapabilityMappings {
if definition, ok := self.NodeType.CapabilityDefinitions[name]; ok {
if mappedDefinition, ok := mapping.GetCapabilityDefinition(); ok {
if (definition.CapabilityType != nil) && (mappedDefinition.CapabilityType != nil) {
if !self.Context.Hierarchy.IsCompatible(definition.CapabilityType, mappedDefinition.CapabilityType) {
self.Context.ReportIncompatibleType(definition.CapabilityType, mappedDefinition.CapabilityType)
}
}
}
} else {
mapping.Context.Clone(name).ReportReferenceNotFound("capability", self.NodeType)
}
}
for name, mapping := range self.RequirementMappings {
if _, ok := self.NodeType.RequirementDefinitions[name]; !ok {
mapping.Context.Clone(name).ReportReferenceNotFound("requirement", self.NodeType)
}
}
self.PropertyMappings.Render(inputDefinitions)
for name, mapping := range self.PropertyMappings {
if definition, ok := self.NodeType.PropertyDefinitions[name]; ok {
if mapping.InputDefinition != nil {
// Input mapping
if (definition.DataType != nil) && (mapping.InputDefinition.DataType != nil) {
if !self.Context.Hierarchy.IsCompatible(definition.DataType, mapping.InputDefinition.DataType) {
self.Context.ReportIncompatibleType(definition.DataType, mapping.InputDefinition.DataType)
}
}
} else if mapping.Property != nil {
// Property mapping (deprecated in TOSCA 1.3)
if (definition.DataType != nil) && (mapping.Property.DataType != nil) {
if !self.Context.Hierarchy.IsCompatible(definition.DataType, mapping.Property.DataType) {
self.Context.ReportIncompatibleType(definition.DataType, mapping.Property.DataType)
}
}
}
} else {
mapping.Context.Clone(name).ReportReferenceNotFound("property", self.NodeType)
}
}
self.AttributeMappings.EnsureRender()
for name, mapping := range self.AttributeMappings {
if definition, ok := self.NodeType.AttributeDefinitions[name]; ok {
if (definition.DataType != nil) && (mapping.Attribute != nil) && (mapping.Attribute.DataType != nil) {
if !self.Context.Hierarchy.IsCompatible(definition.DataType, mapping.Attribute.DataType) {
self.Context.ReportIncompatibleType(definition.DataType, mapping.Attribute.DataType)
}
}
} else {
mapping.Context.Clone(name).ReportReferenceNotFound("attribute", self.NodeType)
}
}
for name, mapping := range self.InterfaceMappings {
if definition, ok := self.NodeType.InterfaceDefinitions[name]; ok {
if mappedDefinition, ok := mapping.GetInterfaceDefinition(); ok {
if (definition.InterfaceType != nil) && (mappedDefinition.InterfaceType != nil) {
if !self.Context.Hierarchy.IsCompatible(definition.InterfaceType, mappedDefinition.InterfaceType) {
self.Context.ReportIncompatibleType(definition.InterfaceType, mappedDefinition.InterfaceType)
}
}
}
} else {
mapping.Context.Clone(name).ReportReferenceNotFound("interface", self.NodeType)
}
}
}
func (self *SubstitutionMappings) Normalize(normalServiceTemplate *normal.ServiceTemplate) *normal.Substitution {
logNormalize.Debug("substitution mappings")
if self.NodeType == nil {
return nil
}
normalSubstitution := normalServiceTemplate.NewSubstitution()
normalSubstitution.Type = tosca.GetCanonicalName(self.NodeType)
if metadata, ok := self.NodeType.GetMetadata(); ok {
normalSubstitution.TypeMetadata = metadata
}
for _, mapping := range self.CapabilityMappings {
if (mapping.NodeTemplate != nil) && (mapping.CapabilityName != nil) {
if normalNodeTemplate, ok := normalServiceTemplate.NodeTemplates[mapping.NodeTemplate.Name]; ok {
normalSubstitution.CapabilityMappings[mapping.Name] = normalNodeTemplate.NewMapping("capability", *mapping.CapabilityName)
}
}
}
for _, mapping := range self.RequirementMappings {
if (mapping.NodeTemplate != nil) && (mapping.RequirementName != nil) {
if normalNodeTemplate, ok := normalServiceTemplate.NodeTemplates[mapping.NodeTemplate.Name]; ok {
normalSubstitution.RequirementMappings[mapping.Name] = normalNodeTemplate.NewMapping("requirement", *mapping.RequirementName)
}
}
}
for _, mapping := range self.PropertyMappings {
if (mapping.NodeTemplate != nil) && (mapping.PropertyName != nil) {
if normalNodeTemplate, ok := normalServiceTemplate.NodeTemplates[mapping.NodeTemplate.Name]; ok {
normalSubstitution.PropertyMappings[mapping.Name] = normalNodeTemplate.NewMapping("property", *mapping.PropertyName)
}
} else if mapping.InputName != nil {
normalSubstitution.PropertyMappings[mapping.Name] = normal.NewMapping("input", *mapping.InputName)
}
}
for _, mapping := range self.AttributeMappings {
if (mapping.NodeTemplate != nil) && (mapping.AttributeName != nil) {
if normalNodeTemplate, ok := normalServiceTemplate.NodeTemplates[mapping.NodeTemplate.Name]; ok {
normalSubstitution.AttributeMappings[mapping.Name] = normalNodeTemplate.NewMapping("attribute", *mapping.AttributeName)
}
}
}
for _, mapping := range self.InterfaceMappings {
if (mapping.NodeTemplate != nil) && (mapping.InterfaceName != nil) {
if normalNodeTemplate, ok := normalServiceTemplate.NodeTemplates[mapping.NodeTemplate.Name]; ok {
normalSubstitution.InterfaceMappings[mapping.Name] = normalNodeTemplate.NewMapping("interface", *mapping.InterfaceName)
}
}
}
return normalSubstitution
}