agent.go 6.24 KB
Newer Older
Stefan Dähling's avatar
Stefan Dähling committed
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
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// defines functions necessary for agent execution like message handling,
// context storing and task execution

package agency

import (
51
	"errors"
Stefan Dähling's avatar
Stefan Dähling committed
52
53
54
	"log"
	"sync"

55
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/client"
Stefan Dähling's avatar
Stefan Dähling committed
56
57
58
59
60
61
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/status"
)

// Agent holds information about an agent and implements functionality for agent execution
type Agent struct {
62
63
64
	mutex      *sync.Mutex
	id         int // unique id of agent
	nodeID     int
65
66
67
68
69
70
71
72
73
	name       string              // Name of agent
	aType      string              // Type of agent
	aSubtype   string              // Subtype of agent
	custom     string              // custom data
	customChan chan string         // channel for custom update behavior
	masID      int                 // ID of MAS agent is belongs to
	status     int                 // Status of agent
	ACL        *ACL                // agent communication
	Logger     *client.AgentLogger // logger object
Stefan Dähling's avatar
Stefan Dähling committed
74
	MQTT       *AgentMQTT          // mqtt object
Stefan Dähling's avatar
Stefan Dähling committed
75
	DF         *client.AgentDF
76
77
78
	logError   *log.Logger
	logInfo    *log.Logger
	active     bool
Stefan Dähling's avatar
Stefan Dähling committed
79
80
81
82
}

// newAgent creates a new agent
func newAgent(info schemas.AgentInfo, msgIn chan schemas.ACLMessage,
83
	aclLookup func(int) (*ACL, error), logCol *client.LogCollector, logConfig schemas.LoggerConfig,
Stefan Dähling's avatar
Stefan Dähling committed
84
	mqttCol *mqttCollector, dfActive bool, dfClient *client.DFClient, logErr *log.Logger,
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
85
	logInf *log.Logger) (ag *Agent) {
Stefan Dähling's avatar
Stefan Dähling committed
86
	ag = &Agent{
87
88
89
90
91
92
93
94
95
96
97
98
		id:         info.ID,
		nodeID:     info.Spec.NodeID,
		name:       info.Spec.Name,
		aType:      info.Spec.AType,
		aSubtype:   info.Spec.ASubtype,
		masID:      info.MASID,
		custom:     info.Spec.Custom,
		customChan: nil,
		mutex:      &sync.Mutex{},
		logError:   logErr,
		logInfo:    logInf,
		active:     true,
Stefan Dähling's avatar
Stefan Dähling committed
99
100
	}
	// in, out := ag.ACL.getCommDataChannels()
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
101
	if logCol != nil {
Stefan Dähling's avatar
Stefan Dähling committed
102
		ag.Logger = logCol.NewAgentLogger(ag.id, ag.logError, ag.logInfo)
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
103
	}
104
	ag.ACL = newACL(info.ID, msgIn, aclLookup, ag.Logger, logErr, logInf)
Stefan Dähling's avatar
Stefan Dähling committed
105
106
107
108
109
110
111
	if mqttCol != nil {
		ag.MQTT = mqttCol.newAgentMQTT(ag.id, ag.Logger, ag.logError, ag.logInfo)
	}
	if dfClient != nil {
		ag.DF = client.NewAgentDF(ag.masID, ag.id, ag.nodeID, dfActive, dfClient, ag.logError,
			ag.logInfo)
	}
Stefan Dähling's avatar
Stefan Dähling committed
112
113
114
115
116
117
118
119
120
121
122
123
124
	return
}

// startAgent starts an agent. It requires an agent task to be executed and the ID of the agent
func (agent *Agent) startAgent(task func(*Agent) error) (err error) {
	go task(agent)
	agent.status = status.Running
	agent.logInfo.Println("Started agent ", agent.GetAgentID())
	return
}

// GetAgentID returns the agent ID
func (agent *Agent) GetAgentID() (ret int) {
Stefan Dähling's avatar
Stefan Dähling committed
125
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
126
	ret = agent.id
Stefan Dähling's avatar
Stefan Dähling committed
127
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
128
129
130
131
132
	return
}

// GetAgentType returns the agent type and subtype
func (agent *Agent) GetAgentType() (aType string, aSubtype string) {
Stefan Dähling's avatar
Stefan Dähling committed
133
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
134
135
	aType = agent.aType
	aSubtype = agent.aSubtype
Stefan Dähling's avatar
Stefan Dähling committed
136
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
137
138
139
140
141
	return
}

// GetAgentName returns the agent name
func (agent *Agent) GetAgentName() (ret string) {
Stefan Dähling's avatar
Stefan Dähling committed
142
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
143
	ret = agent.name
Stefan Dähling's avatar
Stefan Dähling committed
144
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
145
146
147
148
	return
}

// GetCustomData returns custom data
149
func (agent *Agent) GetCustomData() (ret string) {
Stefan Dähling's avatar
Stefan Dähling committed
150
	agent.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
151
	ret = agent.custom
Stefan Dähling's avatar
Stefan Dähling committed
152
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
153
154
155
	return
}

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// registerCustomUpdateChannel sets the channel for a custom config update behavior if not already
// set
func (agent *Agent) registerCustomUpdateChannel(custChan chan string) (err error) {
	agent.mutex.Lock()
	if !agent.active {
		agent.mutex.Unlock()
		return errors.New("agent not active")
	}
	if agent.customChan != nil {
		agent.mutex.Unlock()
		return errors.New("custom config update is already handled")
	}
	agent.customChan = custChan
	agent.mutex.Unlock()
	return
}

173
174
175
176
// updateCustomData updates custom data
func (agent *Agent) updateCustomData(custom string) {
	agent.mutex.Lock()
	agent.custom = custom
177
178
179
180
181
182
	if agent.customChan != nil {
		agent.mutex.Unlock()
		agent.customChan <- custom
	} else {
		agent.mutex.Unlock()
	}
Stefan Dähling's avatar
Stefan Dähling committed
183
	agent.logInfo.Println("Updated config of agent ", agent.GetAgentID())
184
185
}

186
187
188
189
190
191
192
193
// deregisterCustomUpdateChannel deletes the channel for a custom config update behavior
func (agent *Agent) deregisterCustomUpdateChannel() (err error) {
	agent.mutex.Lock()
	agent.customChan = nil
	agent.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
194
195
196
// Terminate terminates the agent
func (agent *Agent) Terminate() {
	agent.logInfo.Println("Terminating agent ", agent.GetAgentID())
Stefan Dähling's avatar
Stefan Dähling committed
197
198
199
	agent.mutex.Lock()
	agent.active = false
	agent.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
200
	agent.ACL.close()
201
	agent.Logger.Close()
202
	agent.MQTT.close()
Stefan Dähling's avatar
Stefan Dähling committed
203
	agent.DF.Close()
Stefan Dähling's avatar
Stefan Dähling committed
204
}