amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package dmiserver |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 22 | "google.golang.org/grpc/codes" |
| 23 | "google.golang.org/grpc/status" |
| 24 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 25 | dmi "github.com/opencord/device-management-interface/go/dmi" |
| 26 | ) |
| 27 | |
| 28 | //ListMetrics lists the supported metrics for the passed device. |
| 29 | func (dms *DmiAPIServer) ListMetrics(ctx context.Context, req *dmi.HardwareID) (*dmi.ListMetricsResponse, error) { |
| 30 | logger.Debugf("ListMetrics invoked with request %+v", req) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 31 | metrics := getMetricsList() |
| 32 | |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 33 | return &dmi.ListMetricsResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 34 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 35 | 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 |
| 43 | func (dms *DmiAPIServer) UpdateMetricsConfiguration(ctx context.Context, req *dmi.MetricsConfigurationRequest) (*dmi.MetricsConfigurationResponse, error) { |
| 44 | logger.Debugf("UpdateMetricConfiguration invoked with request %+v", req) |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 45 | |
| 46 | if req == nil || req.Operation == nil { |
| 47 | return &dmi.MetricsConfigurationResponse{ |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 48 | 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 Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 51 | 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 70 | return &dmi.MetricsConfigurationResponse{ |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 71 | Status: dmi.Status_OK_STATUS, |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 72 | }, nil |
| 73 | } |
| 74 | |
| 75 | //GetMetric gets the instantenous value of a metric |
| 76 | func (dms *DmiAPIServer) GetMetric(ctx context.Context, req *dmi.GetMetricRequest) (*dmi.GetMetricResponse, error) { |
| 77 | logger.Debugf("GetMetric invoked with request %+v", req) |
amit.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 78 | |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 79 | if req == nil || req.GetMetricId() < 0 { |
| 80 | return &dmi.GetMetricResponse{ |
| 81 | Status: dmi.Status_ERROR_STATUS, |
hkouser | 24361d4 | 2020-12-14 19:21:47 +0530 | [diff] [blame] | 82 | //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] | 83 | // change below reason with type INVALID_PARAMS once DMI is updated |
Humera Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 84 | Reason: dmi.Reason_UNDEFINED_REASON, |
| 85 | Metric: &dmi.Metric{}, |
| 86 | }, status.Errorf(codes.FailedPrecondition, "request is nil") |
| 87 | } |
Humera Kouser | 18b275c | 2020-11-30 11:30:36 +0530 | [diff] [blame] | 88 | |
| 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 Kouser | a444295 | 2020-11-23 23:51:19 +0530 | [diff] [blame] | 96 | 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.ghosh | 258d14c | 2020-10-02 15:13:38 +0200 | [diff] [blame] | 103 | } |