VOL-1180: Enable ponsim to send packet-outs on NNI port.

This required adding the out_port metatdata to the message that
VOLTHA uses to send packets to ponsim, so that VOLTHA can tell
ponsim which port to send the packet out.

Also refactored packet-outs in ponsim so that they don't get run
through the forwarding pipeline, they just get sent out the port.
This is what happens in a normal openflow device.

Change-Id: Ic40ea730f061d82d2ecee33fb1a1f371d0ba73ef
diff --git a/ponsim/v2/core/ponsim_device.go b/ponsim/v2/core/ponsim_device.go
index 3fc52a5..9474432 100644
--- a/ponsim/v2/core/ponsim_device.go
+++ b/ponsim/v2/core/ponsim_device.go
@@ -108,35 +108,11 @@
 	o.Counter.CountRxFrame(port, len(common.GetEthernetLayer(frame).Payload))
 
 	if egressPort, egressFrame := o.processFrame(ctx, port, frame); egressFrame != nil {
-		forwarded := 0
-		links := o.links[int(egressPort)]
-
-		if int(egressPort) <= 2 && int(egressPort) > 0 {
-			o.Counter.CountTxFrame(int(egressPort), len(common.GetEthernetLayer(egressFrame).Payload))
-		}
-
-		for _, link := range links {
-			forwarded += 1
-
-			common.Logger().WithFields(logrus.Fields{
-				"device":      o,
-				"egressPort":  port,
-				"egressFrame": egressFrame,
-			}).Debug("Forwarding packet to link")
-
-			link.(func(int, gopacket.Packet))(int(egressPort), egressFrame)
-		}
-		if forwarded == 0 {
-			common.Logger().WithFields(logrus.Fields{
-				"device": o,
-				"port":   port,
-				"frame":  frame,
-			}).Warn("Nothing was forwarded")
-		}
+		o.SendOut(int(egressPort), egressFrame)
 	} else {
 		common.Logger().WithFields(logrus.Fields{
 			"device": o,
-			"port":   egressPort,
+			"port":   int(egressPort),
 			"frame":  egressFrame,
 		}).Error("Failed to properly process frame")
 	}
@@ -145,6 +121,45 @@
 }
 
 /*
+SendOut send a given frame out the given port
+*/
+func (o *PonSimDevice) SendOut(
+	egressPort int,
+	egressFrame gopacket.Packet,
+) {
+	common.Logger().WithFields(logrus.Fields{
+		"egressPort":  egressPort,
+		"egressFrame": egressFrame,
+	}).Debug("Sending packet out port")
+
+	forwarded := 0
+	links := o.links[egressPort]
+
+	if egressPort <= 2 && egressPort > 0 {
+		o.Counter.CountTxFrame(egressPort, len(common.GetEthernetLayer(egressFrame).Payload))
+	}
+
+	for _, link := range links {
+		forwarded++
+
+		common.Logger().WithFields(logrus.Fields{
+			"device":      o,
+			"egressPort":  egressPort,
+			"egressFrame": egressFrame,
+		}).Debug("Forwarding packet to link")
+
+		link.(func(int, gopacket.Packet))(egressPort, egressFrame)
+	}
+	if forwarded == 0 {
+		common.Logger().WithFields(logrus.Fields{
+			"device": o,
+			"egressPort":   egressPort,
+			"egressFrame":  egressFrame,
+		}).Warn("Nothing was forwarded")
+	}
+}
+
+/*
 connectNetworkInterfaces opens network interfaces for reading and/or writing packets
 */
 func (o *PonSimDevice) connectNetworkInterfaces() {
diff --git a/ponsim/v2/core/ponsim_interface.go b/ponsim/v2/core/ponsim_interface.go
index fbceae2..b6f236a 100644
--- a/ponsim/v2/core/ponsim_interface.go
+++ b/ponsim/v2/core/ponsim_interface.go
@@ -30,4 +30,6 @@
 	GetPort() int32
 
 	Forward(context.Context, int, gopacket.Packet) error
+
+	SendOut(int, gopacket.Packet)
 }
