configmap.go 4.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Copyright IBM Corp. 2017 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

                 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package configtx

import (
	"fmt"
	"strings"

	cb "github.com/hyperledger/fabric/protos/common"
)

const (
	RootGroupKey = "Channel"

	GroupPrefix  = "[Groups] "
	ValuePrefix  = "[Values] "
31
	PolicyPrefix = "[Policy] " // The plurarility doesn't match, but, it makes the logs much easier being the same length as "Groups" and "Values"
32
33
34
35

	PathSeparator = "/"
)

36
// MapConfig is intended to be called outside this file
37
// it takes a ConfigGroup and generates a map of fqPath to comparables (or error on invalid keys)
38
func MapConfig(channelGroup *cb.ConfigGroup) (map[string]comparable, error) {
39
	result := make(map[string]comparable)
40
41
42
43
44
	if channelGroup != nil {
		err := recurseConfig(result, []string{RootGroupKey}, channelGroup)
		if err != nil {
			return nil, err
		}
45
46
47
48
	}
	return result, nil
}

49
// addToMap is used only internally by MapConfig
50
51
52
53
54
55
56
57
58
59
60
61
func addToMap(cg comparable, result map[string]comparable) error {
	var fqPath string

	switch {
	case cg.ConfigGroup != nil:
		fqPath = GroupPrefix
	case cg.ConfigValue != nil:
		fqPath = ValuePrefix
	case cg.ConfigPolicy != nil:
		fqPath = PolicyPrefix
	}

62
	if err := validateConfigID(cg.key); err != nil {
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		return fmt.Errorf("Illegal characters in key: %s", fqPath)
	}

	if len(cg.path) == 0 {
		fqPath += PathSeparator + cg.key
	} else {
		fqPath += PathSeparator + strings.Join(cg.path, PathSeparator) + PathSeparator + cg.key
	}

	logger.Debugf("Adding to config map: %s", fqPath)

	result[fqPath] = cg

	return nil
}

79
// recurseConfig is used only internally by MapConfig
80
81
82
83
84
85
func recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error {
	if err := addToMap(comparable{key: path[len(path)-1], path: path[:len(path)-1], ConfigGroup: group}, result); err != nil {
		return err
	}

	for key, group := range group.Groups {
86
87
88
		nextPath := make([]string, len(path)+1)
		copy(nextPath, path)
		nextPath[len(nextPath)-1] = key
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
		if err := recurseConfig(result, nextPath, group); err != nil {
			return err
		}
	}

	for key, value := range group.Values {
		if err := addToMap(comparable{key: key, path: path, ConfigValue: value}, result); err != nil {
			return err
		}
	}

	for key, policy := range group.Policies {
		if err := addToMap(comparable{key: key, path: path, ConfigPolicy: policy}, result); err != nil {
			return err
		}
	}

	return nil
}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

// configMapToConfig is intended to be called from outside this file
// It takes a configMap and converts it back into a *cb.ConfigGroup structure
func configMapToConfig(configMap map[string]comparable) (*cb.ConfigGroup, error) {
	rootPath := PathSeparator + RootGroupKey
	return recurseConfigMap(rootPath, configMap)
}

// recurseConfigMap is used only internally by configMapToConfig
// Note, this function mutates the cb.Config* entrieswithin configMap, so errors are generally fatal
func recurseConfigMap(path string, configMap map[string]comparable) (*cb.ConfigGroup, error) {
	groupPath := GroupPrefix + path
	group, ok := configMap[groupPath]
	if !ok {
		return nil, fmt.Errorf("Missing group at path: %s", groupPath)
	}

	if group.ConfigGroup == nil {
Baohua Yang's avatar
Baohua Yang committed
126
		return nil, fmt.Errorf("ConfigGroup not found at group path: %s", groupPath)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	}

	for key, _ := range group.Groups {
		updatedGroup, err := recurseConfigMap(path+PathSeparator+key, configMap)
		if err != nil {
			return nil, err
		}
		group.Groups[key] = updatedGroup
	}

	for key, _ := range group.Values {
		valuePath := ValuePrefix + path + PathSeparator + key
		value, ok := configMap[valuePath]
		if !ok {
			return nil, fmt.Errorf("Missing value at path: %s", valuePath)
		}
		if value.ConfigValue == nil {
Baohua Yang's avatar
Baohua Yang committed
144
			return nil, fmt.Errorf("ConfigValue not found at value path: %s", valuePath)
145
146
147
148
149
150
151
152
153
154
155
		}
		group.Values[key] = value.ConfigValue
	}

	for key, _ := range group.Policies {
		policyPath := PolicyPrefix + path + PathSeparator + key
		policy, ok := configMap[policyPath]
		if !ok {
			return nil, fmt.Errorf("Missing policy at path: %s", policyPath)
		}
		if policy.ConfigPolicy == nil {
Baohua Yang's avatar
Baohua Yang committed
156
			return nil, fmt.Errorf("ConfigPolicy not found at policy path: %s", policyPath)
157
158
159
160
161
162
		}
		group.Policies[key] = policy.ConfigPolicy
	}

	return group.ConfigGroup, nil
}