blob: c898c460a81f1679f4140908a253f7ede8ec68e9 [file] [log] [blame]
Rohit Singh35fd94c2019-11-11 16:38:18 +05301/*
2 * Copyright 2019-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 * This Work is contributed by Sterlite Technologies
17 */
18package org.onosproject.rest.resources;
19
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.behaviour.BitErrorRateState;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.rest.AbstractWebResource;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import javax.ws.rs.GET;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37
38/*
39 * Rest APIs to Fetch the BER(bit error rate) pre/post FEC(Forward Error Correction).
40 */
41@Path("bit-error-rate")
42public class BitErrorRateWebResource extends AbstractWebResource {
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 private static final String DEVICE_NOT_FOUND = "Device is not found";
47 private static final String BER_UNSUPPORTED = "Bit Error Rate is not supported";
48 private static final String PRE_FEC_BER = "pre-fec-ber";
49 private static final String POST_FEC_BER = "post-fec-ber";
50
51 /*
52 * This method returns the implemenation of BitErrorRateConfig interface.
53 *
54 * @param id the device identifier
55 * @return instance of BitErrorRateConfig driver implementation
56 */
57 private BitErrorRateState getBitErrorRateState(String id) {
58 Device device = get(DeviceService.class).getDevice(DeviceId.deviceId(id));
59 if (device == null) {
60 throw new IllegalArgumentException(DEVICE_NOT_FOUND);
61 }
62 if (device.is(BitErrorRateState.class)) {
63 return device.as(BitErrorRateState.class);
64 }
65 return null;
66 }
67
68 /*
69 * Get Request to fetch the BER value before FEC.
70 *
71 * @param connectPointStr connectPoint (device/portNumber)
72 * @return Json Response with pre FEC,BER value
73 */
74 @GET
75 @Path("{connectPoint}/pre_fec_ber")
76 @Produces(MediaType.APPLICATION_JSON)
77 public Response getPreFecBerValue(@PathParam("connectPoint") String connectPointStr) {
78 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(connectPointStr);
79 BitErrorRateState bitErrorRateState = getBitErrorRateState(connectPoint.deviceId().toString());
80
81 if (bitErrorRateState == null) {
82 throw new IllegalArgumentException(BER_UNSUPPORTED);
83 }
84 ObjectNode result = encode(connectPoint.deviceId(), connectPoint.port(),
85 bitErrorRateState, PRE_FEC_BER);
86 return ok(result).build();
87 }
88
89 /*
90 * Get Request to fetch the BER value after FEC.
91 *
92 * @param connectPointStr connectPoint (device/portNumber)
93 * @return Json Response with post FEC,BER value
94 */
95 @GET
96 @Path("{connectPoint}/post_fec_ber")
97 @Produces(MediaType.APPLICATION_JSON)
98 public Response getPostFecBerValue(@PathParam("connectPoint") String connectPointStr) {
99 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(connectPointStr);
100 BitErrorRateState bitErrorRateState = getBitErrorRateState(connectPoint.deviceId().toString());
101
102 if (bitErrorRateState == null) {
103 throw new IllegalArgumentException(BER_UNSUPPORTED);
104 }
105 ObjectNode result = encode(connectPoint.deviceId(), connectPoint.port(),
106 bitErrorRateState, POST_FEC_BER);
107 return ok(result).build();
108 }
109
110 /*
111 * This method generates the Json Response for BER.
112 *
113 * @param id device identifier
114 * @param portId port identifier
115 * @param bitErrorRateConfig BitErrorRateConfig object
116 * @param type String value to differentiate Pre/Post Fec, Ber value
117 * @return ObjectNode instance
118 */
119 private ObjectNode encode(DeviceId deviceId, PortNumber port,
120 BitErrorRateState bitErrorRateConfig,
121 String type) {
122 double ber = 0.0;
123
124 if (PRE_FEC_BER.equals(type)) {
125 ber = bitErrorRateConfig.getPreFecBer(deviceId, port).get();
126 } else if (POST_FEC_BER.equals(type)) {
127 ber = bitErrorRateConfig.getPostFecBer(deviceId, port).get();
128 } else {
129 log.error("Invalid type");
130 throw new IllegalArgumentException("Invalid type");
131 }
132 ObjectNode responseNode = mapper().createObjectNode();
133
134 responseNode.put("deviceId", deviceId.toString());
135 responseNode.put("portId", port.toString());
136 responseNode.put("ber", ber);
137
138 return responseNode;
139 }
140}