[VOL-5536] - VGC recent fixes

Change-Id: Id6f0e647b37baac827230afbb45d132df8a62b68
Signed-off-by: Sridhar Ravindra <sridhar.ravindra@radisys.com>
diff --git a/internal/pkg/controller/controller.go b/internal/pkg/controller/controller.go
index 1c02738..ba33d78 100644
--- a/internal/pkg/controller/controller.go
+++ b/internal/pkg/controller/controller.go
@@ -131,8 +131,6 @@
 
 	d.RestoreMetersFromDb(cntx)
 	d.RestoreGroupsFromDb(cntx)
-	d.RestoreFlowsFromDb(cntx)
-	d.RestorePortsFromDb(cntx)
 	d.ConnectInd(context.TODO(), intf.DeviceDisc)
 	d.packetOutChannel = config.PacketOutChannel
 
@@ -280,6 +278,10 @@
 	v.app.CheckAndDeactivateService(ctx, flow, devSerialNum, devID)
 }
 
+func (v *VoltController) GetAllFlowsForSvc(ctx context.Context, flow *of.VoltSubFlow, devID string, devSerialNum string) []uint64 {
+	return v.app.GetAllFlowsForSvc(ctx, flow, devID, devSerialNum)
+}
+
 // AddVPAgent to add the vpagent
 func (v *VoltController) AddVPAgent(vep string, vpa *vpagent.VPAgent) {
 	v.vagent[vep] = vpa
@@ -306,7 +308,7 @@
 }
 
 // AddFlows to add flows
-func (v *VoltController) AddFlows(cntx context.Context, port string, device string, flow *of.VoltFlow) error {
+func (v *VoltController) AddFlows(cntx context.Context, port string, device string, flow *of.VoltFlow, skipFlowPushToVoltha bool) error {
 	d, err := v.GetDevice(device)
 	if err != nil {
 		logger.Errorw(ctx, "Device Not Found", log.Fields{"Device": device})
@@ -347,10 +349,28 @@
 			}
 		}
 	} else {
-		flow.Command = of.CommandAdd
-		d.UpdateFlows(flow, devPort)
-		for cookie := range flow.SubFlows {
-			logger.Debugw(ctx, "Flow Add added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
+		flowsToVoltha := &of.VoltFlow{}
+		flowsToVoltha.SubFlows = make(map[uint64]*of.VoltSubFlow)
+		// During VGC restart, build and add flows only to cache.
+		// No need to push flows to voltha now as it will be audited later
+		for _, subFlow := range flow.SubFlows {
+			logger.Debugw(ctx, "Adding flows to device cache", log.Fields{"Cookie": subFlow.Cookie})
+			if !skipFlowPushToVoltha {
+				subFlow.State = of.FlowAddPending
+			}
+			if err := d.AddFlow(cntx, subFlow); err != nil {
+				logger.Warnw(ctx, "Add Flow Error", log.Fields{"Cookie": subFlow.Cookie, "Reason": err.Error()})
+			} else {
+				flowsToVoltha.SubFlows[subFlow.Cookie] = subFlow
+			}
+		}
+
+		if !skipFlowPushToVoltha {
+			flowsToVoltha.Command = of.CommandAdd
+			d.UpdateFlows(flowsToVoltha, devPort)
+			for cookie := range flowsToVoltha.SubFlows {
+				logger.Debugw(ctx, "Flow Add added to queue", log.Fields{"Cookie": cookie, "Device": device, "Port": port})
+			}
 		}
 	}
 	return nil
@@ -471,8 +491,8 @@
 }
 
 // PortUpInd for port up indication
-func (v *VoltController) PortUpInd(cntx context.Context, device string, port string) {
-	v.app.PortUpInd(cntx, device, port)
+func (v *VoltController) PortUpInd(cntx context.Context, device string, port string, flag bool) {
+	v.app.PortUpInd(cntx, device, port, flag)
 }
 
 // PortDownInd for port down indication