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"
34 getCmd.AddCommand(newNamespacesCmd(newDatabase))
37 func newNamespacesCmd(dbCreateCb DbCreateCb) *cobra.Command {
38 cmd := &cobra.Command{
40 Short: "List all the namespaces in database",
41 Long: "List all the namespaces in database",
42 Args: cobra.ExactArgs(0),
43 RunE: func(cmd *cobra.Command, args []string) error {
44 showPerDb, _ := cmd.Flags().GetBool("group")
45 nsMap, err := runNamespaces(dbCreateCb)
47 cmd.PrintErrf("%s\n", buf.String())
51 printNamespacesPerDb(cmd, nsMap)
53 printNamespaces(cmd, nsMap)
59 cmd.Flags().BoolP("group", "g", false, "Show namespaces per SDL DB cluster group")
63 func runNamespaces(dbCreateCb DbCreateCb) (map[string][]string, error) {
64 nsMap := make(map[string][]string)
65 for _, dbInst := range dbCreateCb().Instances {
66 keys, err := dbInst.Keys("*")
70 id, err := getServiceAddress(dbInst)
74 for _, key := range keys {
75 namespace, err := parseKeyNamespace(key)
79 if isUniqueNamespace(nsMap[id], namespace) {
80 nsMap[id] = append(nsMap[id], namespace)
87 func getServiceAddress(db iDatabase) (string, error) {
88 state, err := db.State()
92 return state.MasterDbState.GetAddress(), nil
95 func parseKeyNamespace(key string) (string, error) {
96 sIndex := strings.Index(key, "{")
98 return "", fmt.Errorf("Namespace parsing error, no '{' in key string '%s'", key)
100 str := key[sIndex+len("{"):]
101 eIndex := strings.Index(str, "}")
103 return "", fmt.Errorf("Namespace parsing error, no '}' in key string '%s'", key)
105 return str[:eIndex], nil
108 func isUniqueNamespace(namespaces []string, newNs string) bool {
109 for _, n := range namespaces {
117 func printNamespaces(cmd *cobra.Command, nsMap map[string][]string) {
118 var namespaces []string
119 for _, nsList := range nsMap {
120 namespaces = append(namespaces, nsList...)
123 sort.Strings(namespaces)
124 for _, ns := range namespaces {
129 func printNamespacesPerDb(cmd *cobra.Command, nsMap map[string][]string) {
130 for addr, nsList := range nsMap {
132 for _, ns := range nsList {
134 cmd.Printf("%s\n", ns)
136 cmd.Printf("%s: %s\n", addr, ns)