bit error rate ui and cli for retrieve pre and post fec ber

Change-Id: Ic68ed02ba4516c326baacac814569cb4eebd086f
diff --git a/apps/roadm/app/src/main/java/org/onosproject/roadm/RoadmPortViewMessageHandler.java b/apps/roadm/app/src/main/java/org/onosproject/roadm/RoadmPortViewMessageHandler.java
index 9abd2f1..647b22e 100644
--- a/apps/roadm/app/src/main/java/org/onosproject/roadm/RoadmPortViewMessageHandler.java
+++ b/apps/roadm/app/src/main/java/org/onosproject/roadm/RoadmPortViewMessageHandler.java
@@ -30,6 +30,7 @@
 import org.onosproject.net.OchSignal;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.BitErrorRateState;
 import org.onosproject.net.behaviour.PowerConfig;
 import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState;
 import org.onosproject.net.behaviour.protection.TransportEndpointState;
@@ -94,11 +95,15 @@
     private static final String TARGET_POWER = "targetPower";
     private static final String MODULATION = "modulation";
     private static final String HAS_TARGET_POWER = "hasTargetPower";
+    private static final String PRE_FEC_BER = "preFecBitErrorRate";
+    private static final String POST_FEC_BER = "postFecBitErrorRate";
     private static final String SERVICE_STATE = "serviceState";
 
+
     private static final String[] COLUMN_IDS = {
             ID, REVERSE_PORT, TYPE, NAME, ENABLED, MIN_FREQ, MAX_FREQ, GRID, CURR_FREQ, POWER_RANGE,
-            CURRENT_POWER, CURRENT_INPUT_POWER, SERVICE_STATE, TARGET_POWER, MODULATION, HAS_TARGET_POWER
+            CURRENT_POWER, CURRENT_INPUT_POWER, SERVICE_STATE, TARGET_POWER, MODULATION, HAS_TARGET_POWER,
+            PRE_FEC_BER, POST_FEC_BER
     };
 
     private RoadmService roadmService;
@@ -172,7 +177,9 @@
                     .cell(SERVICE_STATE, getPortServiceState(deviceId, portNum))
                     .cell(MODULATION, getModulation(deviceId, portNum))
                     .cell(TARGET_POWER, getTargetPower(deviceId, portNum))
-                    .cell(HAS_TARGET_POWER, roadmService.hasPortTargetPower(deviceId, portNum));
+                    .cell(HAS_TARGET_POWER, roadmService.hasPortTargetPower(deviceId, portNum))
+                    .cell(PRE_FEC_BER, getPreFecBer(deviceId, portNum))
+                    .cell(POST_FEC_BER, getPostFecBer(deviceId, portNum));
         }
 
         private String getPortServiceState(DeviceId deviceId, PortNumber portNumber) {
@@ -238,6 +245,28 @@
             return RoadmUtil.objectToString(inputPowerVal, RoadmUtil.UNKNOWN);
         }
 
