FTTH-50934 Fix the error response for the VGC NB Apis

Change-Id: Ibeb54b0a7a370a376b222b83154e2bb54250c863
diff --git a/internal/pkg/application/service.go b/internal/pkg/application/service.go
index 1061e21..5d7bc43 100644
--- a/internal/pkg/application/service.go
+++ b/internal/pkg/application/service.go
@@ -1110,11 +1110,13 @@
 
 // DelServiceWithPrefix - Deletes service with the provided prefix.
 // Added for DT/TT usecase with sadis replica interface
-func (va *VoltApplication) DelServiceWithPrefix(cntx context.Context, prefix string) {
+func (va *VoltApplication) DelServiceWithPrefix(cntx context.Context, prefix string) error {
+	var isServiceExist bool
 	va.ServiceByName.Range(func(key, value interface{}) bool {
 		srvName := key.(string)
 		vs := value.(*VoltService)
 		if strings.Contains(srvName, prefix) {
+			isServiceExist = true
 			va.DelService(cntx, srvName, true, nil, false)
 
 			vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-"
@@ -1127,6 +1129,11 @@
 		}
 		return true
 	})
+
+	if !isServiceExist {
+		return errorCodes.ErrServiceNotFound
+	}
+	return nil
 }
 
 // DelService delete a service form the application
@@ -2025,6 +2032,7 @@
 
 // ActivateService to activate pre-provisioned service
 func (va *VoltApplication) ActivateService(cntx context.Context, deviceID, portNo string, sVlan, cVlan of.VlanType, tpID uint16) error {
+	var isParmsInvalid bool
 	logger.Infow(ctx, "Service Activate Request ", log.Fields{"Device": deviceID, "Port": portNo})
 	device, err := va.GetDeviceFromPort(portNo)
 	if err != nil {
@@ -2043,9 +2051,11 @@
 		// If svlan if provided, then the tags and tpID of service has to be matching
 		if sVlan != of.VlanNone && (sVlan != vs.SVlan || cVlan != vs.CVlan || tpID != vs.TechProfileID) {
 			logger.Infow(ctx, "Service Activate Request Does not match", log.Fields{"Device": deviceID, "voltService": vs})
+			isParmsInvalid = true
 			return true
 		}
 		if portNo == vs.Port && !vs.IsActivated {
+			isParmsInvalid = false
 			p := device.GetPort(vs.Port)
 			if p == nil {
 				logger.Warnw(ctx, "Wrong device or port", log.Fields{"Device": deviceID, "Port": portNo})
@@ -2067,21 +2077,31 @@
 		}
 		return true
 	})
+
+	if isParmsInvalid {
+		return errorCodes.ErrInvalidParamInRequest
+	}
 	return nil
 }
 
 // DeactivateService to activate pre-provisioned service
 func (va *VoltApplication) DeactivateService(cntx context.Context, deviceID, portNo string, sVlan, cVlan of.VlanType, tpID uint16) error {
 	logger.Infow(ctx, "Service Deactivate Request ", log.Fields{"Device": deviceID, "Port": portNo})
+	var isServiceExist bool
+	var isParmsInvalid bool
+
 	va.ServiceByName.Range(func(key, value interface{}) bool {
 		vs := value.(*VoltService)
 		// If svlan if provided, then the tags and tpID of service has to be matching
 		logger.Infow(ctx, "Service Deactivate Request ", log.Fields{"Device": deviceID, "Port": portNo})
 		if sVlan != of.VlanNone && (sVlan != vs.SVlan || cVlan != vs.CVlan || tpID != vs.TechProfileID) {
 			logger.Infow(ctx, "condition not matched", log.Fields{"Device": deviceID, "Port": portNo, "sVlan": sVlan, "cVlan": cVlan, "tpID": tpID})
+			isParmsInvalid = true
 			return true
 		}
 		if portNo == vs.Port && vs.IsActivated {
+			isServiceExist = true
+			isParmsInvalid = false
 			vs.IsActivated = false
 			vs.DeactivateInProgress = true
 			va.ServiceByName.Store(vs.Name, vs)
@@ -2109,6 +2129,12 @@
 		}
 		return true
 	})
+
+	if isParmsInvalid {
+		return errorCodes.ErrInvalidParamInRequest
+	} else if !isServiceExist && !isParmsInvalid {
+		return errorCodes.ErrPortNotFound
+	}
 	return nil
 }
 
diff --git a/internal/pkg/application/service_test.go b/internal/pkg/application/service_test.go
index c687e55..1c66cb6 100644
--- a/internal/pkg/application/service_test.go
+++ b/internal/pkg/application/service_test.go
@@ -932,9 +932,8 @@
 				}
 			case "sVlan != of.VlanNone":
 				va.ServiceByName.Store("test_name", voltServiceTest)