diff --git a/ponsim/v2/grpc/nbi/ponsim_handler.go b/ponsim/v2/grpc/nbi/ponsim_handler.go
index 49fd71a..ec64d5c 100644
--- a/ponsim/v2/grpc/nbi/ponsim_handler.go
+++ b/ponsim/v2/grpc/nbi/ponsim_handler.go
@@ -52,10 +52,11 @@
 
 	common.Logger().WithFields(logrus.Fields{
 		"handler": handler,
+		"out_port": int(data.OutPort),
 		"frame":   frame.Dump(),
 	}).Info("Constructed frame")
 
-	handler.device.Forward(context.Background(), 2, frame)
+	handler.device.SendOut(int(data.OutPort), frame)
 
 	out := new(empty.Empty)
 	return out, nil
@@ -145,7 +146,7 @@
 				},
 			)
 		}
-		out.NniPort = 0
+		out.NniPort = 2
 	} else {
 		common.Logger().WithFields(logrus.Fields{
 			"handler": handler,
diff --git a/voltha/adapters/ponsim_olt/ponsim_olt.py b/voltha/adapters/ponsim_olt/ponsim_olt.py
index b84209c..5ef3fc3 100644
--- a/voltha/adapters/ponsim_olt/ponsim_olt.py
+++ b/voltha/adapters/ponsim_olt/ponsim_olt.py
@@ -427,7 +427,7 @@
         self.alarms = AdapterAlarms(self.adapter, device)
 
         nni_port = Port(
-            port_no=2,
+            port_no=info.nni_port,
             label='NNI facing Ethernet port',
             type=Port.ETHERNET_NNI,
             admin_state=AdminState.ENABLED,
@@ -450,7 +450,7 @@
                 hw_desc='simualted pon',
                 sw_desc='simualted pon',
                 # serial_num=uuid4().hex,
-                serial_num="9b7cfd85441b407c87ee3261df7d4818",
+                serial_num=device.serial_number,
                 dp_desc='n/a'
             ),
             switch_features=ofp_switch_features(
@@ -703,17 +703,22 @@
         self.log.info('sending-packet-out', egress_port=egress_port,
                       msg=hexify(msg))
         pkt = Ether(msg)
-        out_pkt = (
-                Ether(src=pkt.src, dst=pkt.dst) /
-                Dot1Q(vlan=4000) /
-                Dot1Q(vlan=egress_port, type=pkt.type) /
-                pkt.payload
-        )
+        out_pkt = pkt
+        if egress_port != self.nni_port.port_no:
+            # don't do the vlan manipulation for the NNI port, vlans are already correct
+            out_pkt = (
+                    Ether(src=pkt.src, dst=pkt.dst) /
+                    Dot1Q(vlan=egress_port, type=pkt.type) /
+                    pkt.payload
+            )
+
+        # TODO need better way of mapping logical ports to PON ports
+        out_port = self.nni_port.port_no if egress_port == self.nni_port.port_no else 1
 
         if self.ponsim_comm == 'grpc':
             # send over grpc stream
             stub = ponsim_pb2.PonSimStub(self.get_channel())
-            frame = PonSimFrame(id=self.device_id, payload=str(out_pkt))
+            frame = PonSimFrame(id=self.device_id, payload=str(out_pkt), out_port=out_port)
             stub.SendFrame(frame)
         else:
             # send over frameio
diff --git a/voltha/protos/ponsim.proto b/voltha/protos/ponsim.proto
index bdf4394..9d62b63 100644
--- a/voltha/protos/ponsim.proto
+++ b/voltha/protos/ponsim.proto
@@ -31,6 +31,7 @@
 message PonSimFrame {
     string id = 1;
     bytes payload = 2;
+    int32 out_port = 3;
 }
 
 message PonSimPacketCounter {