handler.go 12.7 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
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.
*/

// handler for http requests

package ams

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"

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

// handleAPI is the global handler for requests to path /api
func (ams *AMS) handleAPI(w http.ResponseWriter, r *http.Request) {
	var cmapErr, httpErr error
	ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
	// determine which ressource is requested and call corresponding handler
	respath := strings.Split(r.URL.EscapedPath(), "/")
	resvalid := false

	switch len(respath) {
	case 3:
		if respath[2] == "clonemap" {
			cmapErr, httpErr = ams.handleCloneMAP(w, r)
			resvalid = true
Stefan Dähling's avatar
Stefan Dähling committed
74
75
76
		} else if respath[2] == "alive" {
			cmapErr, httpErr = ams.handleAlive(w, r)
			resvalid = true
Stefan Dähling's avatar
Stefan Dähling committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		}
	case 4:
		if respath[2] == "clonemap" && respath[3] == "mas" {
			cmapErr, httpErr = ams.handleMAS(w, r)
			resvalid = true
		}
	case 5:
		var masID int
		masID, cmapErr = strconv.Atoi(respath[4])
		if respath[2] == "clonemap" && respath[3] == "mas" && cmapErr == nil {
			cmapErr, httpErr = ams.handlemasID(masID, w, r)
			resvalid = true
		}
	case 6:
		var masID int
		masID, cmapErr = strconv.Atoi(respath[4])
		if respath[2] == "clonemap" && respath[3] == "mas" && cmapErr == nil {
			if respath[5] == "agents" {
				cmapErr, httpErr = ams.handleAgent(masID, w, r)
				resvalid = true
			} else if respath[5] == "agencies" {
				cmapErr, httpErr = ams.handleAgency(masID, w, r)
				resvalid = true
			}
		}
	case 7:
		var masID int
		masID, cmapErr = strconv.Atoi(respath[4])
		if respath[2] == "clonemap" && respath[3] == "mas" && cmapErr == nil {
			if respath[5] == "agents" {
				var agentID int
				agentID, cmapErr = strconv.Atoi(respath[6])
				if cmapErr == nil {
					cmapErr, httpErr = ams.handleAgentID(masID, agentID, w, r)
					resvalid = true
				}
			}
		}
	case 8:
		var masID int
		masID, cmapErr = strconv.Atoi(respath[4])
		if respath[2] == "clonemap" && respath[3] == "mas" && cmapErr == nil {
			if respath[5] == "agents" {
				var agentID int
				agentID, cmapErr = strconv.Atoi(respath[6])
				if cmapErr == nil {
					// if respath[7] == "status" {
					// 	cmapErr, httpErr = ams.handleAgentStatus(masID, agentID, w, r)
					// 	resvalid = true
					// } else
					if respath[7] == "address" {
						cmapErr, httpErr = ams.handleAgentAddress(masID, agentID, w, r)
						resvalid = true
130
131
132
					} else if respath[7] == "custom" {
						cmapErr, httpErr = ams.handleAgentCustom(masID, agentID, w, r)
						resvalid = true
Stefan Dähling's avatar
Stefan Dähling committed
133
					}
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
134
				}
Stefan Dähling's avatar
Stefan Dähling committed
135
136
137
138
139
140
141
142
143
			}
		}
	case 9:
		var masID int
		masID, cmapErr = strconv.Atoi(respath[4])
		if respath[2] == "clonemap" && respath[3] == "mas" && cmapErr == nil {
			if respath[5] == "imgroup" && respath[7] == "agency" {
				var imID, agencyID int
				imID, cmapErr = strconv.Atoi(respath[6])
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
144
				if cmapErr == nil {
Stefan Dähling's avatar
Stefan Dähling committed
145
					agencyID, cmapErr = strconv.Atoi(respath[8])
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
146
					if cmapErr == nil {
Stefan Dähling's avatar
Stefan Dähling committed
147
						cmapErr, httpErr = ams.handleAgencyID(masID, imID, agencyID, w, r)
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
148
						resvalid = true
149
					}
Stefan Dähling's avatar
Stefan Dähling committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
				}
			}
		}
	default:
		cmapErr = errors.New("Resource not found")
	}

	if !resvalid {
		httpErr = httpreply.NotFoundError(w)
		cmapErr = errors.New("Resource not found")
	}
	if cmapErr != nil {
		ams.logError.Println(respath, cmapErr)
	}
	if httpErr != nil {
		ams.logError.Println(respath, httpErr)
	}
}

