blob: 1cfaaacd454154081c4ee4cfad6e57f5caf1edef [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001/*
Joey Armstrong3881b732022-12-27 07:55:37 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
amit.ghosh258d14c2020-10-02 15:13:38 +02003
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package dmiserver
18
19import (
20 "context"
21
Hardik Windlass63e35cf2022-02-03 05:58:09 +000022 "github.com/golang/protobuf/ptypes/empty"
Humera Kousera4442952020-11-23 23:51:19 +053023 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25
amit.ghosh258d14c2020-10-02 15:13:38 +020026 dmi "github.com/opencord/device-management-interface/go/dmi"
27)
28
29//ListMetrics lists the supported metrics for the passed device.
30func (dms *DmiAPIServer) ListMetrics(ctx context.Context, req *dmi.HardwareID) (*dmi.ListMetricsResponse, error) {
31 logger.Debugf("ListMetrics invoked with request %+v", req)
Humera Kousera4442952020-11-23 23:51:19 +053032 metrics := getMetricsList()
33
amit.ghosh258d14c2020-10-02 15:13:38 +020034 return &dmi.ListMetricsResponse{
Humera Kousera4442952020-11-23 23:51:19 +053035 Status: dmi.Status_OK_STATUS,
amit.ghosh258d14c2020-10-02 15:13:38 +020036 Reason: 0,
37 Metrics: &dmi.MetricsConfig{
38 Metrics: metrics,
39 },
40 }, nil
41}
42
43//UpdateMetricsConfiguration updates the configuration of the list of metrics in the request
44func (dms *DmiAPIServer) UpdateMetricsConfiguration(ctx context.Context, req *dmi.MetricsConfigurationRequest) (*dmi.MetricsConfigurationResponse, error) {
45 logger.Debugf("UpdateMetricConfiguration invoked with request %+v", req)
Humera Kousera4442952020-11-23 23:51:19 +053046
47 if req == nil || req.Operation == nil {
48 return &dmi.MetricsConfigurationResponse{
Humera Kouser18b275c2020-11-30 11:30:36 +053049 Status: dmi.Status_ERROR_STATUS,
50 //TODO reason must be INVALID_PARAMS, currently this is available in Device Management interface (DMI),
51 // change below reason with type INVALID_PARAMS once DMI is updated
ssiddiqui6ca40702021-03-08 18:20:21 +053052 Reason: dmi.MetricsConfigurationResponse_INVALID_METRIC,
Humera Kousera4442952020-11-23 23:51:19 +053053 }, status.Errorf(codes.FailedPrecondition, "request is nil")
54 }
55
56 switch x := req.Operation.(type) {
57 case *dmi.MetricsConfigurationRequest_Changes:
58 for _, chMetric := range x.Changes.Metrics {
59 UpdateMetricConfig(chMetric)
60 }
61 case *dmi.MetricsConfigurationRequest_ResetToDefault:
62 logger.Debugf("To be implemented later")
63 case nil:
64 // The field is not set.
65 logger.Debugf("Update request operation type is nil")
66 return &dmi.MetricsConfigurationResponse{
67 Status: dmi.Status_UNDEFINED_STATUS,
68 }, nil
69 }
70
amit.ghosh258d14c2020-10-02 15:13:38 +020071 return &dmi.MetricsConfigurationResponse{
Humera Kousera4442952020-11-23 23:51:19 +053072 Status: dmi.Status_OK_STATUS,
amit.ghosh258d14c2020-10-02 15:13:38 +020073 }, nil
74}
75
76//GetMetric gets the instantenous value of a metric
77func (dms *DmiAPIServer) GetMetric(ctx context.Context, req *dmi.GetMetricRequest) (*dmi.GetMetricResponse, error) {
78 logger.Debugf("GetMetric invoked with request %+v", req)
amit.ghosh258d14c2020-10-02 15:13:38 +020079
Humera Kousera4442952020-11-23 23:51:19 +053080 if req == nil || req.GetMetricId() < 0 {
81 return &dmi.GetMetricResponse{
82 Status: dmi.Status_ERROR_STATUS,
hkouser24361d42020-12-14 19:21:47 +053083 //TODO reason must be INVALID_PARAMS, currently this is not available in Device Management interface (DMI),
Humera Kouser18b275c2020-11-30 11:30:36 +053084 // change below reason with type INVALID_PARAMS once DMI is updated
ssiddiqui6ca40702021-03-08 18:20:21 +053085 Reason: dmi.GetMetricResponse_INVALID_METRIC,
Humera Kousera4442952020-11-23 23:51:19 +053086 Metric: &dmi.Metric{},
87 }, status.Errorf(codes.FailedPrecondition, "request is nil")
88 }
Humera Kouser18b275c2020-11-30 11:30:36 +053089
90 if dms.root == nil {
91 return &dmi.GetMetricResponse{
92 Status: dmi.Status_ERROR_STATUS,
ssiddiqui6ca40702021-03-08 18:20:21 +053093 Reason: dmi.GetMetricResponse_INTERNAL_ERROR,
Humera Kouser18b275c2020-11-30 11:30:36 +053094 Metric: &dmi.Metric{},
95 }, status.Errorf(codes.FailedPrecondition, "Device is not managed, please start managing device to get the metric")
96 }
Humera Kousera4442952020-11-23 23:51:19 +053097 comp := findComponent(dms.root.Children, req.MetaData.ComponentUuid.Uuid)
98 metric := getMetric(comp, req.GetMetricId())
99 return &dmi.GetMetricResponse{
100 Status: dmi.Status_OK_STATUS,
ssiddiqui6ca40702021-03-08 18:20:21 +0530101 Reason: dmi.GetMetricResponse_UNDEFINED_REASON,
Humera Kousera4442952020-11-23 23:51:19 +0530102 Metric: metric,
103 }, nil
amit.ghosh258d14c2020-10-02 15:13:38 +0200104}
Hardik Windlass63e35cf2022-02-03 05:58:09 +0000105
106// Initiates the server streaming of the metrics
107func (dms *DmiAPIServer) StreamMetrics(req *empty.Empty, srv dmi.NativeMetricsManagementService_StreamMetricsServer) error {
108 return status.Errorf(codes.Unimplemented, "rpc StreamMetrics not implemented")
109}