amqpclient.go 9.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** AMQP package, client.
*
* @author Sonja Happ <sonja.happ@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASweb-backend-go
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
22
package amqp
23
24
25
26

import (
	"encoding/json"
	"fmt"
27
	infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
28
	"github.com/gin-gonic/gin"
29
	"github.com/google/uuid"
30
	"github.com/jinzhu/gorm"
31
	"github.com/streadway/amqp"
32
	"log"
33
34
35
36
37
38
39
40
41
42
43
44
	"time"
)

const VILLAS_EXCHANGE = "villas"

type AMQPclient struct {
	connection *amqp.Connection
	channel    *amqp.Channel
	replies    <-chan amqp.Delivery
}

type Action struct {
Sonja Happ's avatar
Sonja Happ committed
45
46
47
	Act        string   `json:"action"`
	When       float32  `json:"when"`
	Parameters struct{} `json:"parameters"`
Sonja Happ's avatar
Sonja Happ committed
48
49
50
	UUID       *string  `json:"uuid"`
	//Model      struct{} `json:"model"`
	//Results    struct{} `json:"results"`
51
52
}

53
type ICUpdate struct {
54
	State      *string `json:"state"`
55
	Properties struct {
Sonja Happ's avatar
Sonja Happ committed
56
57
58
59
60
61
62
63
		UUID        string  `json:"uuid"`
		Name        *string `json:"name"`
		Category    *string `json:"category"`
		Type        *string `json:"type"`
		Location    *string `json:"location"`
		WS_url      *string `json:"ws_url"`
		API_url     *string `json:"api_url"`
		Description *string `json:"description"`
64
	} `json:"properties"`
Sonja Happ's avatar
Sonja Happ committed
65
	// TODO add JSON start parameter scheme
66
67
}

68
69
70
71
72
73
74
75
76
var client AMQPclient

func ConnectAMQP(uri string) error {

	var err error

	// connect to broker
	client.connection, err = amqp.Dial(uri)
	if err != nil {
77
		return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker %v, error: %v", uri, err)
78
79
80
81
82
	}

	// create channel
	client.channel, err = client.connection.Channel()
	if err != nil {
83
		return fmt.Errorf("AMQP: failed to open a channel, error: %v", err)
84
85
86
87
88
89
90
91
92
93
	}
	// declare exchange
	err = client.channel.ExchangeDeclare(VILLAS_EXCHANGE,
		"headers",
		true,
		false,
		false,
		false,
		nil)
	if err != nil {
94
		return fmt.Errorf("AMQP: failed to declare the exchange, error: %v", err)
95
96
	}

97
98
	// add a queue for the ICs
	ICQueue, err := client.channel.QueueDeclare("infrastructure_components",
99
100
101
102
103
104
		true,
		false,
		false,
		false,
		nil)
	if err != nil {
105
		return fmt.Errorf("AMQP: failed to declare the queue, error: %v", err)
106
107
	}

108
	err = client.channel.QueueBind(ICQueue.Name, "", VILLAS_EXCHANGE, false, nil)
109
	if err != nil {
110
		return fmt.Errorf("AMQP: failed to bind the queue, error: %v", err)
111
112
113
	}

	// consume deliveries
114
	client.replies, err = client.channel.Consume(ICQueue.Name,
115
		"",
116
		true,
117
118
119
120
121
		false,
		false,
		false,
		nil)
	if err != nil {
122
		return fmt.Errorf("AMQP: failed to consume deliveries, error: %v", err)
123
124
125
126
	}

	// consuming queue
	go func() {
127
128
		for {
			for message := range client.replies {
129
130
131
132
				err = processMessage(message)
				if err != nil {
					log.Println(err.Error())
				}
133
			}
134
			time.Sleep(2) // sleep for 2 sek
135
136
137
		}
	}()

138
139
	log.Printf(" AMQP: Waiting for messages... ")

140
141
142
	return nil
}

