-
Notifications
You must be signed in to change notification settings - Fork 20
/
notification-assignment.go
151 lines (122 loc) · 4.51 KB
/
notification-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
package tosca_v2_0
import (
"github.com/tliron/kutil/ard"
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/normal"
)
//
// NotificationAssignment
//
// [TOSCA-v2.0] @ ?
// [TOSCA-Simple-Profile-YAML-v1.3] @ 3.6.19
//
type NotificationAssignment struct {
*Entity `name:"notification"`
Name string
Description *string `read:"description"`
Implementation *InterfaceImplementation `read:"implementation,InterfaceImplementation"`
Outputs OutputMappings `read:"outputs,OutputMapping"`
}
func NewNotificationAssignment(context *tosca.Context) *NotificationAssignment {
return &NotificationAssignment{
Entity: NewEntity(context),
Name: context.Name,
Outputs: make(OutputMappings),
}
}
// tosca.Reader signature
func ReadNotificationAssignment(context *tosca.Context) tosca.EntityPtr {
self := NewNotificationAssignment(context)
if context.Is(ard.TypeMap) {
// Long notation
context.ValidateUnsupportedFields(context.ReadFields(self))
} else if context.ValidateType(ard.TypeMap, ard.TypeString) {
// Short notation
self.Implementation = ReadInterfaceImplementation(context.FieldChild("implementation", context.Data)).(*InterfaceImplementation)
}
return self
}
// tosca.Mappable interface
func (self *NotificationAssignment) GetKey() string {
return self.Name
}
func (self *NotificationAssignment) Normalize(normalInterface *normal.Interface) *normal.Notification {
logNormalize.Debugf("notification: %s", self.Name)
normalNotification := normalInterface.NewNotification(self.Name)
if self.Description != nil {
normalNotification.Description = *self.Description
}
if self.Implementation != nil {
self.Implementation.NormalizeNotification(normalNotification)
}
if normalInterface.NodeTemplate != nil {
self.Outputs.NormalizeForNodeTemplate(normalInterface.NodeTemplate.ServiceTemplate, normalNotification.Outputs)
} else if normalInterface.Relationship != nil {
self.Outputs.NormalizeForRelationship(normalInterface.Relationship, normalNotification.Outputs)
} else if normalInterface.Group != nil {
self.Outputs.NormalizeForGroup(normalInterface.Group.ServiceTemplate, normalNotification.Outputs)
}
return normalNotification
}
//
// NotificationAssignments
//
type NotificationAssignments map[string]*NotificationAssignment
func (self NotificationAssignments) CopyUnassigned(assignments NotificationAssignments) {
for key, assignment := range assignments {
if selfAssignment, ok := self[key]; ok {
selfAssignment.Outputs.CopyUnassigned(assignment.Outputs)
} else {
self[key] = assignment
}
}
}
func (self NotificationAssignments) RenderForNodeTemplate(nodeTemplate *NodeTemplate, definitions NotificationDefinitions, context *tosca.Context) {
self.render(definitions, context)
for _, assignment := range self {
assignment.Outputs.RenderForNodeTemplate(nodeTemplate)
}
}
func (self NotificationAssignments) RenderForRelationship(relationship *RelationshipAssignment, definitions NotificationDefinitions, context *tosca.Context) {
self.render(definitions, context)
for _, assignment := range self {
assignment.Outputs.RenderForRelationship(relationship)
}
}
func (self NotificationAssignments) RenderForGroup(definitions NotificationDefinitions, context *tosca.Context) {
self.render(definitions, context)
for _, assignment := range self {
assignment.Outputs.RenderForGroup()
}
}
func (self NotificationAssignments) render(definitions NotificationDefinitions, context *tosca.Context) {
for key, definition := range definitions {
assignment, ok := self[key]
if !ok {
assignment = NewNotificationAssignment(context.FieldChild(key, nil))
self[key] = assignment
}
if assignment.Description == nil {
assignment.Description = definition.Description
}
if (assignment.Implementation == nil) && (definition.Implementation != nil) {
// If the definition has an implementation then we must have one, too
assignment.Implementation = NewInterfaceImplementation(assignment.Context.FieldChild("implementation", nil))
}
if assignment.Implementation != nil {
assignment.Implementation.Render(definition.Implementation)
}
assignment.Outputs.Inherit(definition.Outputs)
}
for key, assignment := range self {
if _, ok := definitions[key]; !ok {
assignment.Context.ReportUndeclared("notification")
delete(self, key)
}
}
}
func (self NotificationAssignments) Normalize(normalInterface *normal.Interface) {
for key, notification := range self {
normalInterface.Notifications[key] = notification.Normalize(normalInterface)
}
}