amqpclient.go 8.25 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
31
	"github.com/jinzhu/gorm"
	"github.com/jinzhu/gorm/dialects/postgres"
32
	"github.com/streadway/amqp"
33
	"log"
34
35
36
37
38
39
40
41
42
43
44
45
	"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
46
47
48
49
50
	Act        string   `json:"action"`
	When       float32  `json:"when"`
	Parameters struct{} `json:"parameters"`
	Model      struct{} `json:"model"`
	Results    struct{} `json:"results"`
51
52
}

53
type ICUpdate struct {
54
	State      *string `json:"state"`
55
	Properties struct {
56
57
58
59
60
		UUID     string  `json:"uuid"`
		Name     *string `json:"name"`
		Category *string `json:"category"`
		Type     *string `json:"type"`
		Location *string `json:"location"`
61
62
63
	} `json:"properties"`
}

64
65
66
67
68
69
70
71
72
var client AMQPclient

func ConnectAMQP(uri string) error {

	var err error

	// connect to broker
	client.connection, err = amqp.Dial(uri)
	if err != nil {
73
		return fmt.Errorf("AMQP: failed to connect to RabbitMQ broker %v", uri)
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	}

	// create channel
	client.channel, err = client.connection.Channel()
	if err != nil {
		return fmt.Errorf("AMQP: failed to open a channel")
	}
	// declare exchange
	err = client.channel.ExchangeDeclare(VILLAS_EXCHANGE,
		"headers",
		true,
		false,
		false,
		false,
		nil)
	if err != nil {
		return fmt.Errorf("AMQP: failed to declare the exchange")
	}

93
94
	// add a queue for the ICs
	ICQueue, err := client.channel.QueueDeclare("infrastructure_components",
95
96
97
98
99
100
101
102
103
		true,
		false,
		false,
		false,
		nil)
	if err != nil {
		return fmt.Errorf("AMQP: failed to declare the queue")
	}

104
	err = client.channel.QueueBind(ICQueue.Name, "", VILLAS_EXCHANGE, false, nil)
105
106
107
108
109
	if err != nil {
		return fmt.Errorf("AMQP: failed to bind the queue")
	}

	// consume deliveries
110
	client.replies, err = client.channel.Consume(ICQueue.Name,
111
112
113
114
115
116
117
118
119
120
121
122
123
		"",
		false,
		false,
		false,
		false,
		nil)
	if err != nil {
		return fmt.Errorf("AMQP: failed to consume deliveries")
	}

	// consuming queue
	go func() {
		for message := range client.replies {
124
125
126
127
			//err = message.Ack(false)
			//if err != nil {
			//	fmt.Println("AMQP: Unable to ack message:", err)
			//}
128

129
			var payload ICUpdate
130
131
132
			err := json.Unmarshal(message.Body, &payload)
			if err != nil {
				log.Println("AMQP: Could not unmarshal message to JSON:", string(message.Body), "err: ", err)
133
				continue
134
			}
135

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

139
			if err != nil {
140
				//log.Printf("AMQP: UUID not valid: %v, message ignored: %v \n", ICUUID, string(message.Body))
141
				continue
142
			} else {
143

144
145
146
				var sToBeUpdated infrastructure_component.InfrastructureComponent
				err = sToBeUpdated.ByUUID(ICUUID)

147
148
149
150
151
152
153
154
155
156
157
158
159
				if err == gorm.ErrRecordNotFound {
					// create new record
					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)
						log.Println("AMQP: Cannot create new IC, required field(s) is/are missing: name, type, category")
						continue
					}
					newICReq.InfrastructureComponent.Name = *payload.Properties.Name
					newICReq.InfrastructureComponent.Category = *payload.Properties.Category
Sonja Happ's avatar
Sonja Happ committed
160
					newICReq.InfrastructureComponent.Type = *payload.Properties.Type
161
162
163
164
165
166
167
168
169
170
171
172

					// add optional params
					if payload.State != nil {
						newICReq.InfrastructureComponent.State = *payload.State
					} else {
						newICReq.InfrastructureComponent.State = "unknown"
					}
					if payload.Properties.Location != nil {
						newICReq.InfrastructureComponent.Properties = postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Properties.Location + `"}`)}
					}

					// Validate the new IC
173
174
					err = newICReq.Validate()
					if err != nil {
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
						log.Println("AMQP: Validation of new IC failed:", err)
						continue
					}

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

					// save IC
					err = newIC.Save()
					if err != nil {
						log.Println("AMQP: Saving new IC to DB failed:", err)
						continue
					}

					log.Println("AMQP: Created IC ", newIC.Name)

				} else if err != nil {
					log.Println("AMQP: Database error for IC", ICUUID, " DB error message: ", err)
193
					continue
194
				} else {
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

					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.Location != nil {
						updatedICReq.InfrastructureComponent.Properties = postgres.Jsonb{json.RawMessage(`{"location" : " ` + *payload.Properties.Location + `"}`)}
					}

					// Validate the updated IC
					if err = updatedICReq.Validate(); err != nil {
						log.Println("AMQP: Validation of updated IC failed:", err)
						continue
					}

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

					// Finally update the IC in the DB
					err = sToBeUpdated.Update(updatedIC)
224
225
					if err != nil {
						log.Println("AMQP: Unable to update IC", sToBeUpdated.Name, "in DB: ", err)
226
						continue
227
228
					}

229
					log.Println("AMQP: Updated IC ", sToBeUpdated.Name)
230
231
				}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
			}
		}
	}()

	return nil
}

func SendActionAMQP(action Action, uuid string) error {

	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,
	}

	if uuid != "" {
		msg.Headers["uuid"] = uuid
		msg.Headers["action"] = "ping"
	}

	err = client.channel.Publish(VILLAS_EXCHANGE,
		"",
		false,
		false,
		msg)
	return err

}

func PingAMQP() error {
270
	log.Println("AMQP: sending ping command to all ICs")
271
272
273
274
275
276
277

	var a Action
	a.Act = "ping"

	err := SendActionAMQP(a, "")
	return err
}
278
279
280

func CheckConnection() error {

281
282
283
284
285
286
	if client.connection != nil {
		if client.connection.IsClosed() {
			return fmt.Errorf("connection to broker is closed")
		}
	} else {
		return fmt.Errorf("connection is nil")
287
288
289
290
	}

	return nil
}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

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:
311
312
313
314
315
316
317
					//TODO Add a useful regular event here
					/*
						err = PingAMQP()
						if err != nil {
							log.Println("AMQP Error: ", err.Error())
						}
					*/
318
319
320
321
322
323
324
325
326
327
				}
			}

		}()

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

	return nil
}