Stefan Dähling's avatar
Stefan Dähling committed
169
170
171
172
173
174
175
176
177
178
179
// handleAlive is the handler for requests to path /api/alive
func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		httpErr = httpreply.Alive(w, nil)
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/alive")
	}
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// handleCloneMAP is the handler for requests to path /api/clonemap
func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		// return info about running clonemap instance
		var cmapInfo schemas.CloneMAP
		cmapInfo, cmapErr = ams.getCloneMAPInfo()
		httpErr = httpreply.Resource(w, cmapInfo, cmapErr)
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap")
	}
	return
}

// handleMAS is the handler for requests to path /api/clonemap/mas
func (ams *AMS) handleMAS(w http.ResponseWriter, r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		// return short info of all MAS
Stefan Dähling's avatar
Stefan Dähling committed
198
199
		var mass []schemas.MASInfoShort
		mass, cmapErr = ams.getMASsShort()
Stefan Dähling's avatar
Stefan Dähling committed
200
201
202
203
204
205
		httpErr = httpreply.Resource(w, mass, cmapErr)
	} else if r.Method == "POST" {
		// create new MAS
		var body []byte
		body, cmapErr = ioutil.ReadAll(r.Body)
		if cmapErr == nil {
Stefan Dähling's avatar
Stefan Dähling committed
206
207
			var masSpec schemas.MASSpec
			cmapErr = json.Unmarshal(body, &masSpec)
Stefan Dähling's avatar
Stefan Dähling committed
208
			if cmapErr == nil {
Stefan Dähling's avatar
Stefan Dähling committed
209
				cmapErr = ams.createMAS(masSpec)
Stefan Dähling's avatar
Stefan Dähling committed
210
				if cmapErr == nil {
Stefan Dähling's avatar
Stefan Dähling committed
211
212
					httpErr = httpreply.Created(w, cmapErr, "text/plain",
						[]byte("Ressource Created"))
Stefan Dähling's avatar
Stefan Dähling committed
213
				} else {
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
214
					httpErr = httpreply.CMAPError(w, cmapErr.Error())
Stefan Dähling's avatar
Stefan Dähling committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
				}
			} else {
				httpErr = httpreply.JSONUnmarshalError(w)
			}
		} else {
			httpErr = httpreply.InvalidBodyError(w)
		}
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas")
	}
	return
}

// handlemasID is the handler for requests to path /api/clonemap/mas/{mas-id}
func (ams *AMS) handlemasID(masID int, w http.ResponseWriter, r *http.Request) (cmapErr,
	httpErr error) {
	if r.Method == "GET" {
		// return long information about specified MAS
		var masInfo schemas.MASInfo
		masInfo, cmapErr = ams.getMASInfo(masID)
		httpErr = httpreply.Resource(w, masInfo, cmapErr)
	} else if r.Method == "DELETE" {
		// delete specified MAS
		cmapErr = ams.removeMAS(masID)
		httpErr = httpreply.Deleted(w, cmapErr)
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}")
	}
	return
}

// handleAgent is the handler for requests to path /api/clonemap/mas/{mas-id}/agents
func (ams *AMS) handleAgent(masID int, w http.ResponseWriter, r *http.Request) (cmapErr,
	httpErr error) {
	if r.Method == "GET" {
		// return short information of all agents in specified MAS
		var agents schemas.Agents
		agents, cmapErr = ams.getAgents(masID)
		httpErr = httpreply.Resource(w, agents, cmapErr)
Stefan Dähling's avatar
Stefan Dähling committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
	} else if r.Method == "POST" {
		// create new agent in MAS
		var body []byte
		body, cmapErr = ioutil.ReadAll(r.Body)
		if cmapErr == nil {
			var groupSpecs []schemas.ImageGroupSpec
			cmapErr = json.Unmarshal(body, &groupSpecs)
			if cmapErr == nil {
				cmapErr = ams.createAgents(masID, groupSpecs)
				httpErr = httpreply.Created(w, cmapErr, "text/plain", []byte("Ressource Created"))
			} else {
				httpErr = httpreply.JSONUnmarshalError(w)
			}
		} else {
			httpErr = httpreply.InvalidBodyError(w)
		}
Stefan Dähling's avatar
Stefan Dähling committed
272
273
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
274
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/agents")
Stefan Dähling's avatar
Stefan Dähling committed
275
276
277
278
279
280
281
282
283
284
285
286
	}
	return
}

