storage.go 18.4 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
/*
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.
*/

package ams

// gives an interface for interaction with the storage (local or etcd). The Storage
Stefan Dähling's avatar
Stefan Dähling committed
48
// stores the state of the AMS. This is MAS configuration and MAS and agent status
Stefan Dähling's avatar
Stefan Dähling committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

import (
	"errors"
	"strconv"
	"sync"
	"time"

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/status"
)

// storage interface for interaction with storage
type storage interface {
	// getCloneMAPInfo returns stored info about clonemap
	getCloneMAPInfo() (ret schemas.CloneMAP, err error)

	// setCloneMAPInfo sets info specific to running clonemap instance
	setCloneMAPInfo(cloneMAP schemas.CloneMAP) (err error)

Stefan Dähling's avatar
Stefan Dähling committed
68
69
70
	// getMASsShort returns short specs of all MAS
	getMASsShort() (ret []schemas.MASInfoShort, err error)

Stefan Dähling's avatar
Stefan Dähling committed
71
72
73
	// getMASs returns specs of all MAS
	getMASs() (ret schemas.MASs, err error)

Stefan Dähling's avatar
Stefan Dähling committed
74
75
76
	// getMASInfoShort returns info of one MAS
	getMASInfoShort(masID int) (ret schemas.MASInfoShort, err error)

Stefan Dähling's avatar
Stefan Dähling committed
77
78
79
	// getMASInfo returns info of one MAS
	getMASInfo(masID int) (ret schemas.MASInfo, err error)

Stefan Dähling's avatar
Stefan Dähling committed
80
81
82
	// getGroupInfo returns info of one image group
	getGroupInfo(masID int, imID int) (ret schemas.ImageGroupInfo, err error)

Stefan Dähling's avatar
Stefan Dähling committed
83
84
85
86
87
88
89
90
91
92
93
94
	// getAgents returns specs of all agents in MAS
	getAgents(masID int) (ret schemas.Agents, err error)

	// getAgentInfo returns info of one agent
	getAgentInfo(masID int, agentID int) (ret schemas.AgentInfo, err error)

	// getAgentAddress returns address of one agent
	getAgentAddress(masID int, agentID int) (ret schemas.Address, err error)

	// setAgentAddress sets address of agent
	setAgentAddress(masID int, agentID int, address schemas.Address) (err error)

95
96
97
	// setAgentCustom sets custom config of agent
	setAgentCustom(masID int, agentID int, custom string) (err error)

Stefan Dähling's avatar
Stefan Dähling committed
98
99
100
	// getAgencies returns specs of all agencies in MAS
	getAgencies(masID int) (ret schemas.Agencies, err error)

Stefan Dähling's avatar
Stefan Dähling committed
101
	// getAgencyInfoFull returns status of one agency
Stefan Dähling's avatar
Stefan Dähling committed
102
	getAgencyInfoFull(masID int, imID int, agencyID int) (ret schemas.AgencyInfoFull, err error)
103

Stefan Dähling's avatar
Stefan Dähling committed
104
105
106
107
108
109
110
111
112
	// registerMAS registers a new MAS with the storage and returns its ID
	registerMAS() (masID int, err error)

	// storeMAS stores MAS specs
	storeMAS(masID int, masInfo schemas.MASInfo) (err error)

	// deleteMAS deletes MAS with specified ID
	deleteMAS(masID int) (err error)

Stefan Dähling's avatar
Stefan Dähling committed
113
	// registerImageGroup registers a new image group with the storage and returns its ID
Stefan Dähling's avatar
Stefan Dähling committed
114
115
	registerImageGroup(masID int, config schemas.ImageGroupConfig) (newGroup bool, imID int,
		err error)
Stefan Dähling's avatar
Stefan Dähling committed
116

Stefan Dähling's avatar
Stefan Dähling committed
117
	// registerAgent registers a new agent with the storage and returns its ID
Stefan Dähling's avatar
Stefan Dähling committed
118
	registerAgent(masID int, imID int, spec schemas.AgentSpec) (agentID int, err error)
Stefan Dähling's avatar
Stefan Dähling committed
119

Stefan Dähling's avatar
Stefan Dähling committed
120
	// addAgent adds an agent to an existing MAS
Stefan Dähling's avatar
Stefan Dähling committed
121
122
	addAgent(masID int, imID int, agentSpec schemas.AgentSpec) (newAgency bool, agentID int,
		agencyID int, err error)
Stefan Dähling's avatar
Stefan Dähling committed
123
124
125

	// deleteAgent deletes an agent
	deleteAgent(masID int, agentID int) (err error)
Stefan Dähling's avatar
Stefan Dähling committed
126
127
128
129
130
131
}

