Get Policy type Schema from policy id
[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         "encoding/json"
25         "errors"
26         "fmt"
27         "strconv"
28         "strings"
29
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"
33 )
34
35 const (
36         a1PolicyPrefix = "a1.policy_type."
37         a1MediatorNs   = "A1m_ns"
38 )
39
40 var typeAlreadyError = errors.New("Policy Type already exists")
41 var typeMismatchError = errors.New("Policytype Mismatch")
42
43 func (rh *Resthook) IsTypeAlready(err error) bool {
44         return err == typeAlreadyError
45 }
46 func (rh *Resthook) IsTypeMismatch(err error) bool {
47         return err == typeMismatchError
48 }
49 func NewResthook() *Resthook {
50         return createResthook(sdlgo.NewSyncStorage())
51 }
52
53 func createResthook(sdlInst iSdl) *Resthook {
54         return &Resthook{
55                 db: sdlInst,
56         }
57 }
58
59 func (rh *Resthook) GetAllPolicyType() []models.PolicyTypeID {
60
61         var policyTypeIDs []models.PolicyTypeID
62
63         keys, err := rh.db.GetAll("A1m_ns")
64
65         if err != nil {
66                 a1.Logger.Error("error in retrieving policy. err: %v", err)
67                 return policyTypeIDs
68         }
69         a1.Logger.Debug("keys : %+v", keys)
70
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))
76                 }
77         }
78
79         a1.Logger.Debug("return : %+v", policyTypeIDs)
80         return policyTypeIDs
81 }
82
83 func (rh *Resthook) GetPolicyType(policyTypeId models.PolicyTypeID) *models.PolicyTypeSchema {
84         a1.Logger.Debug("GetPolicyType1")
85
86         var policytypeschema *models.PolicyTypeSchema
87         var keys [1]string
88
89         key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
90         keys[0] = key
91
92         a1.Logger.Debug("key : %+v", key)
93
94         valmap, err := rh.db.Get(a1MediatorNs, keys[:])
95
96         a1.Logger.Debug("policytype map : ", valmap)
97
98         if len(valmap) == 0 {
99                 a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
100                 return policytypeschema
101         }
102
103         if err != nil {
104                 a1.Logger.Error("error in retrieving policy type. err: %v", err)
105                 return policytypeschema
106         }
107
108         if valmap[key] == nil {
109                 a1.Logger.Error("policy type Not Present for policyid : %v", policyTypeId)
110                 return policytypeschema
111         }
112
113         a1.Logger.Debug("keysmap : %+v", valmap[key])
114
115         var item models.PolicyTypeSchema
116         valStr := fmt.Sprint(valmap[key])
117
118         a1.Logger.Debug("Policy type for %+v :  %+v", key, valStr)
119         valkey := "`" + valStr + "`"
120         valToUnmarshall, err := strconv.Unquote(valkey)
121         if err != nil {
122                 panic(err)
123         }
124
125         a1.Logger.Debug("Policy type for %+v :  %+v", key, string(b))
126         errunm := json.Unmarshal([]byte(valToUnmarshall), &item)
127
128         a1.Logger.Debug(" Unmarshalled json : %+v", (errunm))
129         a1.Logger.Debug("Policy type Name :  %v", (item.Name))
130
131         return &item
132 }
133
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) {
137                 //error message
138                 a1.Logger.Debug("Policytype Mismatch")
139                 return typeMismatchError
140         }
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)
147                 if err1 != nil {
148                         a1.Logger.Error("error :%+v", err1)
149                         return err1
150                 }
151                 if !success {
152                         a1.Logger.Debug("Policy type %+v already exist", policyTypeId)
153                         return typeAlreadyError
154                 }
155         }
156         return nil
157 }