net config restinterface along with mvlan delete rest api

Change-Id: I4d8f5829d4e8b08be7981716cb3cde26cc5a507c
diff --git a/internal/pkg/application/vnets.go b/internal/pkg/application/vnets.go
index 1313490..a95104c 100644
--- a/internal/pkg/application/vnets.go
+++ b/internal/pkg/application/vnets.go
@@ -16,25 +16,27 @@
 package application
 
 import (
+	"context"
 	"encoding/json"
 	"errors"
-	"context"
 	"net"
-	infraerrorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"strconv"
 	"sync"
 	"time"
 
+	//errorCodes "voltha-go-controller/internal/pkg/errorcodes"
+
 	"github.com/google/gopacket"
 	"github.com/google/gopacket/layers"
 	"go.uber.org/atomic"
 
+	"voltha-go-controller/database"
 	"voltha-go-controller/internal/pkg/controller"
 	cntlr "voltha-go-controller/internal/pkg/controller"
-	"voltha-go-controller/database"
+
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/internal/pkg/of"
 	"voltha-go-controller/internal/pkg/util"
-	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 )
 
@@ -223,7 +225,7 @@
 	vv.Version = database.PresentVersionMap[database.VnetPath]
 	logger.Debugw(ctx, "Updating VNET....", log.Fields{"vnet": vv})
 	if b, err := json.Marshal(vv); err == nil {
-		if err:= db.PutVnet(cntx, vv.Name, string(b)); err != nil {
+		if err := db.PutVnet(cntx, vv.Name, string(b)); err != nil {
 			logger.Warnw(ctx, "Add Vnet to DB failed", log.Fields{"vnet name": vv.Name, "Error": err})
 		}
 	}
@@ -652,7 +654,7 @@
 	// If the only Igmp Enabled service is removed, remove the Igmp trap flow along with it
 	if service.IgmpEnabled {
 		if err := vpv.DelIgmpFlows(cntx); err != nil {
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		}
 
@@ -775,8 +777,18 @@
 		return
 	}
 
-	if vp := device.GetPort(port); vp != nil {
+	if nniPort := device.GetPort(nni); nniPort != nil {
+		//If NNI port is not mached to nb nni port dont send flows
+		devConfig := GetApplication().GetDeviceConfig(device.SerialNum)
+		if devConfig != nil {
+			if devConfig.UplinkPort != int(nniPort.ID) {
+				logger.Errorw(ctx, "NNI port not configured from NB, not pushing flows", log.Fields{"NNI Port": devConfig.UplinkPort, "NB NNI port": nniPort.ID})
+				return
+			}
+		}
+	}
 
+	if vp := device.GetPort(port); vp != nil {
 		if vpv.PonPort != 0xFF && vpv.PonPort != vp.PonPort {
 			logger.Errorw(ctx, "UNI port discovered on wrong PON Port. Dropping Flow Config for VPV", log.Fields{"Device": device.Name, "Port": port, "DetectedPon": vp.PonPort, "ExpectedPon": vpv.PonPort, "Vnet": vpv.VnetName})
 			return
@@ -835,7 +847,7 @@
 		logger.Infow(ctx, "Port Up - IGMP Flows", log.Fields{"Device": device.Name, "Port": port})
 		vpv.RangeOnServices(cntx, AddSvcUsMeterToDevice)
 		if err := vpv.AddIgmpFlows(cntx); err != nil {
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		}
 
@@ -916,7 +928,7 @@
 	_, err := GetApplication().GetDeviceFromPort(vpv.Port)
 	if err != nil {
 		logger.Warnw(ctx, "Not pushing Service Flows: Error Getting Device", log.Fields{"Reason": err.Error()})
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		return
 	}
@@ -1072,10 +1084,10 @@
 		}
 	}
 
-	_, err := GetApplication().GetDeviceFromPort(vpv.Port)
+	voltDevice, err := GetApplication().GetDeviceFromPort(vpv.Port)
 	if err != nil {
 		logger.Warnw(ctx, "Not pushing Service Flows: Error Getting Device", log.Fields{"Reason": err.Error()})
-		//statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		//statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		//TODO-COMM: 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		return
 	}
@@ -1083,6 +1095,14 @@
 		logger.Warn(ctx, "Not pushing Service Flows: Service Not activated")
 		return
 	}
+
+	//If NNI port is not mached to nb nni port
+	devConfig := GetApplication().GetDeviceConfig(voltDevice.SerialNum)
+
+	if strconv.Itoa(devConfig.UplinkPort) != voltDevice.NniPort {
+		logger.Errorw(ctx, "NNI port mismatch", log.Fields{"NNI Port": devConfig.UplinkPort, "NB NNI port": voltDevice.NniPort})
+		return
+	}
 	//Push Service Flows if DHCP relay is not configured
 	//or already DHCP flows are configured for the VPV
 	//to which the serivce is associated
@@ -1090,7 +1110,7 @@
 		if NonZeroMacAddress(vpv.MacAddr) || svc.MacLearning == MacLearningNone {
 			svc.AddHsiaFlows(cntx)
 		} else {
-			if err:= svc.AddUsHsiaFlows(cntx); err != nil {
+			if err := svc.AddUsHsiaFlows(cntx); err != nil {
 				logger.Warnw(ctx, "Add US hsia flow failed", log.Fields{"service": svc.Name, "Error": err})
 			}
 		}
@@ -1102,7 +1122,7 @@
 	if svc.IgmpEnabled && vpv.FlowsApplied {
 		logger.Infow(ctx, "Add Service - IGMP Flows", log.Fields{"Device": vpv.Device, "Port": vpv.Port})
 		if err := vpv.AddIgmpFlows(cntx); err != nil {
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		}
 
@@ -1149,7 +1169,7 @@
 // AddUsHsiaFlows to add upstream hsia flows
 func AddUsHsiaFlows(cntx context.Context, key, value interface{}) bool {
 	svc := value.(*VoltService)
-	if err:= svc.AddUsHsiaFlows(cntx); err != nil {
+	if err := svc.AddUsHsiaFlows(cntx); err != nil {
 		logger.Warnw(ctx, "Add US hsia flow failed", log.Fields{"service": svc.Name, "Error": err})
 	}
 	return true
@@ -1158,7 +1178,7 @@
 // AddDsHsiaFlows to add downstream hsia flows
 func AddDsHsiaFlows(cntx context.Context, key, value interface{}) bool {
 	svc := value.(*VoltService)
-	if err:= svc.AddDsHsiaFlows(cntx); err != nil {
+	if err := svc.AddDsHsiaFlows(cntx); err != nil {
 		logger.Warnw(ctx, "Add DS hsia flow failed", log.Fields{"service": svc.Name, "Error": err})
 	}
 	return true
@@ -1185,7 +1205,7 @@
 // DelDsHsiaFlows to delete hsia flows
 func DelDsHsiaFlows(cntx context.Context, key, value interface{}) bool {
 	svc := value.(*VoltService)
-	if err:= svc.DelDsHsiaFlows(cntx); err != nil {
+	if err := svc.DelDsHsiaFlows(cntx); err != nil {
 		logger.Warnw(ctx, "Delete DS hsia flow failed", log.Fields{"service": svc.Name, "Error": err})
 	}
 	return true
@@ -1194,7 +1214,7 @@
 // DelUsHsiaFlows to delete upstream hsia flows
 func DelUsHsiaFlows(cntx context.Context, key, value interface{}) bool {
 	svc := value.(*VoltService)
-	if err:= svc.DelUsHsiaFlows(cntx); err != nil {
+	if err := svc.DelUsHsiaFlows(cntx); err != nil {
 		logger.Warnw(ctx, "Delete US hsia flow failed", log.Fields{"service": svc.Name, "Error": err})
 	}
 	return true
@@ -1226,11 +1246,11 @@
 	if !vpv.FlowsApplied || vgcRebooted {
 		if vpv.DhcpRelay {
 			if err := vpv.AddUsDhcpFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			if err := vpv.AddDsDhcpFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			logger.Infow(ctx, "ICMPv6 MC Group modification will not be triggered to rwcore for ",
@@ -1238,18 +1258,18 @@
 			//vpv.updateICMPv6McGroup(true)
 		} else if vpv.ArpRelay {
 			if err := vpv.AddUsArpFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			logger.Info(ctx, "ARP trap rules not added in downstream direction")
 
 		} else if vpv.PppoeIa {
 			if err := vpv.AddUsPppoeFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			if err := vpv.AddDsPppoeFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 		}
@@ -1264,36 +1284,36 @@
 	// Delete HSIA & DHCP flows before deleting IGMP flows
 	if vpv.FlowsApplied || vgcRebooted {
 		if vpv.DhcpRelay {
-			if err:= vpv.DelUsDhcpFlows(cntx); err != nil {
+			if err := vpv.DelUsDhcpFlows(cntx); err != nil {
 				logger.Warnw(ctx, "Delete US hsia flow failed", log.Fields{"port": vpv.Port, "SVlan": vpv.SVlan, "CVlan": vpv.CVlan,
 					"UniVlan": vpv.UniVlan, "Error": err})
 			}
 			logger.Infow(ctx, "ICMPv6 MC Group modification will not be triggered to rwcore  for ",
 				log.Fields{"port": vpv.Port})
 			if err := vpv.DelDsDhcpFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			//vpv.updateICMPv6McGroup(false)
 		} else if vpv.ArpRelay {
 			if err := vpv.DelUsArpFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 		} else if vpv.PppoeIa {
 			if err := vpv.DelUsPppoeFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 			if err := vpv.DelDsPppoeFlows(cntx); err != nil {
-				statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+				statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 				vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 			}
 		}
 		vpv.FlowsApplied = false
 		vpv.WriteToDb(cntx)
 	}
-	if err:= vpv.DelIgmpFlows(cntx); err != nil {
+	if err := vpv.DelIgmpFlows(cntx); err != nil {
 		logger.Warnw(ctx, "Delete igmp flow failed", log.Fields{"port": vpv.Port, "SVlan": vpv.SVlan, "CVlan": vpv.CVlan,
 			"UniVlan": vpv.UniVlan, "Error": err})
 	}
@@ -1334,33 +1354,16 @@
 		logger.Debugw(ctx, "Adding US DHCP flows", log.Fields{"Device": device})
 		if err1 := vpv.PushFlows(cntx, vd, flows); err1 != nil {
 			//push ind here ABHI
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err1)
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err1)
 			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 		}
 	} else {
 		logger.Errorw(ctx, "US DHCP Flow Add Failed", log.Fields{"Reason": err.Error(), "Device": device})
 		//push ind here ABHI
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 
 	}
-	/*
-	flows, err = vpv.BuildUsDhcp6Flows()
-	if err == nil {
-		logger.Debugw(ctx, "Adding US DHCP6 flows", log.Fields{"Device": device})
-		if err1 := vpv.PushFlows(vd, flows); err1 != nil {
-			//pussh ind here ABHI
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err1)
-			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-
-		}
-	} else {
-		logger.Errorw(ctx, "US DHCP6 Flow Add Failed", log.Fields{"Reason": err.Error(), "Device": device})
-		//push ind here ABHI
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
-		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-
-	}*/
 	return nil
 }
 
@@ -1388,33 +1391,17 @@
 	if err == nil {
 		if err1 := vpv.PushFlows(cntx, vd, flows); err1 != nil {
 			//push ind here and procced
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err1)
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err1)
 			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 
 		}
 	} else {
 		logger.Errorw(ctx, "DS DHCP Flow Add Failed", log.Fields{"Reason": err.Error()})
 		//send ind here and proceed
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 
 	}
-	/*
-	flows, err = vpv.BuildDsDhcp6Flows()
-	if err == nil {
-		if err1 := vpv.PushFlows(vd, flows); err1 != nil {
-			//push ind and proceed
-			statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err1)
-			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-
-		}
-	} else {
-		logger.Errorw(ctx, "DS DHCP6 Flow Add Failed", log.Fields{"Reason": err.Error()})
-		//Send ind here and proceed
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
-		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-
-	}*/
 	if GetApplication().GetVendorID() != Radisys {
 		vd.GlobalDhcpFlowAdded = true
 	}
@@ -1424,12 +1411,12 @@
 // DelDhcpFlows deletes both US & DS DHCP flows applied for this Vnet instantiated on the port
 func (vpv *VoltPortVnet) DelDhcpFlows(cntx context.Context) {
 	if err := vpv.DelUsDhcpFlows(cntx); err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 	}
 
 	if err := vpv.DelDsDhcpFlows(cntx); err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 	}
 }
@@ -1445,15 +1432,10 @@
 
 	err = vpv.delDhcp4Flows(cntx, device)
 	if err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 	}
-	/*
-	err = vpv.delDhcp6Flows(device)
-	if err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
-		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-	}*/
+
 	return nil
 }
 
@@ -1465,16 +1447,6 @@
 	logger.Errorw(ctx, "US DHCP Flow Delete Failed", log.Fields{"Reason": err.Error()})
 	return err
 }
-/*
-func (vpv *VoltPortVnet) delDhcp6Flows(device *VoltDevice) error {
-	flows, err := vpv.BuildUsDhcp6Flows()
-	if err == nil {
-		return vpv.RemoveFlows(device, flows)
-	}
-	logger.Errorw(ctx, "US DHCP6 Flow Delete Failed", log.Fields{"Reason": err.Error()})
-	return err
-
-}*/
 
 // DelDsDhcpFlows delete the DHCP flows applied for this Vnet instantiated on the port
 // Write the status of the VPV to the DB once the delete is scheduled
@@ -1486,15 +1458,15 @@
 	}
 	err = vpv.delDsDhcp4Flows(cntx, device)
 	if err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
+		statusCode, statusMessage := errorCodes.GetErrorInfo(err)
 		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
 	}
 	/*
-	err = vpv.delDsDhcp6Flows(device)
-	if err != nil {
-		statusCode, statusMessage := infraerrorCodes.GetErrorInfo(err)
-		vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
-	}*/
+		err = vpv.delDsDhcp6Flows(device)
+		if err != nil {
+			statusCode, statusMessage := errorCodes.GetErrorInfo(err)
+			vpv.FlowInstallFailure("VGC processing failure", statusCode, statusMessage)
+		}*/
 	return nil
 }
 
@@ -2126,14 +2098,8 @@
 	subFlow := of.NewVoltSubFlow()
 	subFlow.SetTableID(0)
 
-        if GetApplication().GetVendorID() == Radisys {
-                if err := vpv.setUsMatchVlan(subFlow); err != nil {
-                        return nil, err
-                }
-        } else {
-                subFlow.SetMatchVlan(vpv.UniVlan)
-                subFlow.SetSetVlan(vpv.CVlan)
-        }
+	subFlow.SetMatchVlan(vpv.UniVlan)
+	subFlow.SetSetVlan(vpv.CVlan)
 
 	uniport, err := GetApplication().GetPortID(vpv.Port)
 	if err != nil {
@@ -3226,7 +3192,7 @@
 func (vv *VoltVnet) JsonMarshal() ([]byte, error) {
 	return json.Marshal(VoltVnet{
 		VnetConfig: vv.VnetConfig,
-		Version: vv.Version,
+		Version:    vv.Version,
 		VnetOper: VnetOper{
 			PendingDeleteFlow:     vv.VnetOper.PendingDeleteFlow,
 			DeleteInProgress:      vv.VnetOper.DeleteInProgress,