2 * Copyright (C) 2017-2019 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/aglafb"
24 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
25 "github.com/gin-gonic/gin"
28 //************************* AGL XDS Monitoring *************************
30 // getMonitoringTopo : return current AGL daemons topology using monitoring
31 func (s *APIService) getMonitoringTopo(c *gin.Context) {
33 xdspvr, err := s._initXdsMonitoring()
35 common.APIError(c, err.Error())
39 var res aglafb.AfbReply
40 if err = xdspvr.GetTopo(&res); err != nil {
41 common.APIError(c, err.Error())
45 if res.Request.Status != "success" {
46 common.APIError(c, res.Request.Info)
50 c.JSON(http.StatusOK, res.Response)
53 // startMonitoring : resquest to monitoring to start tracing
54 func (s *APIService) startMonitoring(c *gin.Context) {
56 xdspvr, err := s._initXdsMonitoring()
58 common.APIError(c, err.Error())
62 var cfg XdsSuperVTraceConfig
63 if c.BindJSON(&cfg) != nil {
64 common.APIError(c, "Invalid config argument")
67 s.Log.Debugf("Start Monitoring cfgArg %v", cfg)
69 res := aglafb.NewAfbReply()
70 if err = xdspvr.StartTrace(cfg, res); err != nil {
71 common.APIError(c, err.Error())
76 common.APIError(c, res.GetInfo())
80 c.JSON(http.StatusOK, res.Response)
83 // stopMonitoring : resquest to monitoring to stop tracing
84 func (s *APIService) stopMonitoring(c *gin.Context) {
86 xdspvr, err := s._initXdsMonitoring()
88 common.APIError(c, err.Error())
92 var res aglafb.AfbReply
93 if err = xdspvr.StopTrace(&res); err != nil {
94 common.APIError(c, err.Error())
98 if res.Request.Status != "success" {
99 common.APIError(c, res.Request.Info)
103 c.JSON(http.StatusOK, res.Response)
106 // _initXdsMonitoring .
107 func (s *APIService) _initXdsMonitoring() (*XdsMonitoring, error) {
109 if s.XdsMonitoring == nil {
110 xs := NewXdsMonitoring(s.Context)
111 if err := xs.Connect(); err != nil {
116 return s.XdsMonitoring, nil
119 //************************* AGL Low Collector *************************
121 // XdsLowCollectorConfig Configuration structure for ALC
122 type XdsLowCollectorConfig struct {
123 Time int `json:"time"`
126 // StartLowCollector : resquest to Start low collector
127 func (s *APIService) StartLowCollector(c *gin.Context) {
129 alc, err := s._initXdsLowCollector()
131 common.APIError(c, err.Error())
135 s.Log.Debugf("Init & config AGL Low Collector")
137 if err = alc.Init(); err != nil {
138 common.APIError(c, err.Error())
142 // // Config is optional, if not set used define settings
143 var cfg XdsLowCollectorConfig
144 c.ShouldBindJSON(&cfg)
146 s.Log.Debugf("Start Low Collector cfgArg %v", cfg)
148 if err = alc.Start(cfg.Time); err != nil {
149 common.APIError(c, err.Error())
153 c.JSON(http.StatusOK, "done")
156 // StopLowCollector : resquest to Stop low collector
157 func (s *APIService) StopLowCollector(c *gin.Context) {
159 alc, err := s._initXdsLowCollector()
161 common.APIError(c, err.Error())
165 s.Log.Debugf("Stop AGL Low Collector")
167 if err = alc.Stop(); err != nil {
168 common.APIError(c, err.Error())
173 c.JSON(http.StatusOK, res)
176 // ReadLowCollector : read one data
177 func (s *APIService) ReadLowCollector(c *gin.Context) {
179 alc, err := s._initXdsLowCollector()
181 common.APIError(c, err.Error())
185 s.Log.Debugf("Read data of %s plugin AGL Low Collector", plugin)
188 if err = alc.Read(&data); err != nil {
189 common.APIError(c, err.Error())
194 c.JSON(http.StatusOK, res)
197 // ResetLowCollector : Reset Low Collector
198 func (s *APIService) ResetLowCollector(c *gin.Context) {
200 common.APIError(c, "Not implemented yet")
203 // _initXdsLowCollector .
204 func (s *APIService) _initXdsLowCollector() (*XdsLowCollector, error) {
206 if s.XdsLowCollector == nil {
207 alc := NewXdsLowCollector(s.Context)
208 if err := alc.Connect(); err != nil {
211 s.XdsLowCollector = alc
213 return s.XdsLowCollector, nil