| 1 |
# Copyright 2017 FUJITSU LIMITED |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
|
| 18 |
"""VPN Utilities and helper functions.""" |
| 19 |
|
| 20 |
|
| 21 |
from neutronclient._i18n import _ |
| 22 |
from neutronclient.common import exceptions |
| 23 |
|
| 24 |
DPD_SUPPORTED_ACTIONS = ['hold', 'clear', 'restart', |
| 25 |
'restart-by-peer', 'disabled'] |
| 26 |
DPD_SUPPORTED_KEYS = ['action', 'interval', 'timeout'] |
| 27 |
|
| 28 |
lifetime_keys = ['units', 'value'] |
| 29 |
lifetime_units = ['seconds'] |
| 30 |
|
| 31 |
|
| 32 |
def validate_dpd_dict(dpd_dict): |
| 33 |
for key, value in dpd_dict.items(): |
| 34 |
if key not in DPD_SUPPORTED_KEYS: |
| 35 |
message = _( |
| 36 |
"DPD Dictionary KeyError: " |
| 37 |
"Reason-Invalid DPD key : " |
| 38 |
"'%(key)s' not in %(supported_key)s") % { |
| 39 |
'key': key, 'supported_key': DPD_SUPPORTED_KEYS} |
| 40 |
raise exceptions.CommandError(message) |
| 41 |
if key == 'action' and value not in DPD_SUPPORTED_ACTIONS: |
| 42 |
message = _( |
| 43 |
"DPD Dictionary ValueError: " |
| 44 |
"Reason-Invalid DPD action : " |
| 45 |
"'%(key_value)s' not in %(supported_action)s") % { |
| 46 |
'key_value': value, |
| 47 |
'supported_action': DPD_SUPPORTED_ACTIONS} |
| 48 |
raise exceptions.CommandError(message) |
| 49 |
if key in ('interval', 'timeout'): |
| 50 |
try: |
| 51 |
if int(value) <= 0: |
| 52 |
raise ValueError() |
| 53 |
except ValueError: |
| 54 |
message = _( |
| 55 |
"DPD Dictionary ValueError: " |
| 56 |
"Reason-Invalid positive integer value: " |
| 57 |
"'%(key)s' = %(value)s") % { |
| 58 |
'key': key, 'value': value} |
| 59 |
raise exceptions.CommandError(message) |
| 60 |
else: |
| 61 |
dpd_dict[key] = int(value) |
| 62 |
return |
| 63 |
|
| 64 |
|
| 65 |
def validate_lifetime_dict(lifetime_dict): |
| 66 |
|
| 67 |
for key, value in lifetime_dict.items(): |
| 68 |
if key not in lifetime_keys: |
| 69 |
message = _( |
| 70 |
"Lifetime Dictionary KeyError: " |
| 71 |
"Reason-Invalid unit key : " |
| 72 |
"'%(key)s' not in %(supported_key)s") % { |
| 73 |
'key': key, 'supported_key': lifetime_keys} |
| 74 |
raise exceptions.CommandError(message) |
| 75 |
if key == 'units' and value not in lifetime_units: |
| 76 |
message = _( |
| 77 |
"Lifetime Dictionary ValueError: " |
| 78 |
"Reason-Invalid units : " |
| 79 |
"'%(key_value)s' not in %(supported_units)s") % { |
| 80 |
'key_value': key, 'supported_units': lifetime_units} |
| 81 |
raise exceptions.CommandError(message) |
| 82 |
if key == 'value': |
| 83 |
try: |
| 84 |
if int(value) < 60: |
| 85 |
raise ValueError() |
| 86 |
except ValueError: |
| 87 |
message = _( |
| 88 |
"Lifetime Dictionary ValueError: " |
| 89 |
"Reason-Invalid value should be at least 60:" |
| 90 |
"'%(key_value)s' = %(value)s") % { |
| 91 |
'key_value': key, 'value': value} |
| 92 |
raise exceptions.CommandError(message) |
| 93 |
else: |
| 94 |
lifetime_dict['value'] = int(value) |
| 95 |
return |
| 96 |
|
| 97 |
|
| 98 |
def lifetime_help(policy): |
| 99 |
lifetime = _("%s lifetime attributes. " |
| 100 |
"'units'-seconds, default:seconds. " |
| 101 |
"'value'-non negative integer, default:3600.") % policy |
| 102 |
return lifetime |
| 103 |
|
| 104 |
|
| 105 |
def dpd_help(policy): |
| 106 |
dpd = _(" %s Dead Peer Detection attributes." |
| 107 |
" 'action'-hold,clear,disabled,restart,restart-by-peer." |
| 108 |
" 'interval' and 'timeout' are non negative integers. " |
| 109 |
" 'interval' should be less than 'timeout' value. " |
| 110 |
" 'action', default:hold 'interval', default:30, " |
| 111 |
" 'timeout', default:120.") % policy.capitalize() |
| 112 |
return dpd |