-				if err := va.DeactivateService(tt.args.cntx, tt.args.deviceID, tt.args.portNo, tt.args.sVlan, tt.args.cVlan, tt.args.tpID); (err != nil) != tt.wantErr {
-					t.Errorf("VoltApplication.DeactivateService() error = %v, wantErr %v", err, tt.wantErr)
-				}
+				err := va.DeactivateService(tt.args.cntx, tt.args.deviceID, tt.args.portNo, tt.args.sVlan, tt.args.cVlan, tt.args.tpID)
+				assert.NotNil(t, err)
 			case GetDeviceFromPort_error:
 				va.ServiceByName.Store("test_name", voltServiceTest)
 				if err := va.DeactivateService(tt.args.cntx, tt.args.deviceID, tt.args.portNo, tt.args.sVlan, tt.args.cVlan, tt.args.tpID); (err != nil) != tt.wantErr {
@@ -1089,9 +1088,8 @@
 				va.PortsDisc.Store("test_port", voltPortTest)
 				va.DevicesDisc.Store(test_device, voltDevice)
 				va.ServiceByName.Store("test_name", voltServiceTest)
-				if err := va.ActivateService(tt.args.cntx, tt.args.deviceID, tt.args.portNo, tt.args.sVlan, tt.args.cVlan, tt.args.tpID); (err != nil) != tt.wantErr {
-					t.Errorf("VoltApplication.ActivateService() error = %v, wantErr %v", err, tt.wantErr)
-				}
+				err := va.ActivateService(tt.args.cntx, tt.args.deviceID, tt.args.portNo, tt.args.sVlan, tt.args.cVlan, tt.args.tpID)
+				assert.NotNil(t, err)
 			}
 		})
 	}
@@ -2308,7 +2306,8 @@
 			}
 			va.VnetsBySvlan.Set(of.VlanAny, cuncurrentMap)
 			dbintf.EXPECT().DelVnet(gomock.Any(), gomock.Any()).Return(nil).Times(1)
-			va.DelServiceWithPrefix(tt.args.cntx, tt.args.prefix)
+			err := va.DelServiceWithPrefix(tt.args.cntx, tt.args.prefix)
+			assert.Nil(t, err)
 		})
 	}
 }
diff --git a/internal/pkg/errorcodes/errorcodes.go b/internal/pkg/errorcodes/errorcodes.go
index 68aa55c..db4ae51 100644
--- a/internal/pkg/errorcodes/errorcodes.go
+++ b/internal/pkg/errorcodes/errorcodes.go
@@ -76,6 +76,9 @@
 	ErrImageNotRegistered = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, PrerequisiteNotMet, "Image is not registered")
 	// ErrImageDownloadInProgress is returned when the image download is in progress
 	ErrImageDownloadInProgress = status.Errorf(codes.FailedPrecondition, VolthaErrorMessageFormat, MethodNotAllowed, "Image download is in progress")