Sonja Happ's avatar
Sonja Happ committed
143
func SendActionAMQP(action Action) error {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

	payload, err := json.Marshal(action)
	if err != nil {
		return err
	}

	msg := amqp.Publishing{
		DeliveryMode:    2,
		Timestamp:       time.Now(),
		ContentType:     "application/json",
		ContentEncoding: "utf-8",
		Priority:        0,
		Body:            payload,
	}

Sonja Happ's avatar
Sonja Happ committed
159
160
161
	err = CheckConnection()
	if err != nil {
		return err
162
163
	}

Sonja Happ's avatar
Sonja Happ committed
164
	log.Println("AMQP: Sending message", string(msg.Body))
165
166
167
168
169
170
171
172
173
174
	err = client.channel.Publish(VILLAS_EXCHANGE,
		"",
		false,
		false,
		msg)
	return err

}

func PingAMQP() error {
175
	log.Println("AMQP: sending ping command to all ICs")
176
177
178

	var a Action
	a.Act = "ping"
Sonja Happ's avatar
Sonja Happ committed
179
	*a.UUID = ""
180

Sonja Happ's avatar
Sonja Happ committed
181
	err := SendActionAMQP(a)
182
183
	return err
}
184
185
186

func CheckConnection() error {

187
188
189
190
191
192
	if client.connection != nil {
		if client.connection.IsClosed() {
			return fmt.Errorf("connection to broker is closed")
		}
	} else {
		return fmt.Errorf("connection is nil")
193
194
195
196
	}

	return nil
}
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

func StartAMQP(AMQPurl string, api *gin.RouterGroup) error {
	if AMQPurl != "" {
		log.Println("Starting AMQP client")

		err := ConnectAMQP(AMQPurl)
		if err != nil {
			return err
		}

		// register IC action endpoint only if AMQP client is used
		RegisterAMQPEndpoint(api.Group("/ic"))

		// Periodically call the Ping function to check which ICs are still there
		ticker := time.NewTicker(10 * time.Second)
		go func() {

			for {
				select {
				case <-ticker.C:
217
218
219
220
221
222
223
					//TODO Add a useful regular event here
					/*
						err = PingAMQP()
						if err != nil {
							log.Println("AMQP Error: ", err.Error())
						}
					*/
224
225
226
227
228
229
230
231
232
233
				}
			}

		}()

		log.Printf("Connected AMQP client to %s", AMQPurl)
	}

	return nil
}
234

235
func processMessage(message amqp.Delivery) error {
236

Sonja Happ's avatar
Sonja Happ committed
237
	log.Println("Processing AMQP message: ", string(message.Body))
Sonja Happ's avatar
Sonja Happ committed
238

239
240
241
	var payload ICUpdate
	err := json.Unmarshal(message.Body, &payload)
	if err != nil {
242
		return fmt.Errorf("AMQP: Could not unmarshal message to JSON: %v err: %v", string(message.Body), err)
243
244
245
246
247
248
	}

	ICUUID := payload.Properties.UUID
	_, err = uuid.Parse(ICUUID)

	if err != nil {
249
		return fmt.Errorf("AMQP: UUID not valid: %v, message ignored: %v \n", ICUUID, string(message.Body))
250
251
252
253
254
255
256
	}

	var sToBeUpdated infrastructure_component.InfrastructureComponent
	err = sToBeUpdated.ByUUID(ICUUID)

	if err == gorm.ErrRecordNotFound {
		// create new record
257
258
259
260
261
262
263
264
		err = createNewIC(payload)
	} else if err != nil {
		// database error
		err = fmt.Errorf("AMQP: Database error for IC %v DB error message: %v", ICUUID, err)
	} else {
		// update record
		err = updateIC(payload, sToBeUpdated)
	}
265

266
267
	return err
}
268

