8bd26c194cf08f69233cf7d576c9b1c19dd3b0dd
[icn/sdwan.git] /
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (c) 2020 Intel Corporation
3
4 package contextupdateclient
5
6 import (
7         "context"
8         "time"
9
10         contextpb "github.com/open-ness/EMCO/src/orchestrator/pkg/grpc/contextupdate"
11         log "github.com/open-ness/EMCO/src/orchestrator/pkg/infra/logutils"
12         "github.com/open-ness/EMCO/src/orchestrator/pkg/infra/rpc"
13         pkgerrors "github.com/pkg/errors"
14 )
15
16 // InvokeContextUpdate will make the grpc call to the specified controller
17 // The controller will take the specified intentName and update the AppContext
18 // appropriatly based on its operation as a placement or action controller.
19 func InvokeContextUpdate(controllerName, intentName, appContextId string) error {
20         var err error
21         var rpcClient contextpb.ContextupdateClient
22         var updateRes *contextpb.ContextUpdateResponse
23         ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
24         defer cancel()
25
26         conn := rpc.GetRpcConn(controllerName)
27         if conn != nil {
28                 rpcClient = contextpb.NewContextupdateClient(conn)
29                 updateReq := new(contextpb.ContextUpdateRequest)
30                 updateReq.AppContext = appContextId
31                 updateReq.IntentName = intentName
32                 updateRes, err = rpcClient.UpdateAppContext(ctx, updateReq)
33         } else {
34                 return pkgerrors.Errorf("ContextUpdate Failed - Could not get ContextupdateClient: %v", controllerName)
35         }
36
37         if err == nil {
38                 if updateRes.AppContextUpdated {
39                         log.Info("ContextUpdate Passed", log.Fields{
40                                 "Controller": controllerName,
41                                 "Intent":     intentName,
42                                 "AppContext": appContextId,
43                                 "Message":    updateRes.AppContextUpdateMessage,
44                         })
45                         return nil
46                 } else {
47                         return pkgerrors.Errorf("ContextUpdate Failed: %v", updateRes.AppContextUpdateMessage)
48                 }
49         }
50         return err
51 }