go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/l2/models.go (about) 1 // Copyright (c) 2017 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_l2 16 17 import ( 18 "strings" 19 20 "go.ligato.io/vpp-agent/v3/pkg/models" 21 ) 22 23 // ModuleName is the module name used for models. 24 const ModuleName = "vpp.l2" 25 26 var ( 27 ModelBridgeDomain = models.Register(&BridgeDomain{}, models.Spec{ 28 Module: ModuleName, 29 Type: "bridge-domain", 30 Version: "v2", 31 }) 32 33 ModelFIBEntry = models.Register(&FIBEntry{}, models.Spec{ 34 Module: ModuleName, 35 Type: "fib", 36 Version: "v2", 37 }, models.WithNameTemplate("{{.BridgeDomain}}/mac/{{.PhysAddress}}")) 38 39 ModelXConnectPair = models.Register(&XConnectPair{}, models.Spec{ 40 Module: ModuleName, 41 Type: "xconnect", 42 Version: "v2", 43 }, models.WithNameTemplate("{{.ReceiveInterface}}")) 44 ) 45 46 // BridgeDomainKey returns the key used in NB DB to store the configuration of the 47 // given bridge domain. 48 func BridgeDomainKey(bdName string) string { 49 return models.Key(&BridgeDomain{ 50 Name: bdName, 51 }) 52 } 53 54 // FIBKey returns the key used in NB DB to store the configuration of the 55 // given L2 FIB entry. 56 func FIBKey(bdName string, fibMac string) string { 57 return models.Key(&FIBEntry{ 58 BridgeDomain: bdName, 59 PhysAddress: fibMac, 60 }) 61 } 62 63 // XConnectKey returns the key used in NB DB to store the configuration of the 64 // given xConnect (identified by RX interface). 65 func XConnectKey(rxIface string) string { 66 return models.Key(&XConnectPair{ 67 ReceiveInterface: rxIface, 68 }) 69 } 70 71 /* BD <-> interface binding (derived) */ 72 const ( 73 // bdInterfaceKeyTemplate is a template for (derived) key representing binding 74 // between interface and a bridge domain. 75 bdInterfaceKeyTemplate = "vpp/bd/{bd}/interface/{iface}" 76 ) 77 78 const ( 79 // InvalidKeyPart is used in key for parts which are invalid 80 InvalidKeyPart = "<invalid>" 81 ) 82 83 /* BD <-> interface binding (derived) */ 84 85 // BDInterfaceKey returns the key used to represent binding between the given interface 86 // and the bridge domain. 87 func BDInterfaceKey(bdName string, iface string) string { 88 if bdName == "" { 89 bdName = InvalidKeyPart 90 } 91 if iface == "" { 92 iface = InvalidKeyPart 93 } 94 key := strings.Replace(bdInterfaceKeyTemplate, "{bd}", bdName, 1) 95 key = strings.Replace(key, "{iface}", iface, 1) 96 return key 97 } 98 99 // ParseBDInterfaceKey parses key representing binding between interface and a bridge 100 // domain. 101 func ParseBDInterfaceKey(key string) (bdName string, iface string, isBDIfaceKey bool) { 102 keyComps := strings.Split(key, "/") 103 if len(keyComps) >= 5 && keyComps[0] == "vpp" && keyComps[1] == "bd" && keyComps[3] == "interface" { 104 iface = strings.Join(keyComps[4:], "/") 105 return keyComps[2], iface, true 106 } 107 return "", "", false 108 }