269
func createNewIC(payload ICUpdate) error {
270

271
272
273
274
275
276
277
278
279
280
281
	var newICReq infrastructure_component.AddICRequest
	newICReq.InfrastructureComponent.UUID = payload.Properties.UUID
	if payload.Properties.Name == nil ||
		payload.Properties.Category == nil ||
		payload.Properties.Type == nil {
		// cannot create new IC because required information (name, type, and/or category missing)
		return fmt.Errorf("AMQP: Cannot create new IC, required field(s) is/are missing: name, type, category")
	}
	newICReq.InfrastructureComponent.Name = *payload.Properties.Name
	newICReq.InfrastructureComponent.Category = *payload.Properties.Category
	newICReq.InfrastructureComponent.Type = *payload.Properties.Type
282

283
284
285
	// add optional params
	if payload.State != nil {
		newICReq.InfrastructureComponent.State = *payload.State
286
	} else {
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
		newICReq.InfrastructureComponent.State = "unknown"
	}
	if payload.Properties.WS_url != nil {
		newICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url
	}
	if payload.Properties.API_url != nil {
		newICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url
	}
	if payload.Properties.Location != nil {
		newICReq.InfrastructureComponent.Location = *payload.Properties.Location
	}
	if payload.Properties.Description != nil {
		newICReq.InfrastructureComponent.Description = *payload.Properties.Description
	}
	// TODO add JSON start parameter scheme
302

303
304
	// set managed externally to true because this IC is created via AMQP
	newICReq.InfrastructureComponent.ManagedExternally = newTrue()
305

306
307
308
309
310
	// Validate the new IC
	err := newICReq.Validate()
	if err != nil {
		return fmt.Errorf("AMQP: Validation of new IC failed: %v", err)
	}
311

312
313
	// Create the new IC
	newIC := newICReq.CreateIC()
314

315
316
317
318
319
	// save IC
	err = newIC.Save()
	if err != nil {
		return fmt.Errorf("AMQP: Saving new IC to DB failed: %v", err)
	}
320

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	return nil
}

func updateIC(payload ICUpdate, sToBeUpdated infrastructure_component.InfrastructureComponent) error {
	var updatedICReq infrastructure_component.UpdateICRequest
	if payload.State != nil {
		updatedICReq.InfrastructureComponent.State = *payload.State
	}
	if payload.Properties.Type != nil {
		updatedICReq.InfrastructureComponent.Type = *payload.Properties.Type
	}
	if payload.Properties.Category != nil {
		updatedICReq.InfrastructureComponent.Category = *payload.Properties.Category
	}
	if payload.Properties.Name != nil {
		updatedICReq.InfrastructureComponent.Name = *payload.Properties.Name
	}
	if payload.Properties.WS_url != nil {
		updatedICReq.InfrastructureComponent.WebsocketURL = *payload.Properties.WS_url
	}
	if payload.Properties.API_url != nil {
		updatedICReq.InfrastructureComponent.APIURL = *payload.Properties.API_url
343
	}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	if payload.Properties.Location != nil {
		//postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Properties.Location + `"}`)}
		updatedICReq.InfrastructureComponent.Location = *payload.Properties.Location
	}
	if payload.Properties.Description != nil {
		updatedICReq.InfrastructureComponent.Description = *payload.Properties.Description
	}
	// TODO add JSON start parameter scheme

	// set managed externally to true because this IC is updated via AMQP
	updatedICReq.InfrastructureComponent.ManagedExternally = newTrue()

	// Validate the updated IC
	err := updatedICReq.Validate()
	if err != nil {
		return fmt.Errorf("AMQP: Validation of updated IC failed: %v", err)
	}

	// Create the updated IC from old IC
	updatedIC := updatedICReq.UpdatedIC(sToBeUpdated)

	// Finally update the IC in the DB
	err = sToBeUpdated.Update(updatedIC)
	if err != nil {
		return fmt.Errorf("AMQP: Unable to update IC %v in DB: %v", sToBeUpdated.Name, err)
	}

	return err
}

func newTrue() *bool {
	b := true
	return &b
377
}