go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/acl/models.go (about)

     1  //  Copyright (c) 2018 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_acl
    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.acls"
    25  
    26  var (
    27  	ModelACL = models.Register(&ACL{}, models.Spec{
    28  		Module:  ModuleName,
    29  		Version: "v2",
    30  		Type:    "acl",
    31  	})
    32  )
    33  
    34  // Key returns the prefix used in ETCD to store vpp ACL config
    35  // of a particular ACL in selected vpp instance.
    36  func Key(aclName string) string {
    37  	return models.Key(&ACL{
    38  		Name: aclName,
    39  	})
    40  }
    41  
    42  const (
    43  	aclToInterfaceTemplate = "vpp/acl/{acl}/interface/{flow}/{iface}"
    44  
    45  	// IngressFlow represents ingress packet flow
    46  	IngressFlow = "ingress"
    47  	// EgressFlow represents egress packet flow
    48  	EgressFlow = "egress"
    49  )
    50  
    51  const (
    52  	// InvalidKeyPart is used in key for parts which are invalid
    53  	InvalidKeyPart = "<invalid>"
    54  )
    55  
    56  // ToInterfaceKey returns key for ACL to interface
    57  func ToInterfaceKey(acl, iface, flow string) string {
    58  	if acl == "" {
    59  		acl = InvalidKeyPart
    60  	}
    61  	if iface == "" {
    62  		iface = InvalidKeyPart
    63  	}
    64  	if flow != IngressFlow && flow != EgressFlow {
    65  		flow = InvalidKeyPart
    66  	}
    67  	key := aclToInterfaceTemplate
    68  	key = strings.Replace(key, "{acl}", acl, 1)
    69  	key = strings.Replace(key, "{flow}", flow, 1)
    70  	key = strings.Replace(key, "{iface}", iface, 1)
    71  	return key
    72  }
    73  
    74  // ParseACLToInterfaceKey parses ACL to interface key
    75  func ParseACLToInterfaceKey(key string) (acl, iface, flow string, isACLToInterface bool) {
    76  	parts := strings.Split(key, "/")
    77  
    78  	interfaceIndex := -1
    79  	for index, part := range parts {
    80  		if part == "interface" {
    81  			interfaceIndex = index
    82  			break
    83  		}
    84  	}
    85  
    86  	if len(parts) > interfaceIndex+1 {
    87  		flow = parts[interfaceIndex+1]
    88  	}
    89  
    90  	if len(parts) >= 5 &&
    91  		parts[0] == "vpp" && parts[1] == "acl" && interfaceIndex >= 3 &&
    92  		(flow == IngressFlow || flow == EgressFlow || flow == InvalidKeyPart) {
    93  		acl = strings.Join(parts[2:interfaceIndex], "/")
    94  		iface = strings.Join(parts[interfaceIndex+2:], "/")
    95  		if iface != "" && acl != "" {
    96  			return acl, iface, flow, true
    97  		}
    98  	}
    99  	return "", "", "", false
   100  }