0a832ff9e605704ec7202c71ee9dd8fed71f86c2
[ric-plt/a1.git] / a1-go / pkg / resthooks / resthooks.go
1 /*
2 ==================================================================================
3   Copyright (c) 2021 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 package resthooks
22
23 import (
24         "errors"
25         "strconv"
26         "strings"
27
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"
31 )
32
33 const (
34         a1PolicyPrefix = "a1.policy_type."
35         a1MediatorNs   = "A1m_ns"
36 )
37
38 var typeAlreadyError = errors.New("Policy Type already exists")
39 var typeMismatchError = errors.New("Policytype Mismatch")
40
41 func (rh *Resthook) IsTypeAlready(err error) bool {
42         return err == typeAlreadyError
43 }
44 func (rh *Resthook) IsTypeMismatch(err error) bool {
45         return err == typeMismatchError
46 }
47 func NewResthook() *Resthook {
48         return createResthook(sdlgo.NewSyncStorage())
49 }
50
51 func createResthook(sdlInst iSdl) *Resthook {
52         return &Resthook{
53                 db: sdlInst,
54         }
55 }
56
57 func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
58
59         var policyTypeIDs []models.PolicyTypeID
60
61         keys, err := rh.db.GetAll("A1m_ns")
62
63         if err != nil {
64                 a1.Logger.Error("error in retrieving policy. err: %v", err)
65                 return policyTypeIDs
66         }
67         a1.Logger.Debug("keys : %+v", keys)
68
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))
74                 }
75         }
76
77         a1.Logger.Debug("return : %+v", policyTypeIDs)
78         return policyTypeIDs
79 }
80
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) {
84                 //error message
85                 a1.Logger.Debug("Policytype Mismatch")
86                 return typeMismatchError
87         }
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)
94                 if err1 != nil {
95                         a1.Logger.Error("error :%+v", err1)
96                         return err1
97                 }
98                 if !success {
99                         a1.Logger.Debug("Policy type %+v already exist", policyTypeId)
100                         return typeAlreadyError
101                 }
102         }
103         return nil
104 }