blob: e23786f39f9302f92d61de67b2690c2ec14e0732 [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
Humera Kousera4442952020-11-23 23:51:19 +053022 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
24
amit.ghosh258d14c2020-10-02 15:13:38 +020025 dmi "github.com/opencord/device-management-interface/go/dmi"
26)
27
28//ListMetrics lists the supported metrics for the passed device.
29func (dms *DmiAPIServer) ListMetrics(ctx context.Context, req *dmi.HardwareID) (*dmi.ListMetricsResponse, error) {
30 logger.Debugf("ListMetrics invoked with request %+v", req)
Humera Kousera4442952020-11-23 23:51:19 +053031 metrics := getMetricsList()
32
amit.ghosh258d14c2020-10-02 15:13:38 +020033 return &dmi.ListMetricsResponse{
Humera Kousera4442952020-11-23 23:51:19 +053034 Status: dmi.Status_OK_STATUS,
amit.ghosh258d14c2020-10-02 15:13:38 +020035 Reason: 0,
36 Metrics: &dmi.MetricsConfig{
37 Metrics: metrics,
38 },
39 }, nil
40}
41
42//UpdateMetricsConfiguration updates the configuration of the list of metrics in the request
43func (dms *DmiAPIServer) UpdateMetricsConfiguration(ctx context.Context, req *dmi.MetricsConfigurationRequest) (*dmi.MetricsConfigurationResponse, error) {
44 logger.Debugf("UpdateMetricConfiguration invoked with request %+v", req)
Humera Kousera4442952020-11-23 23:51:19 +053045
46 if req == nil || req.Operation == nil {
47 return &dmi.MetricsConfigurationResponse{
Humera Kouser18b275c2020-11-30 11:30:36 +053048 Status: dmi.Status_ERROR_STATUS,
49 //TODO reason must be INVALID_PARAMS, currently this is available in Device Management interface (DMI),
50 // change below reason with type INVALID_PARAMS once DMI is updated
Humera Kousera4442952020-11-23 23:51:19 +053051 Reason: dmi.Reason_UNDEFINED_REASON,
52 }, status.Errorf(codes.FailedPrecondition, "request is nil")
53 }
54
55 switch x := req.Operation.(type) {
56 case *dmi.MetricsConfigurationRequest_Changes:
57 for _, chMetric := range x.Changes.Metrics {
58 UpdateMetricConfig(chMetric)
59 }
60 case *dmi.MetricsConfigurationRequest_ResetToDefault:
61 logger.Debugf("To be implemented later")
62 case nil:
63 // The field is not set.
64 logger.Debugf("Update request operation type is nil")
65 return &dmi.MetricsConfigurationResponse{
66 Status: dmi.Status_UNDEFINED_STATUS,
67 }, nil
68 }
69
amit.ghosh258d14c2020-10-02 15:13:38 +020070 return &dmi.MetricsConfigurationResponse{
Humera Kousera4442952020-11-23 23:51:19 +053071 Status: dmi.Status_OK_STATUS,
amit.ghosh258d14c2020-10-02 15:13:38 +020072 }, nil
73}
74
75//GetMetric gets the instantenous value of a metric
76func (dms *DmiAPIServer) GetMetric(ctx context.Context, req *dmi.GetMetricRequest) (*dmi.GetMetricResponse, error) {
77 logger.Debugf("GetMetric invoked with request %+v", req)
amit.ghosh258d14c2020-10-02 15:13:38 +020078
Humera Kousera4442952020-11-23 23:51:19 +053079 if req == nil || req.GetMetricId() < 0 {
80 return &dmi.GetMetricResponse{
81 Status: dmi.Status_ERROR_STATUS,
hkouser24361d42020-12-14 19:21:47 +053082 //TODO reason must be INVALID_PARAMS, currently this is not available in Device Management interface (DMI),
Humera Kouser18b275c2020-11-30 11:30:36 +053083 // change below reason with type INVALID_PARAMS once DMI is updated
Humera Kousera4442952020-11-23 23:51:19 +053084 Reason: dmi.Reason_UNDEFINED_REASON,
85 Metric: &dmi.Metric{},
86 }, status.Errorf(codes.FailedPrecondition, "request is nil")
87 }
Humera Kouser18b275c2020-11-30 11:30:36 +053088
89 if dms.root == nil {
90 return &dmi.GetMetricResponse{
91 Status: dmi.Status_ERROR_STATUS,
92 Reason: dmi.Reason_INTERNAL_ERROR,
93 Metric: &dmi.Metric{},
94 }, status.Errorf(codes.FailedPrecondition, "Device is not managed, please start managing device to get the metric")
95 }
Humera Kousera4442952020-11-23 23:51:19 +053096 comp := findComponent(dms.root.Children, req.MetaData.ComponentUuid.Uuid)
97 metric := getMetric(comp, req.GetMetricId())
98 return &dmi.GetMetricResponse{
99 Status: dmi.Status_OK_STATUS,
100 Reason: dmi.Reason_UNDEFINED_REASON,
101 Metric: metric,
102 }, nil
amit.ghosh258d14c2020-10-02 15:13:38 +0200103}