// CommData helper struct for communication data
type CommData struct {
	ID         int // id of other agent
	NumMsgSent int // number of messages sent to this agent
Stefan Dähling's avatar
Stefan Dähling committed
132
	NumMsgRecv int // number of messages received from this agent
Stefan Dähling's avatar
Stefan Dähling committed
133
134
135
136
137
138
139
140
}

// information storage for local use of clonemap
// note: id should always equal slice index!

// represents local storage
type localStorage struct {
	cloneMAP   schemas.CloneMAP
Stefan Dähling's avatar
Stefan Dähling committed
141
142
	masCounter int               // counter for mas
	mas        []schemas.MASInfo // list of all running MAS
Stefan Dähling's avatar
Stefan Dähling committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	mutex      *sync.Mutex
}

// getCloneMAPInfo returns stored info about clonemap
func (stor *localStorage) getCloneMAPInfo() (ret schemas.CloneMAP, err error) {
	stor.mutex.Lock()
	ret = stor.cloneMAP
	stor.mutex.Unlock()
	return
}

// setCloneMAPInfo sets info specific to running clonemap instance
func (stor *localStorage) setCloneMAPInfo(cloneMAP schemas.CloneMAP) (err error) {
	stor.mutex.Lock()
	stor.cloneMAP = cloneMAP
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// getMASsShort returns specs of all MAS
func (stor *localStorage) getMASsShort() (ret []schemas.MASInfoShort, err error) {
	stor.mutex.Lock()
	for i := 0; i < len(stor.mas); i++ {
		var masshort schemas.MASInfoShort
		masshort.ID = stor.mas[i].ID
		masshort.Config = stor.mas[i].Config
		masshort.NumAgents = stor.mas[i].Agents.Counter
		masshort.Uptime = stor.mas[i].Uptime
		masshort.Status = stor.mas[i].Status
		ret = append(ret, masshort)
	}
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
178
179
180
// getMASs returns specs of all MAS
func (stor *localStorage) getMASs() (ret schemas.MASs, err error) {
	stor.mutex.Lock()
Stefan Dähling's avatar
Stefan Dähling committed
181
	ret.Inst = make([]schemas.MASInfo, len(stor.mas), len(stor.mas))
Stefan Dähling's avatar
Stefan Dähling committed
182
183
	ret.Counter = stor.masCounter
	for i := 0; i < len(stor.mas); i++ {
Stefan Dähling's avatar
Stefan Dähling committed
184
		ret.Inst[i] = stor.mas[i]
Stefan Dähling's avatar
Stefan Dähling committed
185
186
187
188
189
	}
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// getMASInfoShort returns info of one MAS
func (stor *localStorage) getMASInfoShort(masID int) (ret schemas.MASInfoShort, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}
	ret.ID = stor.mas[masID].ID
	ret.Config = stor.mas[masID].Config
	ret.NumAgents = stor.mas[masID].Agents.Counter
	ret.Uptime = stor.mas[masID].Uptime
	ret.Status = stor.mas[masID].Status
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
207
208
209
210
211
212
213
214
// getMASInfo returns info of one MAS
func (stor *localStorage) getMASInfo(masID int) (ret schemas.MASInfo, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
215
	ret = stor.mas[masID]
Stefan Dähling's avatar
Stefan Dähling committed
216
217
218
219
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// getGroupInfo returns info of one image group
func (stor *localStorage) getGroupInfo(masID int, imID int) (ret schemas.ImageGroupInfo,
	err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}
	if len(stor.mas[masID].ImageGroups.Inst)-1 < imID {
		stor.mutex.Unlock()
		err = errors.New("ImageGroup does not exist")
		return
	}
	ret = stor.mas[masID].ImageGroups.Inst[imID]
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
239
240
241
242
243
244
245
246
// getAgents returns specs of all agents in MAS
func (stor *localStorage) getAgents(masID int) (ret schemas.Agents, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
247
	ret = stor.mas[masID].Agents
Stefan Dähling's avatar
Stefan Dähling committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	stor.mutex.Unlock()
	return
}

// getAgentInfo returns info of one agent
func (stor *localStorage) getAgentInfo(masID int, agentID int) (ret schemas.AgentInfo, err error) {
	stor.mutex.Lock()
	ret, err = stor.getAgentInfoNolock(masID, agentID)
	stor.mutex.Unlock()
	return
}

// getAgentInfo returns info of one agent
func (stor *localStorage) getAgentInfoNolock(masID int,
	agentID int) (ret schemas.AgentInfo, err error) {
	if len(stor.mas)-1 < masID {
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
267
	if len(stor.mas[masID].Agents.Inst)-1 < agentID {
Stefan Dähling's avatar
Stefan Dähling committed
268
269
270
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
271
	ret = stor.mas[masID].Agents.Inst[agentID]
Stefan Dähling's avatar
Stefan Dähling committed
272
273
274
275
276
277
278
279
280
281
282
	return
}

// getAgentAddress returns address of one agent
func (stor *localStorage) getAgentAddress(masID int, agentID int) (ret schemas.Address, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
283
	if len(stor.mas[masID].Agents.Inst)-1 < agentID {
Stefan Dähling's avatar
Stefan Dähling committed
284
285
286
287
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
288
	ret = stor.mas[masID].Agents.Inst[agentID].Address
Stefan Dähling's avatar
Stefan Dähling committed
289
290
291
292
293
294
295
296
297
298
299
300
301
	stor.mutex.Unlock()
	return
}

// setAgentAddress sets address of agent
func (stor *localStorage) setAgentAddress(masID int, agentID int,
	address schemas.Address) (err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
302
	if len(stor.mas[masID].Agents.Inst)-1 < agentID {
Stefan Dähling's avatar
Stefan Dähling committed
303
304
305
306
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
307
	stor.mas[masID].Agents.Inst[agentID].Address = address
Stefan Dähling's avatar
Stefan Dähling committed
308
309
310
311
	stor.mutex.Unlock()
	return
}

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// setAgentCustom sets custom of agent
func (stor *localStorage) setAgentCustom(masID int, agentID int, custom string) (err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
	if len(stor.mas[masID].Agents.Inst)-1 < agentID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
	stor.mas[masID].Agents.Inst[agentID].Spec.Custom = custom
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// setAgentStatus sets status of agent
func (stor *localStorage) setAgentStatus(masID int, agentID int,
	status schemas.Status) (err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
	if len(stor.mas[masID].Agents.Inst)-1 < agentID {
		stor.mutex.Unlock()
		err = errors.New("Agent does not exist")
		return
	}
	stor.mas[masID].Agents.Inst[agentID].Status = status
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
349
350
351
352
353
354
355
356
// getAgencies returns specs of all agencies in MAS
func (stor *localStorage) getAgencies(masID int) (ret schemas.Agencies, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agency does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
357
	ret.Counter = 0
Stefan Dähling's avatar
Stefan Dähling committed
358
359
360
361
	for i := range stor.mas[masID].ImageGroups.Inst {
		ret.Inst = append(ret.Inst,
			stor.mas[masID].ImageGroups.Inst[i].Agencies.Inst...)
		ret.Counter += len(stor.mas[masID].ImageGroups.Inst[i].Agencies.Inst)
Stefan Dähling's avatar
Stefan Dähling committed
362
	}
Stefan Dähling's avatar
Stefan Dähling committed
363
364
365
366
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
367
// getAgencyInfoFull returns status of one agency
Stefan Dähling's avatar
Stefan Dähling committed
368
func (stor *localStorage) getAgencyInfoFull(masID int, imID int,
Stefan Dähling's avatar
Stefan Dähling committed
369
	agencyID int) (ret schemas.AgencyInfoFull, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
370
371
372
373
374
375
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("Agency does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
376
	if len(stor.mas[masID].ImageGroups.Inst)-1 < imID {
Stefan Dähling's avatar
Stefan Dähling committed
377
378
379
380
		stor.mutex.Unlock()
		err = errors.New("Agency does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
381
	if len(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst)-1 < agencyID {
Stefan Dähling's avatar
Stefan Dähling committed
382
383
384
385
		stor.mutex.Unlock()
		err = errors.New("Agency does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
386
	ret.MASID = masID
Stefan Dähling's avatar
Stefan Dähling committed
387
	ret.Name = stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Name
Stefan Dähling's avatar
Stefan Dähling committed
388
389
	ret.ID = agencyID
	ret.ImageGroupID = imID
Stefan Dähling's avatar
Stefan Dähling committed
390
391
	ret.Logger = stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Logger
	ret.Status = stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Status
Stefan Dähling's avatar
Stefan Dähling committed
392
	ret.Agents = make([]schemas.AgentInfo,
Stefan Dähling's avatar
Stefan Dähling committed
393
394
		len(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Agents),
		len(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Agents))
Stefan Dähling's avatar
Stefan Dähling committed
395
396
	for i := 0; i < len(ret.Agents); i++ {
		var temp schemas.AgentInfo
Stefan Dähling's avatar
Stefan Dähling committed
397
		temp, err = stor.getAgentInfoNolock(masID,
Stefan Dähling's avatar
Stefan Dähling committed
398
			stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[agencyID].Agents[i])
Stefan Dähling's avatar
Stefan Dähling committed
399
400
401
402
403
404
		if err != nil {
			stor.mutex.Unlock()
			return
		}
		ret.Agents[i] = temp
	}
Stefan Dähling's avatar
Stefan Dähling committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	stor.mutex.Unlock()
	return
}

// registerMAS registers a new MAS with the storage and returns its ID
func (stor *localStorage) registerMAS() (masID int, err error) {
	stor.mutex.Lock()
	masID = stor.masCounter
	stor.masCounter++
	stor.mutex.Unlock()
	return
}

// storeMAS stores MAS specs
func (stor *localStorage) storeMAS(masID int, masInfo schemas.MASInfo) (err error) {
	newMAS := createMASStorage(masID, masInfo)
	stor.mutex.Lock()
	numMAS := len(stor.mas)
	if numMAS <= masID {
		for i := 0; i < masID-numMAS+1; i++ {
Stefan Dähling's avatar
Stefan Dähling committed
425
			stor.mas = append(stor.mas, schemas.MASInfo{})
Stefan Dähling's avatar
Stefan Dähling committed
426
427
428
		}
	} else {
		// check if mas stor is already populated
Stefan Dähling's avatar
Stefan Dähling committed
429
		if stor.mas[masID].ID == masID {
Stefan Dähling's avatar
Stefan Dähling committed
430
431
432
433
434
435
436
437
438
439
			err = errors.New("MAS already exists")
			return
		}
	}
	stor.mas[masID] = newMAS
	stor.mutex.Unlock()
	return
}

// createMASStorage returns a filled masStorage object
Stefan Dähling's avatar
Stefan Dähling committed
440
441
442
443
444
445
446
func createMASStorage(masID int, masInfo schemas.MASInfo) (ret schemas.MASInfo) {
	ret = masInfo
	ret.Status.Code = status.Running
	ret.Status.LastUpdate = time.Now()

	ret.ID = masID
	for i := 0; i < ret.Agents.Counter; i++ {
Stefan Dähling's avatar
Stefan Dähling committed
447
448
449
		ret.Agents.Inst[i].MASID = masID
		ret.Agents.Inst[i].Address.Agency = "mas-" + strconv.Itoa(masID) +
			ret.Agents.Inst[i].Address.Agency + ".mas" + strconv.Itoa(masID) + "agencies"
Stefan Dähling's avatar
Stefan Dähling committed
450
	}
Stefan Dähling's avatar
Stefan Dähling committed
451
	for i := 0; i < ret.ImageGroups.Counter; i++ {
Stefan Dähling's avatar
Stefan Dähling committed
452
453
454
455
		for j := 0; j < ret.ImageGroups.Inst[i].Agencies.Counter; j++ {
			ret.ImageGroups.Inst[i].Agencies.Inst[j].MASID = masID
			ret.ImageGroups.Inst[i].Agencies.Inst[j].Name = "mas-" + strconv.Itoa(masID) +
				ret.ImageGroups.Inst[i].Agencies.Inst[j].Name + ".mas" +
Stefan Dähling's avatar
Stefan Dähling committed
456
457
				strconv.Itoa(masID) + "agencies"
		}
Stefan Dähling's avatar
Stefan Dähling committed
458
459
460
461
462
463
464
465
466
467
468
469
	}
	return
}

// deleteMAS deletes MAS with specified ID
func (stor *localStorage) deleteMAS(masID int) (err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
470
471
	stor.mas[masID].Status.Code = status.Terminated
	stor.mas[masID].Status.LastUpdate = time.Now()
Stefan Dähling's avatar
Stefan Dähling committed
472
473
474
475
476
477
478
	// copy(stor.mas[masID:], stor.mas[masID+1:])
	// stor.mas[len(stor.mas)-1] = masStorage{}
	// stor.mas = stor.mas[:len(stor.mas)-1]
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
479
// registerImageGroup registers a new image group with the storage and returns its ID
Stefan Dähling's avatar
Stefan Dähling committed
480
481
func (stor *localStorage) registerImageGroup(masID int,
	config schemas.ImageGroupConfig) (newGroup bool, imID int, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
482
483
484
485
486
487
488
489
490
491
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}

	for i := range stor.mas[masID].ImageGroups.Inst {
		if stor.mas[masID].ImageGroups.Inst[i].Config.Image == config.Image {
			stor.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
492
493
			imID = i
			newGroup = false
Stefan Dähling's avatar
Stefan Dähling committed
494
495
496
			return
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
497
	newGroup = true
Stefan Dähling's avatar
Stefan Dähling committed
498
499
500
501
502
503
504
505
506
507
508
509
510

	info := schemas.ImageGroupInfo{
		Config: config,
		ID:     stor.mas[masID].ImageGroups.Counter,
	}
	stor.mas[masID].ImageGroups.Counter++
	stor.mas[masID].ImageGroups.Inst = append(stor.mas[masID].ImageGroups.Inst,
		info)
	stor.mutex.Unlock()
	imID = info.ID
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
511
// addAgent adds an agent to an existing MAS
Stefan Dähling's avatar
Stefan Dähling committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
func (stor *localStorage) addAgent(masID int, imID int,
	agentSpec schemas.AgentSpec) (newAgency bool, agentID int, agencyID int, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}

	if len(stor.mas[masID].ImageGroups.Inst)-1 < imID {
		stor.mutex.Unlock()
		err = errors.New("ImageGroup does not exist")
		return
	}
	stor.mutex.Unlock()

	// register agent
	agentID, err = stor.registerAgent(masID, imID, agentSpec)
	if err != nil {
		return
	}

	// schedule agent to agency
	newAgency = true
	stor.mutex.Lock()
	agentInfo := stor.mas[masID].Agents.Inst[agentID]
	numAgentsPerAgency := stor.mas[masID].Config.NumAgentsPerAgency
	for i := range stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst {
		if len(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[i].Agents) < numAgentsPerAgency {
			// there exists an agency with space left
			stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[i].Agents =
				append(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst[i].Agents, agentID)
			agencyID = i
			newAgency = false
			break
		}
	}
	if newAgency {
		// new agency has to be created
		agencyID = stor.mas[masID].ImageGroups.Inst[imID].Agencies.Counter
		stor.mas[masID].ImageGroups.Inst[imID].Agencies.Counter++
		agencyInfo := schemas.AgencyInfo{
			MASID:        masID,
			ImageGroupID: imID,
			ID:           agencyID,
			Name: "mas-" + strconv.Itoa(masID) + "-im-" + strconv.Itoa(imID) +
				"-agency-" + strconv.Itoa(agencyID) + ".mas" + strconv.Itoa(masID) + "agencies",
			Logger: stor.mas[masID].Config.Logger,
			Agents: []int{agentID},
		}
		stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst =
			append(stor.mas[masID].ImageGroups.Inst[imID].Agencies.Inst, agencyInfo)
	}

	// store info about agency in agent
	agentInfo.AgencyID = agencyID
	agentInfo.Address.Agency = "mas-" + strconv.Itoa(masID) + "-im-" + strconv.Itoa(imID) +
		"-agency-" + strconv.Itoa(agencyID) + ".mas" + strconv.Itoa(masID) + "agencies"
	stor.mas[masID].Agents.Inst[agentID] = agentInfo
	stor.mutex.Unlock()

Stefan Dähling's avatar
Stefan Dähling committed
573
574
575
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
576
// registerAgent registers a new agent with the storage and returns its ID
Stefan Dähling's avatar
Stefan Dähling committed
577
func (stor *localStorage) registerAgent(masID int, imID int,
Stefan Dähling's avatar
Stefan Dähling committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
	spec schemas.AgentSpec) (agentID int, err error) {
	stor.mutex.Lock()
	if len(stor.mas)-1 < masID {
		stor.mutex.Unlock()
		err = errors.New("MAS does not exist")
		return
	}

	agentID = stor.mas[masID].Agents.Counter
	stor.mas[masID].Agents.Counter++

	info := schemas.AgentInfo{
		Spec:         spec,
		MASID:        masID,
		ImageGroupID: imID,
		ID:           agentID,
	}

	stor.mas[masID].Agents.Inst = append(stor.mas[masID].Agents.Inst, info)
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
601
602
603
604
605
606
607
608
609
610
611
// deleteAgent deletes an agent
func (stor *localStorage) deleteAgent(masID int, agentID int) (err error) {
	err = stor.setAgentAddress(masID, agentID, schemas.Address{Agency: ""})
	if err != nil {
		return
	}
	err = stor.setAgentStatus(masID, agentID, schemas.Status{Code: status.Terminated,
		LastUpdate: time.Now()})
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
612
613
614
615
616
617
618
// newLocalStorage returns Storage interface with localStorage type
func newLocalStorage() storage {
	var temp localStorage
	temp.mutex = &sync.Mutex{}
	temp.cloneMAP.Uptime = time.Now()
	return &temp
}