go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/value_flags.go (about)

     1  // Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at:
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvscheduler
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    23  	"go.ligato.io/vpp-agent/v3/proto/ligato/kvscheduler"
    24  )
    25  
    26  const (
    27  	////// updated by transactions:
    28  
    29  	// LastUpdateFlagName is the name of the LastUpdate flag.
    30  	LastUpdateFlagName = "last-update"
    31  	// LastUpdateFlagIndex is the Index of the LastUpdate flag.
    32  	LastUpdateFlagIndex = 0
    33  
    34  	// ErrorFlagName is the name of the Error flag.
    35  	ErrorFlagName = "error"
    36  	// ErrorFlagIndex is the Index of the ErrorFlag flag.
    37  	ErrorFlagIndex = 1
    38  
    39  	////// updated by transactions + refresh:
    40  
    41  	// ValueStateFlagName is the name of the Value-State flag.
    42  	ValueStateFlagName = "value-state"
    43  	// ValueStateFlagIndex is the index of the Value-State flag.
    44  	ValueStateFlagIndex = 2
    45  
    46  	// UnavailValueFlagName is the name of the Unavailable-Value flag.
    47  	UnavailValueFlagName = "unavailable"
    48  	// UnavailValueFlagIndex is the index of the Unavailable-Value flag.
    49  	UnavailValueFlagIndex = 3
    50  
    51  	// DescriptorFlagName is the name of the Descriptor flag.
    52  	DescriptorFlagName = "descriptor"
    53  	// DescriptorFlagIndex is the index of the Descriptor flag.
    54  	DescriptorFlagIndex = 4
    55  
    56  	// DerivedFlagName is the name of the Derived flag.
    57  	DerivedFlagName = "derived"
    58  	// DerivedFlagIndex is the index of the Derived flag.
    59  	DerivedFlagIndex = 5
    60  )
    61  
    62  // flagNameToIndex converts flag name to the associated index.
    63  func flagNameToIndex(flagName string) int {
    64  	switch flagName {
    65  	case LastUpdateFlagName:
    66  		return LastUpdateFlagIndex
    67  	case ErrorFlagName:
    68  		return ErrorFlagIndex
    69  	case ValueStateFlagName:
    70  		return ValueStateFlagIndex
    71  	case UnavailValueFlagName:
    72  		return UnavailValueFlagIndex
    73  	case DescriptorFlagName:
    74  		return DescriptorFlagIndex
    75  	case DerivedFlagName:
    76  		return DerivedFlagIndex
    77  	}
    78  	return -1
    79  }
    80  
    81  /****************************** LastUpdate Flag *******************************/
    82  
    83  // LastUpdateFlag is set to remember the last transaction which has
    84  // changed/updated the value.
    85  // Not set to values just discovered by refresh (state = DISCOVERED).
    86  type LastUpdateFlag struct {
    87  	txnSeqNum uint64
    88  	txnOp     kvscheduler.TxnOperation
    89  	value     proto.Message
    90  
    91  	// updated only when the value content is being modified
    92  	revert bool
    93  
    94  	// set by NB txn, inherited by Retry and SB notifications
    95  	retryEnabled bool
    96  	retryArgs    *kvs.RetryOpt
    97  }
    98  
    99  // GetIndex returns 0.
   100  func (flag *LastUpdateFlag) GetIndex() int {
   101  	return LastUpdateFlagIndex
   102  }
   103  
   104  // GetName return name of the LastUpdate flag.
   105  func (flag *LastUpdateFlag) GetName() string {
   106  	return LastUpdateFlagName
   107  }
   108  
   109  // GetValue describes the last update (txn-seq number only).
   110  func (flag *LastUpdateFlag) GetValue() string {
   111  	return fmt.Sprintf("TXN-%d", flag.txnSeqNum)
   112  }
   113  
   114  /******************************* Error Flag ***********************************/
   115  
   116  // ErrorFlag is used to store error returned from the last operation, including
   117  // validation errors.
   118  type ErrorFlag struct {
   119  	err       error
   120  	retriable bool
   121  }
   122  
   123  // GetIndex returns 1.
   124  func (flag *ErrorFlag) GetIndex() int {
   125  	return ErrorFlagIndex
   126  }
   127  
   128  // GetName return name of the Origin flag.
   129  func (flag *ErrorFlag) GetName() string {
   130  	return ErrorFlagName
   131  }
   132  
   133  // GetValue returns the error as string.
   134  func (flag *ErrorFlag) GetValue() string {
   135  	if flag.err == nil {
   136  		return ""
   137  	}
   138  	return flag.err.Error()
   139  }
   140  
   141  /***************************** Value State Flag *******************************/
   142  
   143  // ValueStateFlag stores current state of the value.
   144  // Assigned to every value.
   145  type ValueStateFlag struct {
   146  	valueState kvscheduler.ValueState
   147  }
   148  
   149  // GetIndex returns 2.
   150  func (flag *ValueStateFlag) GetIndex() int {
   151  	return ValueStateFlagIndex
   152  }
   153  
   154  // GetName returns name of the ValueState flag.
   155  func (flag *ValueStateFlag) GetName() string {
   156  	return ValueStateFlagName
   157  }
   158  
   159  // GetValue returns the string representation of the state.
   160  func (flag *ValueStateFlag) GetValue() string {
   161  	return flag.valueState.String()
   162  }
   163  
   164  /************************** Unavailable Value Flag ****************************/
   165  
   166  // UnavailValueFlag is used to mark NB values which should not be considered
   167  // when resolving dependencies of other values (for various possible reasons).
   168  type UnavailValueFlag struct {
   169  }
   170  
   171  // GetIndex returns 3.
   172  func (flag *UnavailValueFlag) GetIndex() int {
   173  	return UnavailValueFlagIndex
   174  }
   175  
   176  // GetName return name of the UnavailValue flag.
   177  func (flag *UnavailValueFlag) GetName() string {
   178  	return UnavailValueFlagName
   179  }
   180  
   181  // GetValue return empty string (presence of the flag is the only information).
   182  func (flag *UnavailValueFlag) GetValue() string {
   183  	return ""
   184  }
   185  
   186  /*************************** Descriptor Value Flag ****************************/
   187  
   188  // DescriptorFlag is used to lookup values by their descriptor.
   189  // Not assigned to properties and UNIMPLEMENTED values.
   190  type DescriptorFlag struct {
   191  	descriptorName string
   192  }
   193  
   194  // GetIndex returns 4.
   195  func (flag *DescriptorFlag) GetIndex() int {
   196  	return DescriptorFlagIndex
   197  }
   198  
   199  // GetName return name of the Descriptor flag.
   200  func (flag *DescriptorFlag) GetName() string {
   201  	return DescriptorFlagName
   202  }
   203  
   204  // GetValue returns the descriptor name.
   205  func (flag *DescriptorFlag) GetValue() string {
   206  	return flag.descriptorName
   207  }
   208  
   209  /**************************** Derived Value Flag ******************************/
   210  
   211  // DerivedFlag is used to mark derived values.
   212  type DerivedFlag struct {
   213  	baseKey string
   214  }
   215  
   216  // GetIndex returns 5.
   217  func (flag *DerivedFlag) GetIndex() int {
   218  	return DerivedFlagIndex
   219  }
   220  
   221  // GetName return name of the Derived flag.
   222  func (flag *DerivedFlag) GetName() string {
   223  	return DerivedFlagName
   224  }
   225  
   226  // GetValue returns the key of the base value from which the given derived value
   227  // is derived from (directly or transitively).
   228  func (flag *DerivedFlag) GetValue() string {
   229  	return flag.baseKey
   230  }