2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
27 "github.com/spf13/cobra"
35 getCmd.AddCommand(newNamespacesCmd(newDatabase))
38 func newNamespacesCmd(dbCreateCb DbCreateCb) *cobra.Command {
39 cmd := &cobra.Command{
41 Short: "List all the namespaces in database",
42 Long: "List all the namespaces in database",
43 Args: cobra.ExactArgs(0),
44 RunE: func(cmd *cobra.Command, args []string) error {
45 showWide, _ := cmd.Flags().GetBool("wide")
46 nsMap, err := runNamespaces(dbCreateCb)
48 cmd.PrintErrf("%s\n", buf.String())
52 printNamespacesWide(cmd, nsMap)
54 printNamespaces(cmd, nsMap)
60 cmd.Flags().BoolP("wide", "w", false, "Show SDL DB cluster address, namespace name and its keys count")
64 func runNamespaces(dbCreateCb DbCreateCb) (nsMap, error) {
66 for _, dbInst := range dbCreateCb().Instances {
67 keys, err := dbInst.Keys("*")
71 id, err := getServiceAddress(dbInst)
76 if _, ok := nsMap[id]; !ok {
77 nsMap[id] = make(nsKeyMap)
80 for _, key := range keys {
81 namespace, err := parseKeyNamespace(key)
85 if isUniqueNamespace(nsMap[id], namespace) {
86 nsMap[id][namespace] = 1
88 nsMap[id][namespace]++
95 func getServiceAddress(db iDatabase) (string, error) {
96 state, err := db.State()
100 return state.PrimaryDbState.GetAddress(), nil
103 func parseKeyNamespace(key string) (string, error) {
104 sIndex := strings.Index(key, "{")
106 return "", fmt.Errorf("Namespace parsing error, no '{' in key string '%s'", key)
108 str := key[sIndex+len("{"):]
109 eIndex := strings.Index(str, "}")
111 return "", fmt.Errorf("Namespace parsing error, no '}' in key string '%s'", key)
113 return str[:eIndex], nil
116 func isUniqueNamespace(namespaces nsKeyMap, newNs string) bool {
117 if _, ok := namespaces[newNs]; ok {
123 func printNamespaces(cmd *cobra.Command, nsMap nsMap) {
125 for _, nsKeyMap := range nsMap {
126 for ns, _ := range nsKeyMap {
127 nsList = append(nsList, ns)
132 for _, ns := range nsList {
137 func printNamespacesWide(cmd *cobra.Command, nsMap nsMap) {
139 w := tabwriter.NewWriter(cmd.OutOrStdout(), 6, 4, 3, ' ', 0)
140 fmt.Fprintln(w, "ADDRESS\tNAMESPACE\tKEYS\t")
141 for addr, nsKeyMap := range nsMap {
142 for ns, _ := range nsKeyMap {
143 nsList = append(nsList, ns)
146 for _, ns := range nsList {
148 fmt.Fprintf(w, "\t%s\t%d\t\n", ns, nsKeyMap[ns])
150 fmt.Fprintf(w, "%s\t%s\t%d\t\n", addr, ns, nsKeyMap[ns])