This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
/
traffic_influence_subscription_put.go
120 lines (97 loc) · 2.85 KB
/
traffic_influence_subscription_put.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2019 Intel Corporation
package af
import (
"context"
"encoding/json"
"net/http"
"strconv"
)
func modifySubscriptionByPut(cliCtx context.Context, ts TrafficInfluSub,
afCtx *Context, sID string) (TrafficInfluSub,
*http.Response, error) {
cliCfg := NewConfiguration(afCtx)
cli := NewClient(cliCfg)
tsResp, resp, err := cli.TrafficInfluSubPutAPI.SubscriptionPut(cliCtx,
afCtx.cfg.AfID, sID, ts)
if err != nil {
return TrafficInfluSub{}, resp, err
}
return tsResp, resp, nil
}
// ModifySubscriptionPut function
func ModifySubscriptionPut(w http.ResponseWriter, r *http.Request) {
var (
err error
ts TrafficInfluSub
tsResp TrafficInfluSub
resp *http.Response
sID string
transID int
)
afCtx := r.Context().Value(keyType("af-ctx")).(*Context)
if afCtx == nil {
log.Errf("Traffic Influance Subscription put: " +
"af-ctx retrieved from request is nil")
w.WriteHeader(http.StatusInternalServerError)
return
}
if afCtx.subscriptions == nil {
log.Errf("AF context subscriptions map has not been initialized")
w.WriteHeader(http.StatusInternalServerError)
return
}
if afCtx.transactions == nil {
log.Errf("AF context transactions map has not been initialized")
w.WriteHeader(http.StatusInternalServerError)
return
}
cliCtx, cancel := context.WithCancel(context.Background())
defer cancel()
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err = json.NewDecoder(r.Body).Decode(&ts); err != nil {
log.Errf("Traffic Influance Subscription modify: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
transID, err = genTransactionID(afCtx)
if err != nil {
log.Errf("Traffic Influance Subscription modify %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
ts.AFTransID = strconv.Itoa(transID)
sID, err = getSubsIDFromURL(r.URL)
if err != nil {
log.Errf("Traffic Influence Subscription modify: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
afCtx.transactions[transID] = TrafficInfluSub{}
tsResp, resp, err = modifySubscriptionByPut(cliCtx, ts, afCtx,
sID)
if err != nil {
log.Errf("Traffic Influence Subscription modify : %s", err.Error())
w.WriteHeader(getStatusCode(resp))
return
}
if ts.SubscribedEvents == nil {
delete(afCtx.transactions, transID)
log.Infof("Deleted transaction: %v", transID)
} else {
afCtx.transactions[transID] = tsResp
}
if _, ok := afCtx.subscriptions[sID][strconv.Itoa(transID)]; ok {
delete(afCtx.transactions, transID)
log.Infof("Deleted transaction: %v", transID)
}
if afCtx.subscriptions[sID] == nil {
afCtx.subscriptions[sID] =
make(map[string]TrafficInfluSub)
}
afCtx.subscriptions[sID] =
map[string]TrafficInfluSub{ts.AFTransID: tsResp}
if resp != nil {
w.WriteHeader(resp.StatusCode)
}
}