go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/abf/models.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 vpp_abf
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  
    21  	"go.ligato.io/vpp-agent/v3/pkg/models"
    22  )
    23  
    24  // ModuleName is the name of the module used for models.
    25  const ModuleName = "vpp.abfs"
    26  
    27  var (
    28  	ModelABF = models.Register(&ABF{}, models.Spec{
    29  		Module:  ModuleName,
    30  		Version: "v2",
    31  		Type:    "abf",
    32  	}, models.WithNameTemplate("{{.Index}}"))
    33  )
    34  
    35  // Key returns the prefix used in the ETCD to store VPP ACL-based forwarding
    36  // config of a particular ABF in selected vpp instance.
    37  func Key(index uint32) string {
    38  	return models.Key(&ABF{
    39  		Index: index,
    40  	})
    41  }
    42  
    43  const (
    44  	// ABF to interface template is a derived value key
    45  	abfToInterfaceTemplate = "vpp/abf/{abf}/interface/{iface}"
    46  )
    47  
    48  const (
    49  	// InvalidKeyPart is used in key for parts which are invalid
    50  	InvalidKeyPart = "<invalid>"
    51  )
    52  
    53  // ToABFInterfaceKey returns key for ABF-to-interface
    54  func ToInterfaceKey(abf uint32, iface string) string {
    55  	if iface == "" {
    56  		iface = InvalidKeyPart
    57  	}
    58  	key := abfToInterfaceTemplate
    59  	key = strings.Replace(key, "{abf}", strconv.Itoa(int(abf)), 1)
    60  	key = strings.Replace(key, "{iface}", iface, 1)
    61  	return key
    62  }
    63  
    64  // ParseABFToInterfaceKey parses ABF-to-interface key
    65  func ParseToInterfaceKey(key string) (abf, iface string, isABFToInterface bool) {
    66  	parts := strings.Split(key, "/")
    67  	if len(parts) >= 5 &&
    68  		parts[0] == "vpp" && parts[1] == "abf" && parts[3] == "interface" {
    69  		abf = parts[2]
    70  		iface = strings.Join(parts[4:], "/")
    71  		if iface != "" && abf != "" {
    72  			return abf, iface, true
    73  		}
    74  	}
    75  	return "", "", false
    76  }