go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/scenario/reverted_change.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 scenario
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"go.ligato.io/vpp-agent/v3/client"
    22  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    23  )
    24  
    25  // RevertedChange presents a scenario, in which an update transaction fails
    26  // and gets reverted.
    27  func RevertedChange() {
    28  	c := client.LocalClient
    29  	listKnownModels(c)
    30  
    31  	printMessage(
    32  		"Resync config",
    33  		"  - interface tap1 is configured inside bridge-domain named bd2",
    34  	)
    35  	err := c.ResyncConfig(
    36  		tap1, bd2,
    37  	)
    38  	if err != nil {
    39  		log.Println(err)
    40  	}
    41  	informAboutGraphURL(0, true, true)
    42  
    43  	printMessage(
    44  		"Change config (revert-on-failure enabled)",
    45  		"  - new FIB entry is configured to forward traffic for a certain",
    46  		"    MAC address via tap1",
    47  		"  - bridge domain bd2 is edited to also include tap2 interface",
    48  		"     -> the binding is planned to be temporarily pending until",
    49  		"        the interface gets configured in the next step",
    50  		"  - attempt to configure tap2 fails",
    51  		"     -> the MAC address is invalid as told by the Validate method()",
    52  		"        of the descriptor for interfaces",
    53  		"     -> transaction is reverted",
    54  		"         -> BD is changed back to contain only tap1",
    55  		"         -> the FIB is removed",
    56  	)
    57  	req := c.ChangeRequest()
    58  	req.Update(bd2WithTap2, fib3, tap2Invalid)
    59  
    60  	// IMPORTANT: by default, transactions are best effort - applying the maximum
    61  	// possible subset of requested changes - i.e. allowing partial effect,
    62  	// which is suitable for KVDB NB in combination with automatic Retry
    63  	// of failed operations (see WithRetry option from KVScheduler API).
    64  	// To get the standard transaction behaviour, where either everything or
    65  	// nothing from the transaction is applied, WithRevert(context) must
    66  	// be used to customize the context supplied to the Send() method of the
    67  	// local client.
    68  	ctx := context.Background()
    69  	ctx = kvs.WithRevert(ctx)
    70  
    71  	if err := req.Send(ctx); err != nil {
    72  		log.Println(err)
    73  	}
    74  	informAboutGraphURL(1, false, true)
    75  }