naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2022 Samsung |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | platform project (RICP). |
| 19 | ================================================================================== |
| 20 | */ |
| 21 | |
| 22 | package policy |
| 23 | |
| 24 | import ( |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 25 | "errors" |
| 26 | "fmt" |
naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 27 | "strconv" |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 28 | "strings" |
naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 29 | |
| 30 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 31 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" |
naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 32 | "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" |
| 33 | ) |
| 34 | |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 35 | var policyTypeNotFoundError = errors.New("Policy Type Not Found") |
| 36 | var policyInstanceNotFoundError = errors.New("Policy Instance Not Found") |
| 37 | |
naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 38 | const ( |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 39 | a1HandlerPrefix = "a1.policy_handler." |
| 40 | a1PolicyPrefix = "a1.policy_type." |
| 41 | a1MediatorNs = "A1m_ns" |
| 42 | a1InstancePrefix = "a1.policy_instance." |
naman.gupta | 6d6fe01 | 2022-11-30 15:21:16 +0530 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | func NewPolicyManager(sdl *sdlgo.SyncStorage) *PolicyManager { |
| 46 | return createPolicyManager(sdl) |
| 47 | } |
| 48 | |
| 49 | func createPolicyManager(sdlInst iSdl) *PolicyManager { |
| 50 | pm := &PolicyManager{ |
| 51 | db: sdlInst, |
| 52 | } |
| 53 | return pm |
| 54 | } |
| 55 | func (pm *PolicyManager) SetPolicyInstanceStatus(policyTypeId int, policyInstanceID int, status string) error { |
| 56 | a1.Logger.Debug("message recieved for %d and %d", policyTypeId, policyInstanceID) |
| 57 | instancehandlerKey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + strconv.FormatInt((int64(policyInstanceID)), 10) |
| 58 | err := pm.db.Set(a1MediatorNs, instancehandlerKey, status) |
| 59 | if err != nil { |
| 60 | a1.Logger.Error("error1 :%+v", err) |
| 61 | return err |
| 62 | } |
| 63 | return nil |
| 64 | } |
naman.gupta | 857a953 | 2022-11-30 23:03:35 +0530 | [diff] [blame] | 65 | |
| 66 | func (im *PolicyManager) GetAllPolicyInstance(policyTypeId int) ([]models.PolicyInstanceID, error) { |
| 67 | a1.Logger.Debug("GetAllPolicyInstance") |
| 68 | var policyTypeInstances = []models.PolicyInstanceID{} |
| 69 | |
| 70 | keys, err := im.db.GetAll("A1m_ns") |
| 71 | |
| 72 | if err != nil { |
| 73 | a1.Logger.Error("error in retrieving policy. err: %v", err) |
| 74 | return policyTypeInstances, err |
| 75 | } |
| 76 | a1.Logger.Debug("keys : %+v", keys) |
| 77 | typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." |
| 78 | |
| 79 | for _, key := range keys { |
| 80 | if strings.HasPrefix(strings.TrimLeft(key, " "), typekey) { |
| 81 | pti := strings.Split(strings.Trim(key, " "), typekey)[1] |
| 82 | a1.Logger.Debug("pti %+v", pti) |
| 83 | policyTypeInstances = append(policyTypeInstances, models.PolicyInstanceID(pti)) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if len(policyTypeInstances) == 0 { |
| 88 | a1.Logger.Debug("policy instance Not Present ") |
| 89 | return policyTypeInstances, policyInstanceNotFoundError |
| 90 | } |
| 91 | |
| 92 | a1.Logger.Debug("return : %+v", policyTypeInstances) |
| 93 | return policyTypeInstances, nil |
| 94 | } |
| 95 | |
| 96 | func (im *PolicyManager) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) (interface{}, error) { |
| 97 | a1.Logger.Debug("GetPolicyInstance1") |
| 98 | |
| 99 | var keys [1]string |
| 100 | |
| 101 | typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) |
| 102 | keys[0] = typekey |
| 103 | |
| 104 | a1.Logger.Debug("key1 : %+v", typekey) |
| 105 | |
| 106 | valmap, err := im.db.Get(a1MediatorNs, keys[:]) |
| 107 | if len(valmap) == 0 { |
| 108 | a1.Logger.Debug("policy type Not Present for policyid : %v", policyTypeId) |
| 109 | return nil, policyTypeNotFoundError |
| 110 | } |
| 111 | |
| 112 | if err != nil { |
| 113 | a1.Logger.Error("error in retrieving policy type. err: %v", err) |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | if valmap[typekey] == nil { |
| 118 | a1.Logger.Debug("policy type Not Present for policyid : %v", policyTypeId) |
| 119 | return nil, policyTypeNotFoundError |
| 120 | } |
| 121 | |
| 122 | a1.Logger.Debug("keysmap : %+v", valmap[typekey]) |
| 123 | |
| 124 | instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID) |
| 125 | a1.Logger.Debug("key2 : %+v", instancekey) |
| 126 | keys[0] = instancekey |
| 127 | instanceMap, err := im.db.Get(a1MediatorNs, keys[:]) |
| 128 | if err != nil { |
| 129 | a1.Logger.Error("policy instance error : %v", err) |
| 130 | return nil, err |
| 131 | } |
| 132 | a1.Logger.Debug("policyinstancetype map : %+v", instanceMap) |
| 133 | |
| 134 | if instanceMap[instancekey] == nil { |
| 135 | a1.Logger.Debug("policy instance Not Present for policyinstaneid : %v", policyInstanceID) |
| 136 | return nil, policyInstanceNotFoundError |
| 137 | } |
| 138 | |
| 139 | valStr := fmt.Sprint(instanceMap[instancekey]) |
| 140 | return valStr, nil |
| 141 | } |