amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 1 | /* |
Joey Armstrong | 3881b73 | 2022-12-27 07:55:37 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 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 | |
| 17 | package dmiserver |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | |
Hardik Windlass | 63e35cf | 2022-02-03 05:58:09 +0000 | [diff] [blame] | 22 | "github.com/golang/protobuf/ptypes/empty" |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
| 24 | "google.golang.org/grpc/status" |
| 25 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 26 | dmi "github.com/opencord/device-management-interface/go/dmi" |
| 27 | ) |
| 28 | |
| 29 | //ListMetrics lists the supported metrics for the passed device. |
| 30 | func (dms *DmiAPIServer) ListMetrics(ctx context.Context, req *dmi.HardwareID) (*dmi.ListMetricsResponse, error) { |
| 31 | logger.Debugf("ListMetrics invoked with request %+v", req) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 32 | metrics := getMetricsList() |
| 33 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 34 | return &dmi.ListMetricsResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 35 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 36 | 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 |
| 44 | func (dms *DmiAPIServer) UpdateMetricsConfiguration(ctx context.Context, req *dmi.MetricsConfigurationRequest) (*dmi.MetricsConfigurationResponse, error) { |
| 45 | logger.Debugf("UpdateMetricConfiguration invoked with request %+v", req) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 46 | |
| 47 | if req == nil || req.Operation == nil { |
| 48 | return &dmi.MetricsConfigurationResponse{ |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 49 | 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 |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 52 | Reason: dmi.MetricsConfigurationResponse_INVALID_METRIC, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 53 | }, 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 71 | return &dmi.MetricsConfigurationResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 72 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 73 | }, nil |
| 74 | } |
| 75 | |
| 76 | //GetMetric gets the instantenous value of a metric |
| 77 | func (dms *DmiAPIServer) GetMetric(ctx context.Context, req *dmi.GetMetricRequest) (*dmi.GetMetricResponse, error) { |
| 78 | logger.Debugf("GetMetric invoked with request %+v", req) |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 79 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 80 | if req == nil || req.GetMetricId() < 0 { |
| 81 | return &dmi.GetMetricResponse{ |
| 82 | Status: dmi.Status_ERROR_STATUS, |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 83 | //TODO reason must be INVALID_PARAMS, currently this is not available in Device Management interface (DMI), |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 84 | // change below reason with type INVALID_PARAMS once DMI is updated |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 85 | Reason: dmi.GetMetricResponse_INVALID_METRIC, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 86 | Metric: &dmi.Metric{}, |
| 87 | }, status.Errorf(codes.FailedPrecondition, "request is nil") |
| 88 | } |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 89 | |
| 90 | if dms.root == nil { |
| 91 | return &dmi.GetMetricResponse{ |
| 92 | Status: dmi.Status_ERROR_STATUS, |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 93 | Reason: dmi.GetMetricResponse_INTERNAL_ERROR, |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 94 | Metric: &dmi.Metric{}, |
| 95 | }, status.Errorf(codes.FailedPrecondition, "Device is not managed, please start managing device to get the metric") |
| 96 | } |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 97 | 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, |
ssiddiqui | 6ca4070 | 2021-03-08 18:20:21 +0530 | [diff] [blame] | 101 | Reason: dmi.GetMetricResponse_UNDEFINED_REASON, |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 102 | Metric: metric, |
| 103 | }, nil |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 104 | } |
Hardik Windlass | 63e35cf | 2022-02-03 05:58:09 +0000 | [diff] [blame] | 105 | |
| 106 | // Initiates the server streaming of the metrics |
| 107 | func (dms *DmiAPIServer) StreamMetrics(req *empty.Empty, srv dmi.NativeMetricsManagementService_StreamMetricsServer) error { |
| 108 | return status.Errorf(codes.Unimplemented, "rpc StreamMetrics not implemented") |
| 109 | } |