+
+	// ErrServiceNotFound is returned when the Service is not present in VOLTHA
+	ErrServiceNotFound = status.Errorf(codes.NotFound, VolthaErrorMessageFormat, ResourceNotFound, "Service not found")
 )
 
 // ConvertToVolthaErrorFormat converts the error to Voltha error format
diff --git a/voltha-go-controller/nbi/netConfig.go b/voltha-go-controller/nbi/netConfig.go
index deca2b3..fd54248 100644
--- a/voltha-go-controller/nbi/netConfig.go
+++ b/voltha-go-controller/nbi/netConfig.go
@@ -21,6 +21,7 @@
 	"encoding/json"
 	"net/http"
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 )
 
@@ -69,6 +70,8 @@
 		nch.AddNetConfigInfo(ctx, w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
diff --git a/voltha-go-controller/nbi/subscriber.go b/voltha-go-controller/nbi/subscriber.go
index 882bc61..7fa3060 100644
--- a/voltha-go-controller/nbi/subscriber.go
+++ b/voltha-go-controller/nbi/subscriber.go
@@ -25,6 +25,7 @@
 
 	"voltha-go-controller/internal/pkg/application"
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/internal/pkg/of"
 	"voltha-go-controller/log"
 
@@ -92,6 +93,8 @@
 		sh.DelSubscriberInfo(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -241,9 +244,6 @@
 
 	logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id})
 
-	// HTTP response with 202 accepted for service delete request
-	w.WriteHeader(http.StatusAccepted)
-
 	d := new(bytes.Buffer)
 	if _, err := d.ReadFrom(r.Body); err != nil {
 		logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
@@ -272,5 +272,13 @@
 	}
 
 	logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id})
-	app.GetApplication().DelServiceWithPrefix(cntx, id)
+	err := app.GetApplication().DelServiceWithPrefix(cntx, id)
+	if err != nil {
+		logger.Warnw(ctx, "northbound-del-service-req failed, Subscriber not exist", log.Fields{"ServiceName": id})
+		http.Error(w, err.Error(), http.StatusBadRequest)
+		return
+	}
+
+	// HTTP response with 202 accepted for service delete request
+	w.WriteHeader(http.StatusAccepted)
 }
diff --git a/voltha-go-controller/onos_nbi/deviceconfig.go b/voltha-go-controller/onos_nbi/deviceconfig.go
index 3177b0a..709ba44 100644
--- a/voltha-go-controller/onos_nbi/deviceconfig.go
+++ b/voltha-go-controller/onos_nbi/deviceconfig.go
@@ -22,6 +22,7 @@
 	"net/http"
 
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 
 	"github.com/gorilla/mux"