+        // Returns the current input power as a string, Unknown if no value can be found.
+        private String getPreFecBer(DeviceId deviceId, PortNumber portNumber) {
+            BitErrorRateState bitErrorRateState = deviceService.getDevice(deviceId).as(BitErrorRateState.class);
+            Optional<Double> preFecBer = bitErrorRateState.getPreFecBer(deviceId, portNumber);
+            Double preFecBerVal = null;
+            if (preFecBer.isPresent()) {
+                preFecBerVal = preFecBer.orElse(Double.MIN_VALUE);
+            }
+            return RoadmUtil.objectToString(preFecBerVal, RoadmUtil.UNKNOWN);
+        }
+
+        // Returns the current input power as a string, Unknown if no value can be found.
+        private String getPostFecBer(DeviceId deviceId, PortNumber portNumber) {
+            BitErrorRateState bitErrorRateState = deviceService.getDevice(deviceId).as(BitErrorRateState.class);
+            Optional<Double> postFecBer = bitErrorRateState.getPostFecBer(deviceId, portNumber);
+            Double postFecBerVal = null;
+            if (postFecBer.isPresent()) {
+                postFecBerVal = postFecBer.orElse(Double.MIN_VALUE);
+            }
+            return RoadmUtil.objectToString(postFecBerVal, RoadmUtil.UNKNOWN);
+        }
+
         // Returns target power as a string, Unknown if target power is expected but
         // cannot be found, N/A if port does not have configurable target power
         private String getTargetPower(DeviceId deviceId, PortNumber portNumber) {
diff --git a/apps/roadm/web/roadm-gui/lib/port/port.component.html b/apps/roadm/web/roadm-gui/lib/port/port.component.html
index fc5461d..5c82dc4 100644
--- a/apps/roadm/web/roadm-gui/lib/port/port.component.html
+++ b/apps/roadm/web/roadm-gui/lib/port/port.component.html
@@ -77,6 +77,8 @@
                     <td colId="currentInputPower">CURRENT INPUT POWER (dBm)</td>
                     <td colId="targetPower">TARGET OUTPUT POWER (dBm)</td>
                     <td colId="hasTargetPower">HAS TARGET POWER</td>
+                    <td colId="PRE_FEC_BER">PRE FEC BER</td>
+                    <td colId="POST_FEC_BER">POST FEC BER</td>
                     <td colId="serviceState">SERVICE STATE</td>
                 </tr>
             </table>
@@ -122,6 +124,8 @@
                         </form>
                     </td>
                     <td [ngClass]="(isDelta() ? 'delta' : '')">{{port.hasTargetPower}}</td>
+                    <td [ngClass]="(isDelta() ? 'delta' : '')">{{convertNumber(port.preFecBitErrorRate)}}</td>
+                    <td [ngClass]="(isDelta() ? 'delta' : '')">{{convertNumber(port.postFecBitErrorRate)}}</td>
                     <td [ngClass]="(isDelta() ? 'delta' : '')">{{port.serviceState}}</td>
                 </tr>
             </table>
diff --git a/cli/src/main/java/org/onosproject/cli/net/BitErrorCommand.java b/cli/src/main/java/org/onosproject/cli/net/BitErrorCommand.java
new file mode 100644
index 0000000..6cd668c
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/BitErrorCommand.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * This work was partially supported by EC H2020 project METRO-HAUL (761727).
+ */
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.Port;
+import org.onosproject.net.behaviour.BitErrorRateState;
+import org.onosproject.net.device.DeviceService;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Get the bit-error-rate for specific optical-channel.
+ * This is a command for BitErrorRate.
+ */
+@Service
+@Command(scope = "onos", name = "bit-error-rate",
+        description = "Get ber for specific optical-channel")
+public class BitErrorCommand extends AbstractShellCommand {
+
+    private static final Logger log = getLogger(BitErrorCommand.class);
+
+    @Argument(index = 0, name = "connection point", description = "{DeviceID}/{PortNumber}",
+            required = true, multiValued = false)
+    @Completion(OpticalConnectPointCompleter.class)
+    private String connectPoint = null;
+
+    @Override
+    protected void doExecute() throws Exception {
+        DeviceService deviceService = get(DeviceService.class);
+        ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint);
+        Port port = deviceService.getPort(cp);
+        if (port == null) {
+            print("[ERROR] %s does not exist", cp);
+            return;
+        }
+
+        Device device = deviceService.getDevice(cp.deviceId());
+        BitErrorRateState bitErrorRateState = device.as(BitErrorRateState.class);
+        Optional<Double> preFecBerVal = bitErrorRateState.getPreFecBer(cp.deviceId(), cp.port());
+        if (preFecBerVal.isPresent()) {
+            double preFecBer = preFecBerVal.orElse(Double.MIN_VALUE);
+            print("The pre-fec-ber value in port %s on device %s is %f.",
+                  cp.port().toString(), cp.deviceId().toString(), preFecBer);
+        }
+        Optional<Double> postFecBerVal = bitErrorRateState.getPostFecBer(cp.deviceId(), cp.port());
+        if (postFecBerVal.isPresent()) {
+            double postFecBer = postFecBerVal.orElse(Double.MIN_VALUE);
+            print("The post-fec-ber value in port %s on device %s is %f.",
+                  cp.port().toString(), cp.deviceId().toString(), postFecBer);
+        }
+    }
+}