Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
Cloud
MAS
clonemap
Commits
3c4011be
Commit
3c4011be
authored
Mar 25, 2020
by
Stefan Dähling
Browse files
cleanup of core schemas
parent
d9f03c54
Pipeline
#259911
passed with stages
in 2 minutes and 52 seconds
Changes
10
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
pkg/agency/acc.go
View file @
3c4011be
...
...
@@ -104,10 +104,10 @@ func (agency *Agency) aclLookup(agentID int) (acl *ACL, err error) {
agency
.
mutex
.
Unlock
()
// check if remote agency is already known
if
ok
{
agency
.
logInfo
.
Println
(
"New remote agent "
,
agentInfo
.
Spec
.
ID
,
" in known agency "
,
address
.
Agency
)
agency
.
logInfo
.
Println
(
"New remote agent "
,
agentInfo
.
ID
,
" in known agency "
,
address
.
Agency
)
ag
=
newAgent
(
agentInfo
,
remAgency
.
msgIn
,
nil
,
nil
,
schemas
.
LogConfig
{},
nil
,
agency
.
logError
,
agency
.
logInfo
)
}
else
{
agency
.
logInfo
.
Println
(
"New remote agent "
,
agentInfo
.
Spec
.
ID
,
" in unknown agency "
,
address
.
Agency
)
agency
.
logInfo
.
Println
(
"New remote agent "
,
agentInfo
.
ID
,
" in unknown agency "
,
address
.
Agency
)
// create new remote agency
remAgency
=
&
remoteAgency
{
msgIn
:
make
(
chan
schemas
.
ACLMessage
,
1000
),
...
...
pkg/agency/agency.go
View file @
3c4011be
...
...
@@ -166,9 +166,9 @@ func (agency *Agency) terminate(gracefulStop chan os.Signal) {
// startAgents starts all the agents
func
(
agency
*
Agency
)
startAgents
()
(
err
error
)
{
// request configuration
var
agency
Config
schemas
.
Agency
Config
agency
Config
,
_
,
err
=
amsclient
.
GetAgency
Config
(
agency
.
info
.
Spec
.
MASID
,
agency
.
info
.
Spec
.
ID
)
agency
.
info
.
Spec
.
Logger
=
agency
Config
.
Spec
.
Logger
var
agency
Spec
schemas
.
Agency
Spec
agency
Spec
,
_
,
err
=
amsclient
.
GetAgency
Spec
(
agency
.
info
.
Spec
.
MASID
,
agency
.
info
.
Spec
.
ID
)
agency
.
info
.
Spec
.
Logger
=
agencySpec
.
Logger
if
err
!=
nil
{
agency
.
info
.
Status
=
schemas
.
Status
{
Code
:
status
.
Error
,
...
...
@@ -177,8 +177,8 @@ func (agency *Agency) startAgents() (err error) {
return
}
agency
.
logInfo
.
Println
(
"Starting agents"
)
for
i
:=
0
;
i
<
len
(
agency
Config
.
Agents
);
i
++
{
err
=
agency
.
createAgent
(
agency
Config
.
Agents
[
i
])
for
i
:=
0
;
i
<
len
(
agency
Spec
.
Agents
);
i
++
{
err
=
agency
.
createAgent
(
agency
Spec
.
Agents
[
i
])
if
err
!=
nil
{
agency
.
mutex
.
Lock
()
agency
.
info
.
Status
=
schemas
.
Status
{
...
...
@@ -196,7 +196,7 @@ func (agency *Agency) startAgents() (err error) {
func
(
agency
*
Agency
)
createAgent
(
agentInfo
schemas
.
AgentInfo
)
(
err
error
)
{
// check if agent does not exist
agency
.
mutex
.
Lock
()
_
,
agExist
:=
agency
.
localAgents
[
agentInfo
.
Spec
.
ID
]
_
,
agExist
:=
agency
.
localAgents
[
agentInfo
.
ID
]
agency
.
mutex
.
Unlock
()
if
agExist
{
err
=
errors
.
New
(
"NotAllowedError"
)
...
...
@@ -208,7 +208,7 @@ func (agency *Agency) createAgent(agentInfo schemas.AgentInfo) (err error) {
agency
.
mutex
.
Lock
()
ag
:=
newAgent
(
agentInfo
,
msgIn
,
agency
.
aclLookup
,
agency
.
logger
,
agency
.
info
.
Spec
.
Logger
,
agency
.
mqtt
,
agency
.
logError
,
agency
.
logInfo
)
agency
.
localAgents
[
agentInfo
.
Spec
.
ID
]
=
ag
agency
.
localAgents
[
agentInfo
.
ID
]
=
ag
agency
.
mutex
.
Unlock
()
ag
.
startAgent
(
agency
.
agentTask
)
return
...
...
pkg/agency/agent.go
View file @
3c4011be
...
...
@@ -79,15 +79,15 @@ func newAgent(info schemas.AgentInfo, msgIn chan schemas.ACLMessage,
aclLookup
func
(
int
)
(
*
ACL
,
error
),
log
*
loggerClient
,
logConfig
schemas
.
LogConfig
,
mqtt
*
mqttClient
,
logErr
*
log
.
Logger
,
logInf
*
log
.
Logger
)
(
ag
*
Agent
)
{
ag
=
&
Agent
{
id
:
info
.
Spec
.
ID
,
id
:
info
.
ID
,
nodeID
:
info
.
Spec
.
NodeID
,
name
:
info
.
Spec
.
Name
,
aType
:
info
.
Spec
.
AType
,
aSubtype
:
info
.
Spec
.
ASubtype
,
masID
:
info
.
Spec
.
MASID
,
masID
:
info
.
MASID
,
custom
:
info
.
Spec
.
Custom
,
mutex
:
&
sync
.
Mutex
{},
ACL
:
newACL
(
info
.
Spec
.
ID
,
msgIn
,
aclLookup
,
logErr
,
logInf
),
ACL
:
newACL
(
info
.
ID
,
msgIn
,
aclLookup
,
logErr
,
logInf
),
logError
:
logErr
,
logInfo
:
logInf
,
}
...
...
pkg/ams/ams.go
View file @
3c4011be
...
...
@@ -185,16 +185,16 @@ func (ams *AMS) getAgencies(masID int) (ret schemas.Agencies, err error) {
return
}
// getAgency
Config
returns status of one agency
func
(
ams
*
AMS
)
getAgency
Config
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Config
,
err
error
)
{
ret
,
err
=
ams
.
stor
.
getAgency
Config
(
masID
,
agencyID
)
// getAgency
Spec
returns status of one agency
func
(
ams
*
AMS
)
getAgency
Spec
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Spec
,
err
error
)
{
ret
,
err
=
ams
.
stor
.
getAgency
Spec
(
masID
,
agencyID
)
return
}
// createMAS creates a new mas according to masconfig
func
(
ams
*
AMS
)
createMAS
(
mas
Conf
schemas
.
MAS
Config
)
(
err
error
)
{
func
(
ams
*
AMS
)
createMAS
(
mas
Spec
schemas
.
MAS
Spec
)
(
err
error
)
{
// fill masInfo
masInfo
,
numAgencies
:=
ams
.
configureMAS
(
mas
Conf
)
masInfo
,
numAgencies
:=
ams
.
configureMAS
(
mas
Spec
)
// safe mas in storage and get ID
var
masID
int
...
...
@@ -229,8 +229,8 @@ func (ams *AMS) startMAS(masID int, masInfo schemas.MASInfo, numAgencies int) (e
image
:=
""
pullSecret
:=
""
if
len
(
masInfo
.
Agents
.
Instances
)
>
0
{
image
=
masInfo
.
Agents
.
Instances
[
0
]
.
AgencyImage
pullSecret
=
masInfo
.
Agents
.
Instances
[
0
]
.
ImagePullSecret
image
=
masInfo
.
Agents
.
Instances
[
0
]
.
Spec
.
AgencyImage
pullSecret
=
masInfo
.
Agents
.
Instances
[
0
]
.
Spec
.
ImagePullSecret
}
err
=
ams
.
depl
.
newMAS
(
masID
,
image
,
pullSecret
,
numAgencies
,
masInfo
.
Spec
.
Logging
,
masInfo
.
Spec
.
MQTT
,
masInfo
.
Spec
.
DF
)
...
...
@@ -244,30 +244,30 @@ func (ams *AMS) startMAS(masID int, masInfo schemas.MASInfo, numAgencies int) (e
}
// configureMAS fills the missing configuration as agencies, agent ids and addresses
func
(
ams
*
AMS
)
configureMAS
(
mas
Conf
schemas
.
MAS
Config
)
(
masInfo
schemas
.
MASInfo
,
numAgencies
int
)
{
func
(
ams
*
AMS
)
configureMAS
(
mas
Spec
schemas
.
MAS
Spec
)
(
masInfo
schemas
.
MASInfo
,
numAgencies
int
)
{
// MAS configuration
masInfo
.
Spec
=
mas
Conf
.
Spec
masInfo
.
Agents
.
Counter
=
len
(
mas
Conf
.
Agents
)
masInfo
.
Agents
.
Instances
=
make
([]
schemas
.
Agent
Spec
,
masInfo
.
Agents
.
Counter
,
masInfo
.
Spec
=
masSpec
masInfo
.
Agents
.
Counter
=
len
(
mas
Spec
.
Agents
)
masInfo
.
Agents
.
Instances
=
make
([]
schemas
.
Agent
Info
,
masInfo
.
Agents
.
Counter
,
masInfo
.
Agents
.
Counter
)
numAgencies
=
len
(
mas
Conf
.
Agents
)
/
mas
Conf
.
Spec
.
NumAgentsPerAgency
if
len
(
mas
Conf
.
Agents
)
%
mas
Conf
.
Spec
.
NumAgentsPerAgency
>
0
{
numAgencies
=
len
(
mas
Spec
.
Agents
)
/
masSpec
.
NumAgentsPerAgency
if
len
(
mas
Spec
.
Agents
)
%
masSpec
.
NumAgentsPerAgency
>
0
{
numAgencies
++
}
masInfo
.
Agencies
.
Counter
=
numAgencies
masInfo
.
Agencies
.
Instances
=
make
([]
schemas
.
Agency
Spec
,
numAgencies
,
numAgencies
)
masInfo
.
Graph
=
mas
Conf
.
Graph
masInfo
.
Agencies
.
Instances
=
make
([]
schemas
.
Agency
Info
,
numAgencies
,
numAgencies
)
masInfo
.
Graph
=
mas
Spec
.
Graph
if
len
(
masInfo
.
Graph
.
Node
)
==
0
{
masInfo
.
Graph
.
Node
=
append
(
masInfo
.
Graph
.
Node
,
schemas
.
Node
{
ID
:
0
})
}
// agent configuration
for
i
:=
0
;
i
<
masInfo
.
Agents
.
Counter
;
i
++
{
masInfo
.
Agents
.
Instances
[
i
]
=
mas
Conf
.
Agents
[
i
]
masInfo
.
Agents
.
Instances
[
i
]
.
Spec
=
mas
Spec
.
Agents
[
i
]
masInfo
.
Agents
.
Instances
[
i
]
.
ID
=
i
masInfo
.
Agents
.
Instances
[
i
]
.
AgencyID
=
i
/
mas
Conf
.
Spec
.
NumAgentsPerAgency
masInfo
.
Agents
.
Instances
[
i
]
.
AgencyID
=
i
/
masSpec
.
NumAgentsPerAgency
for
j
:=
range
masInfo
.
Graph
.
Node
{
if
masInfo
.
Graph
.
Node
[
j
]
.
ID
==
masInfo
.
Agents
.
Instances
[
i
]
.
NodeID
{
if
masInfo
.
Graph
.
Node
[
j
]
.
ID
==
masInfo
.
Agents
.
Instances
[
i
]
.
Spec
.
NodeID
{
masInfo
.
Graph
.
Node
[
j
]
.
Agent
=
append
(
masInfo
.
Graph
.
Node
[
j
]
.
Agent
,
masInfo
.
Agents
.
Instances
[
i
]
.
ID
)
break
...
...
@@ -280,16 +280,16 @@ func (ams *AMS) configureMAS(masConf schemas.MASConfig) (masInfo schemas.MASInfo
for
i
:=
0
;
i
<
numAgencies
;
i
++
{
agencySpec
:=
schemas
.
AgencySpec
{
ID
:
i
,
Logger
:
mas
Conf
.
Spec
.
Logger
,
Logger
:
masSpec
.
Logger
,
}
for
j
:=
0
;
j
<
mas
Conf
.
Spec
.
NumAgentsPerAgency
;
j
++
{
for
j
:=
0
;
j
<
masSpec
.
NumAgentsPerAgency
;
j
++
{
if
agentCounter
>=
masInfo
.
Agents
.
Counter
{
break
}
agencySpec
.
Agents
=
append
(
agencySpec
.
Agents
,
agentCounter
)
agencySpec
.
Agents
=
append
(
agencySpec
.
Agents
,
masInfo
.
Agents
.
Instances
[
agentCounter
]
)
agentCounter
++
}
masInfo
.
Agencies
.
Instances
[
i
]
=
agencySpec
masInfo
.
Agencies
.
Instances
[
i
]
.
Spec
=
agencySpec
}
return
}
...
...
pkg/ams/ams_test.go
View file @
3c4011be
...
...
@@ -126,14 +126,12 @@ func dummyClient(s *http.Server, t *testing.T) {
if
httpStatus
!=
http
.
StatusOK
{
t
.
Error
(
"Error GetCloneMAP "
+
strconv
.
Itoa
(
httpStatus
))
}
mas
:=
schemas
.
MASConfig
{
Spec
:
schemas
.
MASSpec
{
Name
:
"test"
,
NumAgentsPerAgency
:
10
,
Logging
:
false
,
MQTT
:
false
,
DF
:
false
,
},
mas
:=
schemas
.
MASSpec
{
Name
:
"test"
,
NumAgentsPerAgency
:
10
,
Logging
:
false
,
MQTT
:
false
,
DF
:
false
,
Agents
:
[]
schemas
.
AgentSpec
{
schemas
.
AgentSpec
{
AgencyImage
:
"agent"
,
...
...
@@ -205,7 +203,7 @@ func dummyClient(s *http.Server, t *testing.T) {
t
.
Error
(
"Error GetAgencies "
+
strconv
.
Itoa
(
httpStatus
))
}
_
,
httpStatus
,
err
=
amsclient
.
GetAgency
Config
(
0
,
0
)
_
,
httpStatus
,
err
=
amsclient
.
GetAgency
Spec
(
0
,
0
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
...
...
pkg/ams/client/client.go
View file @
3c4011be
...
...
@@ -98,7 +98,7 @@ func GetMASs() (mass schemas.MASs, httpStatus int, err error) {
}
// PostMAS post an mas
func
PostMAS
(
mas
schemas
.
MAS
Config
)
(
httpStatus
int
,
err
error
)
{
func
PostMAS
(
mas
schemas
.
MAS
Spec
)
(
httpStatus
int
,
err
error
)
{
js
,
_
:=
json
.
Marshal
(
mas
)
_
,
httpStatus
,
err
=
httpretry
.
Post
(
httpClient
,
"http://"
+
Host
+
":"
+
strconv
.
Itoa
(
Port
)
+
"/api/clonemap/mas"
,
"application/json"
,
js
,
time
.
Second
*
2
,
2
)
...
...
@@ -190,8 +190,8 @@ func GetAgencies(masID int) (agencies schemas.Agencies, httpStatus int, err erro
return
}
// GetAgency
Config
requests agency information
func
GetAgency
Config
(
masID
int
,
agencyID
int
)
(
agency
schemas
.
Agency
Config
,
httpStatus
int
,
// GetAgency
Spec
requests agency information
func
GetAgency
Spec
(
masID
int
,
agencyID
int
)
(
agency
schemas
.
Agency
Spec
,
httpStatus
int
,
err
error
)
{
var
body
[]
byte
body
,
httpStatus
,
err
=
httpretry
.
Get
(
httpClient
,
"http://"
+
Host
+
":"
+
strconv
.
Itoa
(
Port
)
+
...
...
@@ -203,7 +203,7 @@ func GetAgencyConfig(masID int, agencyID int) (agency schemas.AgencyConfig, http
//fmt.Println(string(body))
err
=
json
.
Unmarshal
(
body
,
&
agency
)
if
err
!=
nil
{
agency
=
schemas
.
Agency
Config
{}
agency
=
schemas
.
Agency
Spec
{}
}
return
}
...
...
pkg/ams/etcd.go
View file @
3c4011be
...
...
@@ -203,12 +203,12 @@ func (stor *etcdStorage) uploadAgentInfo(newMAS masStorage) (err error) {
if
err
!=
nil
{
return
}
Ops
[
i
]
=
clientv3
.
OpPut
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
spec
.
ID
)
+
"/agent/"
+
Ops
[
i
]
=
clientv3
.
OpPut
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
id
)
+
"/agent/"
+
strconv
.
Itoa
(
agentIndex
),
string
(
res
))
agentIndex
++
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
cond
:=
clientv3
.
Compare
(
clientv3
.
Version
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
spec
.
ID
)
+
cond
:=
clientv3
.
Compare
(
clientv3
.
Version
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
id
)
+
"/agentcounter"
),
">"
,
0
)
_
,
err
=
stor
.
client
.
Txn
(
ctx
)
.
If
(
cond
)
.
Then
(
Ops
...
)
.
Commit
()
cancel
()
...
...
@@ -239,12 +239,12 @@ func (stor *etcdStorage) uploadAgencyInfo(newMAS masStorage) (err error) {
if
err
!=
nil
{
return
}
Ops
[
i
]
=
clientv3
.
OpPut
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
spec
.
ID
)
+
"/agency/"
+
Ops
[
i
]
=
clientv3
.
OpPut
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
id
)
+
"/agency/"
+
strconv
.
Itoa
(
agencyIndex
),
string
(
res
))
agencyIndex
++
}
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
cond
:=
clientv3
.
Compare
(
clientv3
.
Version
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
spec
.
ID
)
+
cond
:=
clientv3
.
Compare
(
clientv3
.
Version
(
"ams/mas/"
+
strconv
.
Itoa
(
newMAS
.
id
)
+
"/agencycounter"
),
">"
,
0
)
_
,
err
=
stor
.
client
.
Txn
(
ctx
)
.
If
(
cond
)
.
Then
(
Ops
...
)
.
Commit
()
cancel
()
...
...
pkg/ams/handler.go
View file @
3c4011be
...
...
@@ -138,19 +138,19 @@ func (ams *AMS) handleAPI(w http.ResponseWriter, r *http.Request) {
resvalid
=
true
}
}
}
else
if
respath
[
5
]
==
"agencies"
{
var
agencyID
int
agencyID
,
cmapErr
=
strconv
.
Atoi
(
respath
[
6
])
if
cmapErr
==
nil
{
// if respath[7] == "status" {
// cmapErr, httpErr = ams.handleAgencyStatus(masID, agencyID, w, r)
// resvalid = true
// } else
if
respath
[
7
]
==
"config"
{
cmapErr
,
httpErr
=
ams
.
handleAgencyConfig
(
masID
,
agencyID
,
w
,
r
)
resvalid
=
true
}
}
//
} else if respath[5] == "agencies" {
//
var agencyID int
//
agencyID, cmapErr = strconv.Atoi(respath[6])
//
if cmapErr == nil {
//
// if respath[7] == "status" {
//
// cmapErr, httpErr = ams.handleAgencyStatus(masID, agencyID, w, r)
//
// resvalid = true
//
// } else
//
if respath[7] == "config" {
//
cmapErr, httpErr = ams.handleAgencyConfig(masID, agencyID, w, r)
//
resvalid = true
//
}
//
}
}
}
default
:
...
...
@@ -195,10 +195,10 @@ func (ams *AMS) handleMAS(w http.ResponseWriter, r *http.Request) (cmapErr, http
var
body
[]
byte
body
,
cmapErr
=
ioutil
.
ReadAll
(
r
.
Body
)
if
cmapErr
==
nil
{
var
mas
Config
schemas
.
MAS
Config
cmapErr
=
json
.
Unmarshal
(
body
,
&
mas
Config
)
var
mas
Spec
schemas
.
MAS
Spec
cmapErr
=
json
.
Unmarshal
(
body
,
&
mas
Spec
)
if
cmapErr
==
nil
{
cmapErr
=
ams
.
createMAS
(
mas
Config
)
cmapErr
=
ams
.
createMAS
(
mas
Spec
)
if
cmapErr
==
nil
{
httpErr
=
httpreply
.
Created
(
w
,
cmapErr
,
"text/plain"
,
[]
byte
(
"Ressource Created"
))
}
else
{
...
...
@@ -340,9 +340,9 @@ func (ams *AMS) handleAgency(masID int, w http.ResponseWriter, r *http.Request)
func
(
ams
*
AMS
)
handleAgencyID
(
masID
int
,
agencyid
int
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
cmapErr
,
httpErr
error
)
{
if
r
.
Method
==
"GET"
{
var
agency
Config
schemas
.
Agency
Config
agency
Config
,
cmapErr
=
ams
.
getAgency
Config
(
masID
,
agencyid
)
httpErr
=
httpreply
.
Resource
(
w
,
agency
Config
,
cmapErr
)
var
agency
Spec
schemas
.
Agency
Spec
agency
Spec
,
cmapErr
=
ams
.
getAgency
Spec
(
masID
,
agencyid
)
httpErr
=
httpreply
.
Resource
(
w
,
agency
Spec
,
cmapErr
)
}
else
{
httpErr
=
httpreply
.
MethodNotAllowed
(
w
)
cmapErr
=
errors
.
New
(
"Error: Method not allowed on path /api/clonemap/mas/{mas-id}/"
+
...
...
@@ -351,22 +351,22 @@ func (ams *AMS) handleAgencyID(masID int, agencyid int, w http.ResponseWriter,
return
}
// handleAgencyConfig is the handler for requests to path
// /api/clonemap/mas/{mas-id}/agencies/{agency-id}/config
func
(
ams
*
AMS
)
handleAgencyConfig
(
masID
int
,
agencyid
int
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
cmapErr
,
httpErr
error
)
{
if
r
.
Method
==
"GET"
{
// return config of specified agency
var
agencyConfig
schemas
.
AgencyConfig
agencyConfig
,
cmapErr
=
ams
.
getAgencyConfig
(
masID
,
agencyid
)
httpErr
=
httpreply
.
Resource
(
w
,
agencyConfig
,
cmapErr
)
}
else
{
httpErr
=
httpreply
.
MethodNotAllowed
(
w
)
cmapErr
=
errors
.
New
(
"Error: Method not allowed on path /api/clonemap/mas/{mas-id}/"
+
"agencies/{agency-id}/config"
)
}
return
}
//
// handleAgencyConfig is the handler for requests to path
//
//
/api/clonemap/mas/{mas-id}/agencies/{agency-id}/config
//
func (ams *AMS) handleAgencyConfig(masID int, agencyid int, w http.ResponseWriter,
//
r *http.Request) (cmapErr, httpErr error) {
//
if r.Method == "GET" {
//
// return config of specified agency
//
var agencyConfig schemas.AgencyConfig
//
agencyConfig, cmapErr = ams.getAgencyConfig(masID, agencyid)
//
httpErr = httpreply.Resource(w, agencyConfig, cmapErr)
//
} else {
//
httpErr = httpreply.MethodNotAllowed(w)
//
cmapErr = errors.New("Error: Method not allowed on path /api/clonemap/mas/{mas-id}/" +
//
"agencies/{agency-id}/config")
//
}
//
return
//
}
// listen opens a http server listening and serving request
func
(
ams
*
AMS
)
listen
()
(
err
error
)
{
...
...
pkg/ams/storage.go
View file @
3c4011be
...
...
@@ -86,8 +86,8 @@ type storage interface {
// getAgencies returns specs of all agencies in MAS
getAgencies
(
masID
int
)
(
ret
schemas
.
Agencies
,
err
error
)
// getAgency
Config
returns status of one agency
getAgency
Config
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Config
,
err
error
)
// getAgency
Spec
returns status of one agency
getAgency
Spec
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Spec
,
err
error
)
// registerMAS registers a new MAS with the storage and returns its ID
registerMAS
()
(
masID
int
,
err
error
)
...
...
@@ -122,6 +122,7 @@ type localStorage struct {
// mas storage
type
masStorage
struct
{
id
int
// mas id
spec
schemas
.
MASSpec
status
schemas
.
Status
agentCounter
int
// counter for agents
...
...
@@ -170,16 +171,16 @@ func (stor *localStorage) getMASInfo(masID int) (ret schemas.MASInfo, err error)
ret
.
Spec
=
stor
.
mas
[
masID
]
.
spec
ret
.
Status
=
stor
.
mas
[
masID
]
.
status
ret
.
Graph
=
stor
.
mas
[
masID
]
.
graph
ret
.
Agencies
.
Instances
=
make
([]
schemas
.
Agency
Spec
,
len
(
stor
.
mas
[
masID
]
.
agencies
),
ret
.
Agencies
.
Instances
=
make
([]
schemas
.
Agency
Info
,
len
(
stor
.
mas
[
masID
]
.
agencies
),
len
(
stor
.
mas
[
masID
]
.
agencies
))
for
i
:=
0
;
i
<
len
(
stor
.
mas
[
masID
]
.
agencies
);
i
++
{
ret
.
Agencies
.
Instances
[
i
]
=
stor
.
mas
[
masID
]
.
agencies
[
i
]
.
Spec
ret
.
Agencies
.
Instances
[
i
]
.
Spec
=
stor
.
mas
[
masID
]
.
agencies
[
i
]
.
Spec
}
ret
.
Agencies
.
Counter
=
stor
.
mas
[
masID
]
.
agencyCounter
ret
.
Agents
.
Instances
=
make
([]
schemas
.
Agent
Spec
,
len
(
stor
.
mas
[
masID
]
.
agents
),
ret
.
Agents
.
Instances
=
make
([]
schemas
.
Agent
Info
,
len
(
stor
.
mas
[
masID
]
.
agents
),
len
(
stor
.
mas
[
masID
]
.
agents
))
for
i
:=
0
;
i
<
len
(
stor
.
mas
[
masID
]
.
agents
);
i
++
{
ret
.
Agents
.
Instances
[
i
]
=
stor
.
mas
[
masID
]
.
agents
[
i
]
.
Spec
ret
.
Agents
.
Instances
[
i
]
.
Spec
=
stor
.
mas
[
masID
]
.
agents
[
i
]
.
Spec
}
ret
.
Agents
.
Counter
=
stor
.
mas
[
masID
]
.
agentCounter
stor
.
mutex
.
Unlock
()
...
...
@@ -194,10 +195,10 @@ func (stor *localStorage) getAgents(masID int) (ret schemas.Agents, err error) {
err
=
errors
.
New
(
"MAS does not exist"
)
return
}
ret
.
Instances
=
make
([]
schemas
.
Agent
Spec
,
len
(
stor
.
mas
[
masID
]
.
agents
),
ret
.
Instances
=
make
([]
schemas
.
Agent
Info
,
len
(
stor
.
mas
[
masID
]
.
agents
),
len
(
stor
.
mas
[
masID
]
.
agents
))
for
i
:=
0
;
i
<
len
(
stor
.
mas
[
masID
]
.
agents
);
i
++
{
ret
.
Instances
[
i
]
=
stor
.
mas
[
masID
]
.
agents
[
i
]
.
Spec
ret
.
Instances
[
i
]
.
Spec
=
stor
.
mas
[
masID
]
.
agents
[
i
]
.
Spec
}
ret
.
Counter
=
stor
.
mas
[
masID
]
.
agentCounter
stor
.
mutex
.
Unlock
()
...
...
@@ -272,19 +273,19 @@ func (stor *localStorage) getAgencies(masID int) (ret schemas.Agencies, err erro
err
=
errors
.
New
(
"Agency does not exist"
)
return
}
ret
.
Instances
=
make
([]
schemas
.
Agency
Spec
,
len
(
stor
.
mas
[
masID
]
.
agencies
),
ret
.
Instances
=
make
([]
schemas
.
Agency
Info
,
len
(
stor
.
mas
[
masID
]
.
agencies
),
len
(
stor
.
mas
[
masID
]
.
agencies
))
for
i
:=
0
;
i
<
len
(
stor
.
mas
[
masID
]
.
agencies
);
i
++
{
ret
.
Instances
[
i
]
=
stor
.
mas
[
masID
]
.
agencies
[
i
]
.
Spec
ret
.
Instances
[
i
]
.
Spec
=
stor
.
mas
[
masID
]
.
agencies
[
i
]
.
Spec
}
ret
.
Counter
=
stor
.
mas
[
masID
]
.
agencyCounter
stor
.
mutex
.
Unlock
()
return
}
// getAgency
Config
returns status of one agency
func
(
stor
*
localStorage
)
getAgency
Config
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Config
,
err
error
)
{
// getAgency
Spec
returns status of one agency
func
(
stor
*
localStorage
)
getAgency
Spec
(
masID
int
,
agencyID
int
)
(
ret
schemas
.
Agency
Spec
,
err
error
)
{
stor
.
mutex
.
Lock
()
if
len
(
stor
.
mas
)
-
1
<
masID
{
stor
.
mutex
.
Unlock
()
...
...
@@ -296,17 +297,17 @@ func (stor *localStorage) getAgencyConfig(masID int,
err
=
errors
.
New
(
"Agency does not exist"
)
return
}
ret
.
Spec
=
stor
.
mas
[
masID
]
.
agencies
[
agencyID
]
.
Spec
ret
.
Agents
=
make
([]
schemas
.
AgentInfo
,
len
(
ret
.
Spec
.
Agents
),
len
(
ret
.
Spec
.
Agents
))
for
i
:=
0
;
i
<
len
(
ret
.
Spec
.
Agents
);
i
++
{
var
temp
schemas
.
AgentInfo
temp
,
err
=
stor
.
getAgentInfoNolock
(
masID
,
ret
.
Spec
.
Agents
[
i
])
if
err
!=
nil
{
stor
.
mutex
.
Unlock
()
return
}
ret
.
Agents
[
i
]
=
temp
}
ret
=
stor
.
mas
[
masID
]
.
agencies
[
agencyID
]
.
Spec
//
ret.Agents = make([]schemas.AgentInfo, len(ret.Spec.Agents), len(ret.Spec.Agents))
//
for i := 0; i < len(ret.Spec.Agents); i++ {
//
var temp schemas.AgentInfo
//
temp, err = stor.getAgentInfoNolock(masID, ret.Spec.Agents[i]
.ID
)
//
if err != nil {
//
stor.mutex.Unlock()
//
return
//
}
//
ret.Agents[i] = temp
//
}
stor
.
mutex
.
Unlock
()
return
}
...
...
@@ -331,7 +332,7 @@ func (stor *localStorage) storeMAS(masID int, masInfo schemas.MASInfo) (err erro
}
}
else
{
// check if mas stor is already populated
if
stor
.
mas
[
masID
]
.
spec
.
ID
==
masID
{
if
stor
.
mas
[
masID
]
.
id
==
masID
{
err
=
errors
.
New
(
"MAS already exists"
)
return
}
...
...
@@ -350,17 +351,17 @@ func createMASStorage(masID int, masInfo schemas.MASInfo) (ret masStorage) {
ret
.
agencyCounter
=
masInfo
.
Agencies
.
Counter
ret
.
graph
=
masInfo
.
Graph
ret
.
spec
.
ID
=
masID
ret
.
id
=
masID
ret
.
agents
=
make
([]
schemas
.
AgentInfo
,
ret
.
agentCounter
,
ret
.
agentCounter
)
for
i
:=
0
;
i
<
ret
.
agentCounter
;
i
++
{
ret
.
agents
[
i
]
.
Spec
=
masInfo
.
Agents
.
Instances
[
i
]
ret
.
agents
[
i
]
.
Spec
.
MASID
=
masID
ret
.
agents
[
i
]
.
Spec
=
masInfo
.
Agents
.
Instances
[
i
]
.
Spec
ret
.
agents
[
i
]
.
MASID
=
masID
ret
.
agents
[
i
]
.
Address
.
Agency
=
"mas-"
+
strconv
.
Itoa
(
masID
)
+
"-agency-"
+
strconv
.
Itoa
(
ret
.
agents
[
i
]
.
Spec
.
AgencyID
)
+
".mas"
+
strconv
.
Itoa
(
masID
)
+
"agencies"
strconv
.
Itoa
(
ret
.
agents
[
i
]
.
AgencyID
)
+
".mas"
+
strconv
.
Itoa
(
masID
)
+
"agencies"
}
ret
.
agencies
=
make
([]
schemas
.
AgencyInfo
,
ret
.
agencyCounter
,
ret
.
agencyCounter
)
for
i
:=
0
;
i
<
ret
.
agencyCounter
;
i
++
{
ret
.
agencies
[
i
]
.
Spec
=
masInfo
.
Agencies
.
Instances
[
i
]
ret
.
agencies
[
i
]
.
Spec
=
masInfo
.
Agencies
.
Instances
[
i
]
.
Spec
ret
.
agencies
[
i
]
.
Spec
.
MASID
=
masID
ret
.
agencies
[
i
]
.
Spec
.
Name
=
"mas-"
+
strconv
.
Itoa
(
masID
)
+
"-agency-"
+
strconv
.
Itoa
(
i
)
+
".mas"
+
strconv
.
Itoa
(
masID
)
+
"agencies"
...
...
pkg/schemas/core.go
View file @
3c4011be
...
...
@@ -56,25 +56,48 @@ type CloneMAP struct {
Uptime
time
.
Time
`json:"uptime,omitempty"`
// uptime of clonemap instance
}
// MASInfo contains info about MAS spec, agents and agencies in MAS
type
MASInfo
struct
{
Spec
MASSpec
`json:"spec"`
ID
int
`json:"id"`
Agents
Agents
`json:"agents"`
Agencies
Agencies
`json:"agencies"`
Status
Status
`json:"status"`
Graph
Graph
`json:"graph"`
}
// MASSpec contains information about a MAS running in clonemap
type
MASSpec
struct
{
ID
int
`json:"id"`
// unique ID of MAS
//
ID int `json:"id"` // unique ID of MAS
Name
string
`json:"name,omitempty"`
// name/description of MAS
NumAgentsPerAgency
int
`json:"agentsperagency,omitempty"`
// number of agents per agency
Logging
bool
`json:"logging"`
// switch for logging module
// Analysis bool `json:"analysis"` // switch for analysis logging
MQTT
bool
`json:"mqtt"`
//switch for mqtt
DF
bool
`json:"df"`
//switch for df
Logger
LogConfig
`json:"log"`
// logger configuration
Uptime
time
.
Time
`json:"uptime"`
MQTT
bool
`json:"mqtt"`
//switch for mqtt
DF
bool
`json:"df"`
//switch for df
Logger
LogConfig
`json:"log"`
// logger configuration
Uptime
time
.
Time
`json:"uptime"`
Agents
[]
AgentSpec
`json:"agents"`
Graph
Graph
`json:"graph"`
}
// AgentInfo contains information about agent spec, address, communication, mqtt and status
type
AgentInfo
struct
{
Spec
AgentSpec
`json:"spec"`
MASID
int
`json:"masid"`
// ID of MAS
AgencyID
int
`json:"agencyid"`
// name of the agency
ImageID
int
`json:"imid"`
// ID of agency image
ID
int
`json:"id"`
// ID of agent
Address
Address
`json:"address"`
Status
Status
`json:"status"`
}
// AgentSpec contains information about a agent running in a MAS
type
AgentSpec
struct
{
MASID
int
`json:"masid"`
// ID of MAS
AgencyID
int
`json:"agencyid"`
// name of the agency
NodeID
int
`json:"nodeid"`
// id of the node the agent is attached to
ID
int
`json:"id"`
// unique ID of agent
//
MASID int `json:"masid"` // ID of MAS
//
AgencyID int `json:"agencyid"` // name of the agency
NodeID
int
`json:"nodeid"`
// id of the node the agent is attached to
//
ID int `json:"id"` // unique ID of agent
AgencyImage
string
`json:"image"`
// docker image to be used for agencies
ImagePullSecret
string
`json:"secret,omitempty"`
// image pull secret
Name
string
`json:"name,omitempty"`
// name/description of agent
...
...
@@ -88,25 +111,66 @@ type Address struct {
Agency
string
`json:"agency"`
}
// // MQTT contains information regarding MQTT topics of agents
// type MQTT struct {
// SubTopics []string // list of topics the agent has/should subscribed to
// PubTopics []string // list of topics the agent can publish
// }
// Status contains information about an agent's or agency's status
type
Status
struct
{
Code
int
`json:"code"`
// status code
LastUpdate
time
.
Time
`json:"lastupdate"`
// time of last update
}
// AgencyInfo contains information about agency spec and status
type
AgencyInfo
struct
{
Spec
AgencySpec
`json:"spec"`
Status
Status
`json:"status"`
}
// AgencySpec contains information about agency
type
AgencySpec
struct
{
MASID
int
`json:"masid"`
// ID of MAS
Name
string
`json:"name"`
// name of agency (hostname of pod given by kubernetes)
ID
int
`json:"id"`
// unique ID (contained in name)
Logger
LogConfig
`json:"log"`
// logger configuration
Agents
[]
int
`json:"agents"`
MASID
int
`json:"masid"`
// ID of MAS
Name
string
`json:"name"`
// name of agency (hostname of pod given by kubernetes)
ID
int
`json:"id"`
// unique ID (contained in name)
Logger
LogConfig
`json:"log"`
// logger configuration
Agents
[]
AgentInfo
`json:"agents"`
}