2 ==================================================================================
3 Copyright (c) 2021 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"
36 a1PolicyPrefix = "a1.policy_type."
37 a1MediatorNs = "A1m_ns"
40 var typeAlreadyError = errors.New("Policy Type already exists")
41 var typeMismatchError = errors.New("Policytype Mismatch")
43 func (rh *Resthook) IsTypeAlready(err error) bool {
44 return err == typeAlreadyError
46 func (rh *Resthook) IsTypeMismatch(err error) bool {
47 return err == typeMismatchError
49 func NewResthook() *Resthook {
50 return createResthook(sdlgo.NewSyncStorage())
53 func createResthook(sdlInst iSdl) *Resthook {
59 func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
61 var policyTypeIDs []models.PolicyTypeID
63 keys, err := rh.db.GetAll("A1m_ns")
66 a1.Logger.Error("error in retrieving policy. err: %v", err)
69 a1.Logger.Debug("keys : %+v", keys)
71 for _, key := range keys {
72 if strings.HasPrefix(strings.TrimLeft(key, " "), a1PolicyPrefix) {
73 pti := strings.Split(strings.Trim(key, " "), a1PolicyPrefix)[1]
74 ptii, _ := strconv.ParseInt(pti, 10, 64)
75 policyTypeIDs = append(policyTypeIDs, models.PolicyTypeID(ptii))
79 a1.Logger.Debug("return : %+v", policyTypeIDs)
83 func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.PolicyTypeSchema {
84 a1.Logger.Debug("GetPolicyType1")
86 var policytypeschema *models.PolicyTypeSchema
89 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
92 a1.Logger.Debug("key : %+v", key)
94 valmap, err := rh.db.Get(a1MediatorNs, keys[:])
96 a1.Logger.Debug("policytype map : ", valmap)
99 a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
100 return policytypeschema
104 a1.Logger.Error("error in retrieving policy type. err: %v", err)
105 return policytypeschema
108 if valmap[key] == nil {
109 a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
110 return policytypeschema
113 a1.Logger.Debug("keysmap : %+v", valmap[key])
115 var item models.PolicyTypeSchema
116 valStr := fmt.Sprint(valmap[key])
118 a1.Logger.Debug("Policy type for %+v : %+v", key, valStr)
119 valkey := "`" + valStr + "`"
120 valToUnmarshall, err := strconv.Unquote(valkey)
125 a1.Logger.Debug("Policy type for %+v : %+v", key, string(b))
126 errunm := json.Unmarshal([]byte(valToUnmarshall), &item)
128 a1.Logger.Debug(" Unmarshalled json : %+v", (errunm))
129 a1.Logger.Debug("Policy type Name : %v", (item.Name))
134 func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error {
135 a1.Logger.Debug("CreatePolicyType function")
136 if policyTypeId != models.PolicyTypeID(*httprequest.PolicyTypeID) {
138 a1.Logger.Debug("Policytype Mismatch")
139 return typeMismatchError
141 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
142 a1.Logger.Debug("key %+v ", key)
143 if data, err := httprequest.MarshalBinary(); err == nil {
144 a1.Logger.Debug("Marshaled String : %+v", string(data))
145 success, err1 := rh.db.SetIfNotExists(a1MediatorNs, key, string(data))
146 a1.Logger.Info("success:%+v", success)
148 a1.Logger.Error("error :%+v", err1)
152 a1.Logger.Debug("Policy type %+v already exist", policyTypeId)
153 return typeAlreadyError