efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 1 | # ============LICENSE_START=================================================== |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 2 | # Copyright (C) 2019-2020 Nordix Foundation. |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 3 | # ============================================================================ |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain 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, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # SPDX-License-Identifier: Apache-2.0 |
| 17 | # ============LICENSE_END===================================================== |
| 18 | import re |
| 19 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 20 | import mod.pmsh_logging as logger |
| 21 | from mod import db |
| 22 | from mod.db_models import SubscriptionModel, NfSubRelationalModel |
| 23 | |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 24 | |
| 25 | class Subscription: |
| 26 | def __init__(self, **kwargs): |
| 27 | self.subscriptionName = kwargs.get('subscriptionName') |
| 28 | self.administrativeState = kwargs.get('administrativeState') |
| 29 | self.fileBasedGP = kwargs.get('fileBasedGP') |
| 30 | self.fileLocation = kwargs.get('fileLocation') |
| 31 | self.nfTypeModelInvariantId = kwargs.get('nfTypeModelInvariantId') |
| 32 | self.nfFilter = kwargs.get('nfFilter') |
| 33 | self.measurementGroups = kwargs.get('measurementGroups') |
| 34 | |
| 35 | def prepare_subscription_event(self, xnf_name): |
| 36 | """Prepare the sub event for publishing |
| 37 | |
| 38 | Args: |
| 39 | xnf_name: the AAI xnf name. |
| 40 | |
| 41 | Returns: |
| 42 | dict: the Subscription event to be published. |
| 43 | """ |
| 44 | clean_sub = {k: v for k, v in self.__dict__.items() if k != 'nfFilter'} |
| 45 | clean_sub.update({'nfName': xnf_name, 'policyName': f'OP-{self.subscriptionName}'}) |
| 46 | return clean_sub |
| 47 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 48 | def create(self): |
| 49 | """ Creates a subscription database entry |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 50 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 51 | Returns: |
| 52 | Subscription object |
| 53 | """ |
| 54 | existing_subscription = (SubscriptionModel.query.filter( |
| 55 | SubscriptionModel.subscription_name == self.subscriptionName).one_or_none()) |
| 56 | |
| 57 | if existing_subscription is None: |
| 58 | new_subscription = SubscriptionModel(subscription_name=self.subscriptionName, |
| 59 | status=self.administrativeState) |
| 60 | |
| 61 | db.session.add(new_subscription) |
| 62 | db.session.commit() |
| 63 | |
| 64 | return new_subscription |
| 65 | |
| 66 | else: |
| 67 | logger.debug(f'Subscription {self.subscriptionName} already exists,' |
| 68 | f' returning this subscription..') |
| 69 | return existing_subscription |
| 70 | |
| 71 | def add_network_functions_to_subscription(self, nf_list): |
| 72 | """ Associates network functions to a Subscription |
| 73 | |
| 74 | Args: |
| 75 | nf_list : A list of NetworkFunction objects. |
| 76 | """ |
| 77 | current_sub = self.create() |
| 78 | logger.debug(f'Adding network functions to subscription {current_sub.subscription_name}') |
| 79 | |
| 80 | for nf in nf_list: |
| 81 | current_nf = nf.create() |
| 82 | |
| 83 | existing_entry = NfSubRelationalModel.query.filter( |
| 84 | NfSubRelationalModel.subscription_name == current_sub.subscription_name, |
| 85 | NfSubRelationalModel.nf_name == current_nf.nf_name).one_or_none() |
| 86 | if existing_entry is None: |
| 87 | new_nf_sub = NfSubRelationalModel(current_sub.subscription_name, nf.nf_name) |
| 88 | new_nf_sub.nf = current_nf |
| 89 | logger.debug(current_nf) |
| 90 | current_sub.nfs.append(new_nf_sub) |
| 91 | |
| 92 | db.session.add(current_sub) |
| 93 | db.session.commit() |
| 94 | |
| 95 | @staticmethod |
| 96 | def get(subscription_name): |
| 97 | """ Retrieves a subscription |
| 98 | |
| 99 | Args: |
| 100 | subscription_name (str): The subscription name |
| 101 | |
| 102 | Returns: |
| 103 | Subscription object else None |
| 104 | """ |
| 105 | return SubscriptionModel.query.filter( |
| 106 | SubscriptionModel.subscription_name == subscription_name).one_or_none() |
| 107 | |
| 108 | @staticmethod |
| 109 | def get_all(): |
| 110 | """ Retrieves a list of subscriptions |
| 111 | |
| 112 | Returns: |
| 113 | list: Subscription list else empty |
| 114 | """ |
| 115 | return SubscriptionModel.query.all() |
| 116 | |
| 117 | @staticmethod |
| 118 | def get_all_nfs_subscription_relations(): |
| 119 | """ Retrieves all network function to subscription relations |
| 120 | |
| 121 | Returns: |
| 122 | list: NetworkFunctions per Subscription list else empty |
| 123 | """ |
| 124 | nf_per_subscriptions = NfSubRelationalModel.query.all() |
| 125 | |
| 126 | return nf_per_subscriptions |
| 127 | |
| 128 | |
| 129 | class NetworkFunctionFilter: |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 130 | def __init__(self, **kwargs): |
| 131 | self.nf_sw_version = kwargs.get('swVersions') |
| 132 | self.nf_names = kwargs.get('nfNames') |
| 133 | self.regex_matcher = re.compile('|'.join(raw_regex for raw_regex in self.nf_names)) |
| 134 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 135 | def is_nf_in_filter(self, nf_name): |
| 136 | """Match the nf name against regex values in Subscription.nfFilter.nfNames |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 137 | |
| 138 | Args: |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 139 | nf_name: the AAI nf name. |
efiacor | bbe05d8 | 2019-12-11 12:00:26 +0000 | [diff] [blame] | 140 | |
| 141 | Returns: |
| 142 | bool: True if matched, else False. |
| 143 | """ |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 144 | return self.regex_matcher.search(nf_name) |