|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package inspector |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/golang/protobuf/proto" |
| 23 | + cb "github.com/hyperledger/fabric/protos/common" |
| 24 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 25 | +) |
| 26 | + |
| 27 | +// This file contains the functions needed to create Viewables for protos defined in |
| 28 | +// the orderer configuration proto |
| 29 | + |
| 30 | +type ordererTypes struct{} |
| 31 | + |
| 32 | +func (ot ordererTypes) Value(configItem *cb.ConfigurationItem) Viewable { |
| 33 | + name := "Value" |
| 34 | + switch configItem.Key { |
| 35 | + case "ConsensusType": |
| 36 | + value := &ab.ConsensusType{} |
| 37 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 38 | + return viewableError(name, err) |
| 39 | + } |
| 40 | + return viewableConsensusType(configItem.Key, value) |
| 41 | + case "BatchSize": |
| 42 | + value := &ab.BatchSize{} |
| 43 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 44 | + return viewableError(name, err) |
| 45 | + } |
| 46 | + return viewableBatchSize(configItem.Key, value) |
| 47 | + case "BatchTimeout": |
| 48 | + value := &ab.BatchTimeout{} |
| 49 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 50 | + return viewableError(name, err) |
| 51 | + } |
| 52 | + return viewableBatchTimeout(configItem.Key, value) |
| 53 | + case "CreationPolicy": |
| 54 | + value := &ab.CreationPolicy{} |
| 55 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 56 | + return viewableError(name, err) |
| 57 | + } |
| 58 | + return viewableCreationPolicy(configItem.Key, value) |
| 59 | + case "IngressPolicy": |
| 60 | + value := &ab.IngressPolicy{} |
| 61 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 62 | + return viewableError(name, err) |
| 63 | + } |
| 64 | + return viewableIngressPolicy(configItem.Key, value) |
| 65 | + case "EgressPolicy": |
| 66 | + value := &ab.EgressPolicy{} |
| 67 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 68 | + return viewableError(name, err) |
| 69 | + } |
| 70 | + return viewableEgressPolicy(configItem.Key, value) |
| 71 | + case "ChainCreators": |
| 72 | + value := &ab.ChainCreators{} |
| 73 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 74 | + return viewableError(name, err) |
| 75 | + } |
| 76 | + return viewableChainCreators(configItem.Key, value) |
| 77 | + case "KafkaBrokers": |
| 78 | + value := &ab.KafkaBrokers{} |
| 79 | + if err := proto.Unmarshal(configItem.Value, value); err != nil { |
| 80 | + return viewableError(name, err) |
| 81 | + } |
| 82 | + return viewableKafkaBrokers(configItem.Key, value) |
| 83 | + default: |
| 84 | + } |
| 85 | + return viewableError(name, fmt.Errorf("Unknown key: %s", configItem.Key)) |
| 86 | +} |
| 87 | + |
| 88 | +func viewableConsensusType(name string, consensusType *ab.ConsensusType) Viewable { |
| 89 | + return &field{ |
| 90 | + name: name, |
| 91 | + values: []Viewable{viewableString("Type", consensusType.Type)}, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func viewableBatchTimeout(name string, batchTimeout *ab.BatchTimeout) Viewable { |
| 96 | + return &field{ |
| 97 | + name: name, |
| 98 | + values: []Viewable{viewableString("Timeout", batchTimeout.Timeout)}, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func viewableIngressPolicy(name string, ingressPolicy *ab.IngressPolicy) Viewable { |
| 103 | + return &field{ |
| 104 | + name: name, |
| 105 | + values: []Viewable{viewableString("Name", ingressPolicy.Name)}, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func viewableEgressPolicy(name string, egressPolicy *ab.EgressPolicy) Viewable { |
| 110 | + return &field{ |
| 111 | + name: name, |
| 112 | + values: []Viewable{viewableString("Name", egressPolicy.Name)}, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func viewableChainCreators(name string, creators *ab.ChainCreators) Viewable { |
| 117 | + return &field{ |
| 118 | + name: name, |
| 119 | + values: []Viewable{viewableStringSlice("Policies", creators.Policies)}, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func viewableKafkaBrokers(name string, brokers *ab.KafkaBrokers) Viewable { |
| 124 | + return &field{ |
| 125 | + name: name, |
| 126 | + values: []Viewable{viewableStringSlice("Brokers", brokers.Brokers)}, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func viewableCreationPolicy(name string, creationPolicy *ab.CreationPolicy) Viewable { |
| 131 | + return &field{ |
| 132 | + name: name, |
| 133 | + values: []Viewable{ |
| 134 | + viewableString("Policy", creationPolicy.Policy), |
| 135 | + viewableBytes("Digest", creationPolicy.Digest), |
| 136 | + }, |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func viewableBatchSize(name string, batchSize *ab.BatchSize) Viewable { |
| 141 | + return &field{ |
| 142 | + name: name, |
| 143 | + values: []Viewable{ |
| 144 | + viewableUint32("MaxMessageCount", batchSize.MaxMessageCount), |
| 145 | + viewableUint32("AbsoluteMaxBytes", batchSize.AbsoluteMaxBytes), |
| 146 | + }, |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func init() { |
| 151 | + typeMap[cb.ConfigurationItem_Orderer] = ordererTypes{} |
| 152 | +} |
0 commit comments