go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/aclplugin/vppcalls/vpp2101/vppcalls_handlers.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 vpp2101
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	govppapi "go.fd.io/govpp/api"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls"
    25  	vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/acl"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    29  )
    30  
    31  func init() {
    32  	msgs := acl.AllMessages()
    33  	vppcalls.AddHandlerVersion(vpp2101.Version, msgs, NewACLVppHandler)
    34  }
    35  
    36  // ACLVppHandler is accessor for acl-related vppcalls methods
    37  type ACLVppHandler struct {
    38  	callsChannel govppapi.Channel
    39  	// TODO: use only RPC service
    40  	acl       acl.RPCService
    41  	ifIndexes ifaceidx.IfaceMetadataIndex
    42  }
    43  
    44  func NewACLVppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex) vppcalls.ACLVppAPI {
    45  	ch, err := c.NewAPIChannel()
    46  	if err != nil {
    47  		return nil
    48  	}
    49  	return &ACLVppHandler{
    50  		callsChannel: ch,
    51  		acl:          acl.NewServiceClient(c),
    52  		ifIndexes:    ifIdx,
    53  	}
    54  }
    55  
    56  func prefixToString(address ip_types.Prefix) string {
    57  	if address.Address.Af == ip_types.ADDRESS_IP6 {
    58  		ip6 := address.Address.Un.GetIP6()
    59  		return fmt.Sprintf("%s/%d", net.IP(ip6[:]).To16(), address.Len)
    60  	} else {
    61  		ip4 := address.Address.Un.GetIP4()
    62  		return fmt.Sprintf("%s/%d", net.IP(ip4[:]).To4(), address.Len)
    63  	}
    64  }
    65  
    66  func addressToIP(address ip_types.Address) net.IP {
    67  	if address.Af == ip_types.ADDRESS_IP6 {
    68  		ipAddr := address.Un.GetIP6()
    69  		return net.IP(ipAddr[:]).To16()
    70  	}
    71  	ipAddr := address.Un.GetIP4()
    72  	return net.IP(ipAddr[:]).To4()
    73  }