diff --git a/vppcfg/vpp/planner.py b/vppcfg/vpp/planner.py index 1eb50a2..c4f4c74 100644 --- a/vppcfg/vpp/planner.py +++ b/vppcfg/vpp/planner.py @@ -19,7 +19,6 @@ metadata, and plan configuration changes towards a given YAML target configurati """ import sys import logging -from vppcfg.config import loopback from vppcfg.config import interface from vppcfg.config import lcp from .vppapi import VPPApi @@ -43,6 +42,7 @@ class Planner(PlannerPruneOperations, PlannerCreateOperations, PlannerSyncOperat vpp_api_socket="/run/vpp/api.sock", vpp_json_dir=None, ): + super().__init__() self.logger = logging.getLogger("vppcfg.planner") self.logger.addHandler(logging.NullHandler()) @@ -84,7 +84,6 @@ class Planner(PlannerPruneOperations, PlannerCreateOperations, PlannerSyncOperat ret = False return ret - def write(self, outfile, emit_ok=False): """Emit the CLI contents to stdout (if outfile=='-') or a named file otherwise. If the 'emit_ok' flag is False, emit a warning at the top and bottom of the file. diff --git a/vppcfg/vpp/planner_base.py b/vppcfg/vpp/planner_base.py new file mode 100644 index 0000000..babcc46 --- /dev/null +++ b/vppcfg/vpp/planner_base.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# +# Copyright (c) 2022 Pim van Pelt +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# -*- coding: utf-8 -*- +""" +Base class defining the interface expected by planner mixins. +""" + +from abc import ABC +from typing import Dict, Any, List + + +class PlannerBase(ABC): # pylint: disable=too-few-public-methods + """Abstract base class defining the interface expected by planner mixins.""" + + def __init__(self): + # These attributes will be set by the concrete Planner class + self.cfg: Dict[str, Any] + self.vpp: Any # VPPApi instance + self.logger: Any # Logger instance + self.cli: Dict[str, List[str]] # CLI commands grouped by operation type diff --git a/vppcfg/vpp/planner_create.py b/vppcfg/vpp/planner_create.py index 7b255bd..70e5d8c 100644 --- a/vppcfg/vpp/planner_create.py +++ b/vppcfg/vpp/planner_create.py @@ -23,9 +23,10 @@ from vppcfg.config import bondethernet from vppcfg.config import bridgedomain from vppcfg.config import vxlan_tunnel from vppcfg.config import tap +from .planner_base import PlannerBase -class PlannerCreateOperations: +class PlannerCreateOperations(PlannerBase): # pylint: disable=too-few-public-methods """Mixin class providing create operations for the Planner.""" def create(self): @@ -213,4 +214,4 @@ class PlannerCreateOperations: continue cli = f"lcp create {ifname} host-if {iface['lcp']}" self.cli["create"].append(cli) - return True \ No newline at end of file + return True diff --git a/vppcfg/vpp/planner_prune.py b/vppcfg/vpp/planner_prune.py index 0f1d58b..1eb9349 100644 --- a/vppcfg/vpp/planner_prune.py +++ b/vppcfg/vpp/planner_prune.py @@ -22,11 +22,11 @@ from vppcfg.config import interface from vppcfg.config import bondethernet from vppcfg.config import bridgedomain from vppcfg.config import vxlan_tunnel -from vppcfg.config import lcp from vppcfg.config import tap +from .planner_base import PlannerBase -class PlannerPruneOperations: +class PlannerPruneOperations(PlannerBase): # pylint: disable=too-few-public-methods """Mixin class providing prune operations for the Planner.""" def prune(self): @@ -264,7 +264,7 @@ class PlannerPruneOperations: return True return False - def _tap_has_diff(self, ifname): + def _tap_has_diff(self, ifname): # pylint: disable=too-many-return-statements """Returns True if the given ifname (tap0) has different attributes between VPP and the given configuration, or if either does not exist. @@ -692,4 +692,4 @@ class PlannerPruneOperations: cli = f"set interface state {ifname} down" self.cli["prune"].append(cli) - return True \ No newline at end of file + return True diff --git a/vppcfg/vpp/planner_sync.py b/vppcfg/vpp/planner_sync.py index 318151d..3d7e3eb 100644 --- a/vppcfg/vpp/planner_sync.py +++ b/vppcfg/vpp/planner_sync.py @@ -21,9 +21,10 @@ from vppcfg.config import loopback from vppcfg.config import interface from vppcfg.config import bondethernet from vppcfg.config import bridgedomain +from .planner_base import PlannerBase -class PlannerSyncOperations: +class PlannerSyncOperations(PlannerBase): # pylint: disable=too-few-public-methods """Mixin class providing sync operations for the Planner.""" def sync(self): @@ -589,4 +590,4 @@ class PlannerSyncOperations: state = "down" cli = f"set interface state {vpp_ifname} {state}" self.cli["sync"].append(cli) - return True \ No newline at end of file + return True