Fix for meter delete and meter state update

Change-Id: Ic0945a6785da87aa66ab9620b0a599d75e563c78
diff --git a/internal/pkg/application/service.go b/internal/pkg/application/service.go
index 9315b5b..e129b48 100644
--- a/internal/pkg/application/service.go
+++ b/internal/pkg/application/service.go
@@ -1174,47 +1174,39 @@
 		logger.Warnw(ctx, "Deleted service from DB/Cache successfully", log.Fields{"serviceName": vs.Name})
 	}
 
-	meterProfiles := make(map[*VoltMeter]bool)
-
 	if nil != newSvc {
 		logger.Infow(ctx, "Old Service meter profiles", log.Fields{"AGG": vs.AggDsMeterProfile, "DS": vs.DsMeterProfile, "US": vs.UsMeterProfile})
 		logger.Infow(ctx, "New Service meter profiles", log.Fields{"AGG": newSvc.AggDsMeterProfile, "DS": newSvc.DsMeterProfile, "US": newSvc.UsMeterProfile})
 	}
-	skipMeterDeletion := false
-	if aggMeter, ok := va.MeterMgr.GetMeterByID(vs.AggDsMeterID); ok {
-		if nil != newSvc && aggMeter.Name == newSvc.AggDsMeterProfile {
-			skipMeterDeletion = true
-		}
 
-		meterProfiles[aggMeter] = skipMeterDeletion
-		skipMeterDeletion = false
+	logger.Infow(ctx, "About to mark meter for deletion\n", log.Fields{"serviceName": vs.Name})
+
+	if aggMeter, ok := va.MeterMgr.GetMeterByID(vs.AggDsMeterID); ok {
+		if nil == newSvc || (nil != newSvc && aggMeter.Name != newSvc.AggDsMeterProfile) {
+                       if aggMeter.AssociatedServices > 0 {
+                               aggMeter.AssociatedServices--
+                               logger.Infow(ctx, "Agg Meter assocaited services updated\n", log.Fields{"MeterID": aggMeter})
+                               va.UpdateMeterProf(cntx, *aggMeter)
+                       }
+		}
 	}
 	if dsMeter, ok := va.MeterMgr.GetMeterByID(vs.DsMeterID); ok {
-		if nil != newSvc && dsMeter.Name == newSvc.DsMeterProfile {
-			skipMeterDeletion = true
+	       if nil == newSvc || (nil != newSvc && dsMeter.Name != newSvc.DsMeterProfile) {
+                       if dsMeter.AssociatedServices > 0 {
+                               dsMeter.AssociatedServices--
+                               logger.Infow(ctx, "DS Meter assocaited services updated\n", log.Fields{"MeterID": dsMeter})
+                               va.UpdateMeterProf(cntx, *dsMeter)
+                       }
 		}
-		meterProfiles[dsMeter] = skipMeterDeletion
-		skipMeterDeletion = false
 	}
 	if vs.AggDsMeterID != vs.UsMeterID {
 		if usMeter, ok := va.MeterMgr.GetMeterByID(vs.UsMeterID); ok {
-			if nil != newSvc && usMeter.Name == newSvc.UsMeterProfile {
-				skipMeterDeletion = true
-			}
-			meterProfiles[usMeter] = skipMeterDeletion
-		}
-	}
-
-	for meter, skipMeterDeletion := range meterProfiles {
-		if nil == meter {
-			logger.Debug(ctx, "Null meter found, continuing")
-			continue
-		}
-		if meter.AssociatedServices > 0 {
-			meter.AssociatedServices--
-			if meter.AssociatedServices == 0 && !skipMeterDeletion {
-				logger.Infow(ctx, "Meter should be deleted now\n", log.Fields{"MeterID": meter})
-				va.UpdateMeterProf(cntx, *meter)
+                       if nil == newSvc || (nil != newSvc && usMeter.Name != newSvc.UsMeterProfile) {
+                               if usMeter.AssociatedServices > 0 {
+                                       usMeter.AssociatedServices--
+                                       logger.Infow(ctx, "US Meter assocaited services updated\n", log.Fields{"MeterID": usMeter})
+                                       va.UpdateMeterProf(cntx, *usMeter)
+                               }
 			}
 		}
 	}
diff --git a/internal/pkg/controller/device.go b/internal/pkg/controller/device.go
index 8d185a3..a3527c8 100644
--- a/internal/pkg/controller/device.go
+++ b/internal/pkg/controller/device.go
@@ -393,6 +393,19 @@
 	return nil
 }
 
+// UpdateMeter to update meter
+func (d *Device) UpdateMeter(cntx context.Context, meter *of.Meter) error {
+       d.meterLock.Lock()
+       defer d.meterLock.Unlock()
+       if _, ok := d.meters[meter.ID]; ok {
+               d.meters[meter.ID] = meter
+               d.AddMeterToDb(cntx, meter)
+       } else {
+               return errors.New("Meter not found for updation")
+       }
+       return nil
+}
+
 // GetMeter to get meter
 func (d *Device) GetMeter(id uint32) (*of.Meter, error) {
 	d.meterLock.RLock()
diff --git a/internal/pkg/controller/modmeter.go b/internal/pkg/controller/modmeter.go
index 62d2f40..acbef44 100644
--- a/internal/pkg/controller/modmeter.go
+++ b/internal/pkg/controller/modmeter.go
@@ -93,6 +93,7 @@
 			// Meter already exists so we dont have to do anything here
 			return nil
 		}
+		logger.Infow(ctx, "Updated meter state to pending", log.Fields{"Meter": mmt.meter.ID})
 	} else {
 		if !mmt.device.DelMeter(ctx, mmt.meter) {
 			// Meter doesn't exist so we dont have to do anything here
@@ -114,6 +115,13 @@
 
 		if _, err = vc.UpdateLogicalDeviceMeterTable(mmt.ctx, meterMod); err != nil {
 			logger.Errorw(ctx, "Update Meter Table Failed", log.Fields{"Reason": err.Error()})
+		} else {
+			mmt.meter.State = of.MeterOperSuccess
+			if err := mmt.device.UpdateMeter(ctx, mmt.meter); err != nil {
+				// Meter does not exist, update failed
+				logger.Error(ctx, "Update meter to DB failed")
+			}
+			logger.Infow(ctx, "Updated meter state to success", log.Fields{"Meter": mmt.meter.ID})
 		}
 		//triggerMeterNotification(err)
 		return err