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 ==================================================================================
28 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
29 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
30 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
34 a1PolicyPrefix = "a1.policy_type."
35 a1MediatorNs = "A1m_ns"
38 var typeAlreadyError = errors.New("Policy Type already exists")
39 var typeMismatchError = errors.New("Policytype Mismatch")
41 func (rh *Resthook) IsTypeAlready(err error) bool {
42 return err == typeAlreadyError
44 func (rh *Resthook) IsTypeMismatch(err error) bool {
45 return err == typeMismatchError
47 func NewResthook() *Resthook {
48 return createResthook(sdlgo.NewSyncStorage())
51 func createResthook(sdlInst iSdl) *Resthook {
57 func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
59 var policyTypeIDs []models.PolicyTypeID
61 keys, err := rh.db.GetAll("A1m_ns")
64 a1.Logger.Error("error in retrieving policy. err: %v", err)
67 a1.Logger.Debug("keys : %+v", keys)
69 for _, key := range keys {
70 if strings.HasPrefix(strings.TrimLeft(key, " "), a1PolicyPrefix) {
71 pti := strings.Split(strings.Trim(key, " "), a1PolicyPrefix)[1]
72 ptii, _ := strconv.ParseInt(pti, 10, 64)
73 policyTypeIDs = append(policyTypeIDs, models.PolicyTypeID(ptii))
77 a1.Logger.Debug("return : %+v", policyTypeIDs)
81 func (rh *Resthook) CreatePolicyType(policyTypeId models.PolicyTypeID, httprequest models.PolicyTypeSchema) error {
82 a1.Logger.Debug("CreatePolicyType function")
83 if policyTypeId != models.PolicyTypeID(*httprequest.PolicyTypeID) {
85 a1.Logger.Debug("Policytype Mismatch")
86 return typeMismatchError
88 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
89 a1.Logger.Debug("key %+v ", key)
90 if data, err := httprequest.MarshalBinary(); err == nil {
91 a1.Logger.Debug("Marshaled String : %+v", string(data))
92 success, err1 := rh.db.SetIfNotExists(a1MediatorNs, key, string(data))
93 a1.Logger.Info("success:%+v", success)
95 a1.Logger.Error("error :%+v", err1)
99 a1.Logger.Debug("Policy type %+v already exist", policyTypeId)
100 return typeAlreadyError