@@ -47,6 +48,8 @@
 		oh.FetchDeviceConfig(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -74,18 +77,24 @@
 	serialNum := vars["serialNumber"]
 	deviceInfo := DeviceConfigPayload{}
 	dc := app.GetApplication().GetDeviceConfig(serialNum)
-	deviceInfo.DeviceConfig = dc
-	oltInfoJSON, err := json.Marshal(deviceInfo)
-	if err != nil {
-		logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"Error": err})
-		w.WriteHeader(http.StatusInternalServerError)
-		return
-	}
+	if dc != nil {
+		deviceInfo.DeviceConfig = dc
+		oltInfoJSON, err := json.Marshal(deviceInfo)
+		if err != nil {
+			logger.Errorw(ctx, "Failed to marshal olt payload response", log.Fields{"Error": err})
+			w.WriteHeader(http.StatusInternalServerError)
+			return
+		}
 
-	w.Header().Add("Content-Type", "application/json")
-	_, err = w.Write(oltInfoJSON)
-	if err != nil {
-		logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"Error": err})
-		w.WriteHeader(http.StatusInternalServerError)
+		w.Header().Add("Content-Type", "application/json")
+		_, err = w.Write(oltInfoJSON)
+		if err != nil {
+			logger.Errorw(ctx, "Failed to write olt payload response", log.Fields{"Error": err})
+			w.WriteHeader(http.StatusInternalServerError)
+		}
+	} else {
+		logger.Warnw(ctx, "Device not found", log.Fields{"serialNum": serialNum})
+		err := errorCodes.ErrDeviceNotFound
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
diff --git a/voltha-go-controller/onos_nbi/deviceportadapter.go b/voltha-go-controller/onos_nbi/deviceportadapter.go
index 72c5433..c27737e 100644
--- a/voltha-go-controller/onos_nbi/deviceportadapter.go
+++ b/voltha-go-controller/onos_nbi/deviceportadapter.go
@@ -20,6 +20,7 @@
 	"net/http"
 
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 
 	"github.com/gorilla/mux"
@@ -41,6 +42,8 @@
 		dh.GetDeviceList(w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -81,6 +84,8 @@
 		dh.GetPortList(w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -92,6 +97,8 @@
 		dh.GetPortListPerDevice(w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
diff --git a/voltha-go-controller/onos_nbi/dhcprelayadapter.go b/voltha-go-controller/onos_nbi/dhcprelayadapter.go
index 2d6391b..7c30351 100644
--- a/voltha-go-controller/onos_nbi/dhcprelayadapter.go
+++ b/voltha-go-controller/onos_nbi/dhcprelayadapter.go
@@ -21,6 +21,7 @@
 	"net/http"
 
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 
 	"github.com/gorilla/mux"
@@ -51,6 +52,8 @@
 		dh.GetAllocations(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
diff --git a/voltha-go-controller/onos_nbi/flowadapter.go b/voltha-go-controller/onos_nbi/flowadapter.go
index fcb1968..1861e05 100644
--- a/voltha-go-controller/onos_nbi/flowadapter.go
+++ b/voltha-go-controller/onos_nbi/flowadapter.go
@@ -22,6 +22,7 @@
 	"strconv"
 
 	cntlr "voltha-go-controller/internal/pkg/controller"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/internal/pkg/of"
 	"voltha-go-controller/log"
 
@@ -62,6 +63,8 @@
 		fh.GetFlows(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -72,6 +75,8 @@
 		pfh.GetPendingFlows(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -79,6 +84,7 @@
 	flows, err := cntlr.GetController().GetAllPendingFlows()
 	if err != nil {
 		logger.Errorw(ctx, "Failed to get Pending flows", log.Fields{"Error": err})
+		w.WriteHeader(http.StatusInternalServerError)
 		return
 	}
 	flowResp := ConvertFlowsToFlowEntry(flows)
@@ -107,6 +113,7 @@
 		flow, err := fh.getFlow(deviceID, flowID)
 		if err != nil {
 			logger.Errorw(ctx, "Failed to Fetch flow", log.Fields{"Error": err})
+			http.Error(w, err.Error(), http.StatusBadRequest)
 			return
 		}
 		flowResp = ConvertFlowToFlowEntry(flow)
@@ -115,6 +122,7 @@
 		flows, err := fh.getAllFlows(deviceID)
 		if err != nil {
 			logger.Errorw(ctx, "Failed to Fetch flows", log.Fields{"Error": err})
+			http.Error(w, err.Error(), http.StatusBadRequest)
 			return
 		}
 		flowResp = ConvertFlowsToFlowEntry(flows)
diff --git a/voltha-go-controller/onos_nbi/maclearning_info.go b/voltha-go-controller/onos_nbi/maclearning_info.go
index e5b8c06..cde6b0b 100644
--- a/voltha-go-controller/onos_nbi/maclearning_info.go
+++ b/voltha-go-controller/onos_nbi/maclearning_info.go
@@ -20,6 +20,7 @@
 	"encoding/json"
 	"net/http"
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 
 	"github.com/gorilla/mux"
@@ -53,6 +54,8 @@
 		}
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
diff --git a/voltha-go-controller/onos_nbi/meters.go b/voltha-go-controller/onos_nbi/meters.go
index 78f1fea..78d715c 100644
--- a/voltha-go-controller/onos_nbi/meters.go
+++ b/voltha-go-controller/onos_nbi/meters.go
@@ -21,6 +21,7 @@
 	"net/http"
 	"strconv"
 	app "voltha-go-controller/internal/pkg/controller"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 
 	"github.com/gorilla/mux"
@@ -55,6 +56,8 @@
 
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -65,6 +68,8 @@
 	mID, err := strconv.ParseUint(meterID, 10, 32)
 	if err != nil {
 		logger.Errorw(ctx, "Failed to parse string to uint32", log.Fields{"Reason": err.Error()})
+		w.WriteHeader(http.StatusInternalServerError)
+		return
 	}
 	id := uint32(mID)
 	logger.Infow(ctx, "Meter Id", log.Fields{"metreId": id})
@@ -72,6 +77,7 @@
 	if err != nil {
 		logger.Errorw(ctx, "Failed to get meter info", log.Fields{"Reason": err.Error()})
 		w.WriteHeader(http.StatusNotFound)
+		return
 	}
 
 	for deviceID, meter := range meterInfo {
diff --git a/voltha-go-controller/onos_nbi/oltapprestadapter.go b/voltha-go-controller/onos_nbi/oltapprestadapter.go
index ff4ad0f..cb926e1 100644
--- a/voltha-go-controller/onos_nbi/oltapprestadapter.go
+++ b/voltha-go-controller/onos_nbi/oltapprestadapter.go
@@ -23,6 +23,7 @@
 	"strconv"
 
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/internal/pkg/of"
 	"voltha-go-controller/log"
 
@@ -91,6 +92,8 @@
 		sa.DeactivateServiceWithPortName(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
@@ -124,6 +127,7 @@
 		portName := device.GetPortNameFromPortID(uint32(port))
 		if len(portName) == 0 {
 			logger.Warnw(ctx, "Port does not exists", log.Fields{"deviceID": deviceID})
+			err := errorCodes.ErrPortNotFound
 			http.Error(w, err.Error(), http.StatusConflict)
 			return
 		}
@@ -164,6 +168,7 @@
 		portName := device.GetPortNameFromPortID(uint32(port))
 		if len(portName) == 0 {
 			logger.Warnw(ctx, "Port does not exists", log.Fields{"deviceID": deviceID})
+			err := errorCodes.ErrPortNotFound
 			http.Error(w, err.Error(), http.StatusConflict)
 			return
 		}
diff --git a/voltha-go-controller/onos_nbi/oltflowservice.go b/voltha-go-controller/onos_nbi/oltflowservice.go
index d4e3454..2da53d3 100644
--- a/voltha-go-controller/onos_nbi/oltflowservice.go
+++ b/voltha-go-controller/onos_nbi/oltflowservice.go
@@ -22,6 +22,7 @@
 	"net/http"
 
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 )
 
@@ -39,6 +40,8 @@
 		oh.fetchOltFlowService(context.Background(), w, r)
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }
 
diff --git a/voltha-go-controller/onos_nbi/portIgnored.go b/voltha-go-controller/onos_nbi/portIgnored.go
index 399be8a..513bb3b 100644
--- a/voltha-go-controller/onos_nbi/portIgnored.go
+++ b/voltha-go-controller/onos_nbi/portIgnored.go
@@ -20,6 +20,7 @@
 	"encoding/json"
 	"net/http"
 	app "voltha-go-controller/internal/pkg/application"
+	errorCodes "voltha-go-controller/internal/pkg/errorcodes"
 	"voltha-go-controller/log"
 )
 
@@ -44,6 +45,8 @@
 
 	default:
 		logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
+		err := errorCodes.ErrOperationNotSupported
+		http.Error(w, err.Error(), http.StatusBadRequest)
 	}
 }