go.ligato.io/vpp-agent/v3@v3.5.0/ansible/action_plugins/plugins/bridgeDomain.py (about)

     1  # Copyright (c) 2019 PANTHEON.tech
     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  import json
    16  
    17  import etcd3
    18  from google.protobuf.json_format import MessageToJson, Parse
    19  
    20  from action_plugins.pout.models.vpp.l2.bridge_domain_pb2 import BridgeDomain
    21  
    22  
    23  def plugin_init(name, values, agent_name, ip, port):
    24      if name == 'bridge-domain':
    25          return BridgeDomainValidation(values, agent_name)
    26      elif name == 'add-bridge-domain-interface':
    27          return AddBridgeDomainInterfaceValidation(values, agent_name, ip, port)
    28      elif name == 'remove-bridge-domain-interface':
    29          return RemoveBridgeDomainInterfaceValidation(values, agent_name, ip, port)
    30      else:
    31          return False
    32  
    33  
    34  class BridgeDomainValidation:
    35  
    36      def __init__(self, values, agent_name):
    37          self.values = values
    38          self.agent_name = agent_name
    39  
    40      def validate(self):
    41          bridgeDomain = BridgeDomain()
    42          Parse(json.dumps(self.values), bridgeDomain)
    43          return MessageToJson(bridgeDomain, preserving_proto_field_name=True, indent=None)
    44  
    45      def create_key(self):
    46          return "/vnf-agent/{}/config/vpp/l2/v2/bridge-domain/{}".format(self.agent_name, self.values['name'])
    47  
    48  
    49  class AddBridgeDomainInterfaceValidation:
    50  
    51      def __init__(self, values, agent_name, ip, port):
    52          self.values = values
    53          self.agent_name = agent_name
    54          host = ip
    55          port = port
    56          self.client = etcd3.client(host, port)
    57  
    58      def validate(self):
    59          etcd_values = self.client.get(self.create_key())
    60          val = {}
    61          if etcd_values[0] is None:
    62              val['interfaces'] = []
    63          else:
    64              val = json.loads(etcd_values[0])
    65  
    66          if val.get('interfaces') is None:
    67              val['interfaces'] = []
    68          val['interfaces'] += self.values['interfaces']
    69  
    70          bridgeDomain = BridgeDomain()
    71          Parse(json.dumps(val), bridgeDomain)
    72          return MessageToJson(bridgeDomain, preserving_proto_field_name=True, indent=None)
    73  
    74      def create_key(self):
    75          return "/vnf-agent/{}/config/vpp/l2/v2/bridge-domain/{}".format(self.agent_name, self.values['name'])
    76  
    77  
    78  class RemoveBridgeDomainInterfaceValidation:
    79  
    80      def __init__(self, values, agent_name, ip, port):
    81          self.values = values
    82          self.agent_name = agent_name
    83          host = ip
    84          port = int(port)
    85          self.client = etcd3.client(host, port)
    86  
    87      def validate(self):
    88          etcd_values = self.client.get(self.create_key())
    89          val = json.loads(etcd_values[0])
    90          try:
    91              val['interfaces'].remove(self.values['interfaces'][0])
    92          except:
    93              pass
    94  
    95          bridgeDomain = BridgeDomain()
    96          Parse(json.dumps(val), bridgeDomain)
    97          return MessageToJson(bridgeDomain, preserving_proto_field_name=True, indent=None)
    98  
    99      def create_key(self):
   100          return "/vnf-agent/{}/config/vpp/l2/v2/bridge-domain/{}".format(self.agent_name, self.values['name'])