2 ==================================================================================
3 Copyright (c) 2022 Samsung
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
17 This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 platform project (RICP).
19 ==================================================================================
30 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
31 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
32 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
35 var policyTypeNotFoundError = errors.New("Policy Type Not Found")
36 var policyInstanceNotFoundError = errors.New("Policy Instance Not Found")
39 a1HandlerPrefix = "a1.policy_handler."
40 a1PolicyPrefix = "a1.policy_type."
41 a1MediatorNs = "A1m_ns"
42 a1InstancePrefix = "a1.policy_instance."
45 func NewPolicyManager(sdl *sdlgo.SyncStorage) *PolicyManager {
46 return createPolicyManager(sdl)
49 func createPolicyManager(sdlInst iSdl) *PolicyManager {
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)
60 a1.Logger.Error("error1 :%+v", err)
66 func (im *PolicyManager) GetAllPolicyInstance(policyTypeId int) ([]models.PolicyInstanceID, error) {
67 a1.Logger.Debug("GetAllPolicyInstance")
68 var policyTypeInstances = []models.PolicyInstanceID{}
70 keys, err := im.db.GetAll("A1m_ns")
73 a1.Logger.Error("error in retrieving policy. err: %v", err)
74 return policyTypeInstances, err
76 a1.Logger.Debug("keys : %+v", keys)
77 typekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "."
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))
87 if len(policyTypeInstances) == 0 {
88 a1.Logger.Debug("policy instance Not Present ")
89 return policyTypeInstances, policyInstanceNotFoundError
92 a1.Logger.Debug("return : %+v", policyTypeInstances)
93 return policyTypeInstances, nil
96 func (im *PolicyManager) GetPolicyInstance(policyTypeId models.PolicyTypeID, policyInstanceID models.PolicyInstanceID) (interface{}, error) {
97 a1.Logger.Debug("GetPolicyInstance1")
101 typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
104 a1.Logger.Debug("key1 : %+v", typekey)
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
113 a1.Logger.Error("error in retrieving policy type. err: %v", err)
117 if valmap[typekey] == nil {
118 a1.Logger.Debug("policy type Not Present for policyid : %v", policyTypeId)
119 return nil, policyTypeNotFoundError
122 a1.Logger.Debug("keysmap : %+v", valmap[typekey])
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[:])
129 a1.Logger.Error("policy instance error : %v", err)
132 a1.Logger.Debug("policyinstancetype map : %+v", instanceMap)
134 if instanceMap[instancekey] == nil {
135 a1.Logger.Debug("policy instance Not Present for policyinstaneid : %v", policyInstanceID)
136 return nil, policyInstanceNotFoundError
139 valStr := fmt.Sprint(instanceMap[instancekey])