// handleAgentID is the handler for requests to path /api/clonemap/mas/{mas-id}/agents/{agent-id}
func (ams *AMS) handleAgentID(masID int, agentid int, w http.ResponseWriter,
	r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		// return long information of specified agent
		var agentInfo schemas.AgentInfo
		agentInfo, cmapErr = ams.getAgentInfo(masID, agentid)
		httpErr = httpreply.Resource(w, agentInfo, cmapErr)
Stefan Dähling's avatar
Stefan Dähling committed
287
288
289
290
291
	} else if r.Method == "DELETE" {
		// delete specified agent
		cmapErr = ams.removeAgent(masID, agentid)
		httpErr = httpreply.Deleted(w, cmapErr)

Stefan Dähling's avatar
Stefan Dähling committed
292
293
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
294
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/agents/" +
Stefan Dähling's avatar
Stefan Dähling committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
			"{agent-id}")
	}
	return
}

// handleAgentAddress is the handler for requests to path
// /api/clonemap/mas/{mas-id}/agents/{agent-id}/address
func (ams *AMS) handleAgentAddress(masID int, agentid int, w http.ResponseWriter,
	r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		// return address of specified agent
		var agentAddr schemas.Address
		agentAddr, cmapErr = ams.getAgentAddress(masID, agentid)
		httpErr = httpreply.Resource(w, agentAddr, cmapErr)
	} else if r.Method == "PUT" {
		// update address of specified agent
		var body []byte
		body, cmapErr = ioutil.ReadAll(r.Body)
		if cmapErr == nil {
			var agentAddr schemas.Address
			cmapErr := json.Unmarshal(body, &agentAddr)
			if cmapErr == nil {
317
318
				cmapErr = ams.updateAgentAddress(masID, agentid, agentAddr)
				httpErr = httpreply.Updated(w, cmapErr)
Stefan Dähling's avatar
Stefan Dähling committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
			} else {
				httpErr = httpreply.JSONUnmarshalError(w)
			}
		} else {
			httpErr = httpreply.InvalidBodyError(w)
		}
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/agent/" +
			"{agent-id}/address")
	}
	return
}

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// handleAgentAddress is the handler for requests to path
// /api/clonemap/mas/{mas-id}/agents/{agent-id}/custom
func (ams *AMS) handleAgentCustom(masID int, agentid int, w http.ResponseWriter,
	r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "PUT" {
		// update custom of specified agent
		var body []byte
		body, cmapErr = ioutil.ReadAll(r.Body)
		if cmapErr == nil {
			custom := string(body)
			cmapErr = ams.updateAgentCustom(masID, agentid, custom)
			httpErr = httpreply.Updated(w, cmapErr)
		} else {
			httpErr = httpreply.InvalidBodyError(w)
		}
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/agent/" +
			"{agent-id}/custom")
	}
	return
}

356
// handleAgency is the handler for requests to path /api/cloumap/mas/{mas-id}/agencies
Stefan Dähling's avatar
Stefan Dähling committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
func (ams *AMS) handleAgency(masID int, w http.ResponseWriter, r *http.Request) (cmapErr,
	httpErr error) {
	if r.Method == "GET" {
		// return information of specified agency
		var agencies schemas.Agencies
		agencies, cmapErr = ams.getAgencies(masID)
		httpErr = httpreply.Resource(w, agencies, cmapErr)
	} else {
		httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/" +
			"agencies/")
	}
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
372
373
// handleAgencyID is the handler for requests to path /api/clonemap/mas/{mas-id}/imgroup/{imID}/agencies/{agency-id}
func (ams *AMS) handleAgencyID(masID int, imID int, agencyid int, w http.ResponseWriter,
374
375
376
	r *http.Request) (cmapErr, httpErr error) {
	if r.Method == "GET" {
		var agencySpec schemas.AgencyInfoFull
Stefan Dähling's avatar
Stefan Dähling committed
377
		agencySpec, cmapErr = ams.getAgencyInfoFull(masID, imID, agencyid)
378
379
380
381
382
383
384
385
		httpErr = httpreply.Resource(w, agencySpec, cmapErr)
	} else {
		httpErr = httpreply.MethodNotAllowed(w)
		cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/" +
			"agencies/{agency-id}")
	}
	return
}
Stefan Dähling's avatar
Stefan Dähling committed
386
387
388
389
390
391
392
393
394
395
396
397
398

// listen opens a http server listening and serving request
func (ams *AMS) listen() (err error) {
	mux := http.NewServeMux()
	mux.HandleFunc("/api/", ams.handleAPI)
	s := &http.Server{
		Addr:    ":9000",
		Handler: mux,
	}
	ams.logInfo.Println("AMS listening on port 9000")
	err = s.ListenAndServe()
	return
}