blob: 3f339418c97ea4ea582d5e51e0b719cb5dbf232d [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{
48 Status: dmi.Status_UNDEFINED_STATUS,
49 Reason: dmi.Reason_UNDEFINED_REASON,
50 }, status.Errorf(codes.FailedPrecondition, "request is nil")
51 }
52
53 switch x := req.Operation.(type) {
54 case *dmi.MetricsConfigurationRequest_Changes:
55 for _, chMetric := range x.Changes.Metrics {
56 UpdateMetricConfig(chMetric)
57 }
58 case *dmi.MetricsConfigurationRequest_ResetToDefault:
59 logger.Debugf("To be implemented later")
60 case nil:
61 // The field is not set.
62 logger.Debugf("Update request operation type is nil")
63 return &dmi.MetricsConfigurationResponse{
64 Status: dmi.Status_UNDEFINED_STATUS,
65 }, nil
66 }
67
amit.ghosh258d14c2020-10-02 15:13:38 +020068 return &dmi.MetricsConfigurationResponse{
Humera Kousera4442952020-11-23 23:51:19 +053069 Status: dmi.Status_OK_STATUS,
amit.ghosh258d14c2020-10-02 15:13:38 +020070 }, nil
71}
72
73//GetMetric gets the instantenous value of a metric
74func (dms *DmiAPIServer) GetMetric(ctx context.Context, req *dmi.GetMetricRequest) (*dmi.GetMetricResponse, error) {
75 logger.Debugf("GetMetric invoked with request %+v", req)
amit.ghosh258d14c2020-10-02 15:13:38 +020076
Humera Kousera4442952020-11-23 23:51:19 +053077 if req == nil || req.GetMetricId() < 0 {
78 return &dmi.GetMetricResponse{
79 Status: dmi.Status_ERROR_STATUS,
80 Reason: dmi.Reason_UNDEFINED_REASON,
81 Metric: &dmi.Metric{},
82 }, status.Errorf(codes.FailedPrecondition, "request is nil")
83 }
84 comp := findComponent(dms.root.Children, req.MetaData.ComponentUuid.Uuid)
85 metric := getMetric(comp, req.GetMetricId())
86 return &dmi.GetMetricResponse{
87 Status: dmi.Status_OK_STATUS,
88 Reason: dmi.Reason_UNDEFINED_REASON,
89 Metric: metric,
90 }, nil
amit.ghosh258d14c2020-10-02 15:13:38 +020091}