df.go 5.44 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
/*
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 agency

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

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

// DF provides access to the functionality of the DF
type DF struct {
	agentID            int
	masID              int
	nodeID             int
	mutex              *sync.Mutex
	registeredServices map[string]schemas.Service
65
	active             bool // indicates if df is active (switch via env)
Stefan Dähling's avatar
Stefan Dähling committed
66
	dfClient           *client.DFClient
Stefan Dähling's avatar
Stefan Dähling committed
67
68
69
70
71
72
	logError           *log.Logger
	logInfo            *log.Logger
}

// RegisterService registers a new service with the DF
func (df *DF) RegisterService(svc schemas.Service) (id string, err error) {
73
74
75
76
77
78
	df.mutex.Lock()
	if !df.active {
		df.mutex.Unlock()
		return
	}
	df.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
79
80
	id = "-1"
	if svc.Desc == "" {
81
		err = errors.New("empty description not allowed")
Stefan Dähling's avatar
Stefan Dähling committed
82
83
84
85
86
87
		return
	}
	df.mutex.Lock()
	_, ok := df.registeredServices[svc.Desc]
	df.mutex.Unlock()
	if ok {
88
		err = errors.New("service already registered")
Stefan Dähling's avatar
Stefan Dähling committed
89
90
91
92
93
94
95
96
97
98
99
100
		return
	}
	df.mutex.Lock()
	masID := df.masID
	agentID := df.agentID
	nodeID := df.nodeID
	df.mutex.Unlock()
	svc.MASID = masID
	svc.AgentID = agentID
	svc.NodeID = nodeID
	svc.CreatedAt = time.Now()
	svc.ChangedAt = svc.CreatedAt
Stefan Dähling's avatar
Stefan Dähling committed
101
	svc, _, err = df.dfClient.PostSvc(masID, svc)
Stefan Dähling's avatar
Stefan Dähling committed
102
103
104
105
106
107
108
109
110
111
112
113
114
	id = svc.GUID
	if err != nil {
		return
	}
	df.mutex.Lock()
	df.registeredServices[svc.Desc] = svc
	df.mutex.Unlock()
	return
}

// SearchForService search for a service with given description
func (df *DF) SearchForService(desc string) (svc []schemas.Service, err error) {
	df.mutex.Lock()
115
116
117
118
	if !df.active {
		df.mutex.Unlock()
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
119
120
121
	masID := df.masID
	df.mutex.Unlock()
	var temp []schemas.Service
Stefan Dähling's avatar
Stefan Dähling committed
122
	temp, _, err = df.dfClient.GetSvc(masID, desc)
Stefan Dähling's avatar
Stefan Dähling committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	if err != nil {
		return
	}
	for i := range temp {
		if temp[i].AgentID != df.agentID {
			svc = append(svc, temp[i])
		}
	}
	return
}

// SearchForLocalService search for a service with given description
func (df *DF) SearchForLocalService(desc string, dist float64) (svc []schemas.Service, err error) {
	df.mutex.Lock()
137
138
139
140
	if !df.active {
		df.mutex.Unlock()
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
141
142
143
144
	masID := df.masID
	nodeID := df.nodeID
	df.mutex.Unlock()
	var temp []schemas.Service
Stefan Dähling's avatar
Stefan Dähling committed
145
	temp, _, err = df.dfClient.GetLocalSvc(masID, desc, nodeID, dist)
Stefan Dähling's avatar
Stefan Dähling committed
146
147
148
149
150
151
152
153
154
155
156
157
158
	if err != nil {
		return
	}
	for i := range temp {
		if temp[i].AgentID != df.agentID {
			svc = append(svc, temp[i])
		}
	}
	return
}

// DeregisterService registers a new service with the DF
func (df *DF) DeregisterService(svcID string) (err error) {
159
160
161
162
163
164
	df.mutex.Lock()
	if !df.active {
		df.mutex.Unlock()
		return
	}
	df.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
165
166
167
168
169
170
171
172
173
174
175
	desc := ""
	df.mutex.Lock()
	masID := df.masID
	for i := range df.registeredServices {
		if df.registeredServices[i].GUID == svcID {
			desc = i
			break
		}
	}
	df.mutex.Unlock()
	if desc == "" {
176
		err = errors.New("no such service")
Stefan Dähling's avatar
Stefan Dähling committed
177
178
179
180
181
		return
	}
	df.mutex.Lock()
	delete(df.registeredServices, desc)
	df.mutex.Unlock()
Stefan Dähling's avatar
Stefan Dähling committed
182
	_, err = df.dfClient.DeleteSvc(masID, svcID)
Stefan Dähling's avatar
Stefan Dähling committed
183
184
185
186
	return
}

// newDF creates a new DF object
Stefan Dähling's avatar
Stefan Dähling committed
187
func newDF(masID int, agentID int, nodeID int, dfCli *client.DFClient, logErr *log.Logger,
Stefan Dähling's avatar
Stefan Dähling committed
188
	logInf *log.Logger) (df *DF) {
Stefan Dähling's avatar
Stefan Dähling committed
189
190
191
192
193
	df = &DF{
		agentID:  agentID,
		masID:    masID,
		nodeID:   nodeID,
		mutex:    &sync.Mutex{},
194
		active:   false,
Stefan Dähling's avatar
Stefan Dähling committed
195
196
197
		logError: logErr,
		logInfo:  logInf,
	}
198
199
200
201
	act := os.Getenv("CLONEMAP_DF")
	if act == "ON" {
		df.active = true
	}
Stefan Dähling's avatar
Stefan Dähling committed
202
203
204
	df.registeredServices = make(map[string]schemas.Service)
	return
}
Stefan Dähling's avatar
Stefan Dähling committed
205
206
207
208
209
210
211
212
213
214
215
216

// close closes the DF module
func (df *DF) close() {
	for d := range df.registeredServices {
		svc := df.registeredServices[d]
		df.DeregisterService(svc.GUID)
	}
	df.mutex.Lock()
	df.logInfo.Println("Closing DF of agent ", df.agentID)
	df.active = false
	df.mutex.Unlock()
}