blob: 15f325b57bbf1e6e34e6aa0684c8252150265c1c [file] [log] [blame]
naman.gupta6d6fe012022-11-30 15:21:16 +05301/*
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
22package policy
23
24import (
naman.gupta857a9532022-11-30 23:03:35 +053025 "errors"
26 "fmt"
naman.gupta6d6fe012022-11-30 15:21:16 +053027 "strconv"
naman.gupta857a9532022-11-30 23:03:35 +053028 "strings"
naman.gupta6d6fe012022-11-30 15:21:16 +053029
30 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
naman.gupta857a9532022-11-30 23:03:35 +053031 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
naman.gupta6d6fe012022-11-30 15:21:16 +053032 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
33)
34
naman.gupta857a9532022-11-30 23:03:35 +053035var policyTypeNotFoundError = errors.New("Policy Type Not Found")
36var policyInstanceNotFoundError = errors.New("Policy Instance Not Found")
37
naman.gupta6d6fe012022-11-30 15:21:16 +053038const (
naman.gupta857a9532022-11-30 23:03:35 +053039 a1HandlerPrefix = "a1.policy_handler."
40 a1PolicyPrefix = "a1.policy_type."
41 a1MediatorNs = "A1m_ns"
42 a1InstancePrefix = "a1.policy_instance."
naman.gupta6d6fe012022-11-30 15:21:16 +053043)
44
45func NewPolicyManager(sdl *sdlgo.SyncStorage) *PolicyManager {
46 return createPolicyManager(sdl)
47}
48
49func createPolicyManager(sdlInst iSdl) *PolicyManager {
50 pm := &PolicyManager{
51 db: sdlInst,
52 }
53 return pm
54}
55func (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.gupta857a9532022-11-30 23:03:35 +053065
66func (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
96func (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}