Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
OSM Packages
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
17
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vnf-onboarding
OSM Packages
Merge requests
!190
Fix bug 1987
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Fix bug 1987
fix-bug-1987
into
master
Overview
0
Commits
1
Pipelines
18
Changes
44
Merged
garciadav
requested to merge
fix-bug-1987
into
master
2 years ago
Overview
0
Commits
1
Pipelines
18
Changes
3
Expand
0
0
Merge request reports
Compare
version 4
version 8
943215c7
2 years ago
version 7
649fc52b
2 years ago
version 6
10c5fec6
2 years ago
version 5
29d261eb
2 years ago
version 4
4d420a7d
2 years ago
version 3
0eed5e2c
2 years ago
version 2
65bdbe47
2 years ago
version 1
dd0aeb5d
2 years ago
master (base)
and
version 5
latest version
d433b37c
1 commit,
2 years ago
version 8
943215c7
1 commit,
2 years ago
version 7
649fc52b
1 commit,
2 years ago
version 6
10c5fec6
1 commit,
2 years ago
version 5
29d261eb
1 commit,
2 years ago
version 4
4d420a7d
1 commit,
2 years ago
version 3
0eed5e2c
1 commit,
2 years ago
version 2
65bdbe47
1 commit,
2 years ago
version 1
dd0aeb5d
1 commit,
2 years ago
Show latest version
3 files
+
3
−
3
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
nscharm_ns/charms/ops/ns-operator/src/charm.py
0 → 100755
+
134
−
0
Options
#!/usr/bin/env python3
# Copyright 2020 David Garcia
# See LICENSE file for licensing details.
import
json
import
logging
import
traceback
from
ops.main
import
main
from
ops.charm
import
CharmBase
from
ops.model
import
ActiveStatus
logger
=
logging
.
getLogger
(
__name__
)
class
VnfPolicyCharm
(
CharmBase
):
def
__init__
(
self
,
*
args
):
super
().
__init__
(
*
args
)
# Charm events
self
.
framework
.
observe
(
self
.
on
.
install
,
self
.
_on_install
)
self
.
framework
.
observe
(
self
.
on
.
add_user_action
,
self
.
_on_add_user_action
)
def
_on_install
(
self
,
_
):
import
subprocess
subprocess
.
run
([
"
apt
"
,
"
install
"
,
"
python3.8
"
,
"
-y
"
])
subprocess
.
run
([
"
rm
"
,
"
/usr/bin/python3
"
])
subprocess
.
run
([
"
ln
"
,
"
-s
"
,
"
/usr/bin/python3.8
"
,
"
/usr/bin/python3
"
])
self
.
unit
.
status
=
ActiveStatus
()
def
_on_add_user_action
(
self
,
event
):
"""
Add user action.
"""
from
ns
import
NetworkService
err
=
""
output
=
""
try
:
username
=
event
.
params
[
"
username
"
]
bw
=
event
.
params
.
get
(
"
bw
"
)
qos
=
event
.
params
.
get
(
"
qos
"
)
tariff
=
event
.
params
.
get
(
"
tariff
"
)
client
=
NetworkService
(
user
=
self
.
config
[
"
juju-username
"
],
secret
=
self
.
config
[
"
juju-password
"
],
)
user_id
=
self
.
_add_user
(
client
,
username
,
tariff
)
if
user_id
>
0
:
success
=
self
.
_set_policy
(
client
,
user_id
,
bw
,
qos
)
else
:
logger
.
debug
(
"
user_id is 0; add_user failed.
"
)
logger
.
debug
(
"
Output from charm: {}
"
.
format
(
output
))
event
.
set_results
(
{
"
user-id
"
:
user_id
,
"
policy-set
"
:
success
,
}
)
except
Exception
as
err
:
logger
.
error
(
str
(
err
))
logger
.
debug
(
str
(
traceback
.
format_exc
()))
event
.
fail
(
str
(
err
))
def
_add_user
(
self
,
client
,
username
,
tariff
):
"""
Add a user to the database and return the id.
"""
ns_config_info
=
json
.
loads
(
self
.
config
[
"
ns_config_info
"
])
application
=
ns_config_info
[
"
osm-config-mapping
"
][
"
{}.{}.{}
"
.
format
(
"
1
"
,
# member-vnf-index
"
userVM
"
,
# vdu_id
"
0
"
,
# vdu_count_index
)
]
output
=
client
.
ExecutePrimitiveGetOutput
(
# The name of the application for adding a user
application
,
# The name of the action to call
"
add-user
"
,
# The parameter(s) required by the above charm and action
params
=
{
"
username
"
:
username
,
"
tariff
"
:
tariff
,
},
# How long to wait (in seconds) for the action to finish
timeout
=
500
,
)
# Get the output from the `add-user` function
user_id
=
int
(
output
[
"
user-id
"
])
return
user_id
def
_set_policy
(
self
,
client
,
user_id
,
bw
,
qos
):
"""
Set the policy for a user.
"""
success
=
False
ns_config_info
=
json
.
loads
(
self
.
config
[
"
ns_config_info
"
])
application
=
ns_config_info
[
"
osm-config-mapping
"
][
"
{}.{}.{}
"
.
format
(
"
2
"
,
# member-vnf-index
"
policyVM
"
,
# vdu_id
"
0
"
,
# vdu_count_index
)
]
success
=
client
.
ExecutePrimitiveGetOutput
(
# The name of the application for policy management
application
,
# The name of the action to call
"
set-policy
"
,
# The parameter(s) required by the above charm and action
params
=
{
"
user_id
"
:
user_id
,
"
bw
"
:
bw
,
"
qos
"
:
qos
,
},
# How long to wait (in seconds) for the action to finish
timeout
=
500
,
)
# Get the output from the `add-user` function
return
success
.
get
(
"
updated
"
,
False
)
if
__name__
==
"
__main__
"
:
main
(
VnfPolicyCharm
)
Loading