VOL-1437 : Fix display of devices in CLI

Renamed state field in image download proto which caused
json marshalling to fail.

other updates
- unmarshal data properly when pulling data from kv in watch
- do not update unecessarily if watch data matches data in memory
- added ofagent target to python Makefile
- fixed grpc path to update log level which interfered with getdevice

Change-Id: I4fceeb539c3325b6754efe2b68251d83b7367211
diff --git a/db/model/non_persisted_revision.go b/db/model/non_persisted_revision.go
index 765bbaf..27d3d9a 100644
--- a/db/model/non_persisted_revision.go
+++ b/db/model/non_persisted_revision.go
@@ -250,9 +250,9 @@
 	npr.mutex.Lock()
 	defer npr.mutex.Unlock()
 
-	if npr.Config.Data != nil && npr.Config.Data == data {
+	if npr.Config.Data != nil && npr.Config.hashData(npr.Root, data) == npr.Config.Hash {
 		log.Debugw("stored-data-matches-latest", log.Fields{"stored": npr.Config.Data, "provided": data})
-		return nil
+		return npr
 	}
 
 	newRev := NonPersistedRevision{}
@@ -285,10 +285,13 @@
 			existChildMap[child.GetHash()] = i
 		}
 
-		// Identify the revisions that are not present in the existing list and add them
 		for _, newChild := range children {
 			if _, childExists := existChildMap[newChild.GetHash()]; !childExists {
+				// revision is not present in the existing list... add it
 				updatedRev.Children[name] = append(updatedRev.Children[name], newChild)
+			} else {
+				// replace
+				updatedRev.Children[name][existChildMap[newChild.GetHash()]] = newChild
 			}
 		}
 	} else {
diff --git a/db/model/persisted_revision.go b/db/model/persisted_revision.go
index 84b9569..98e80e4 100644
--- a/db/model/persisted_revision.go
+++ b/db/model/persisted_revision.go
@@ -131,7 +131,13 @@
 				if dataPair, err := pr.kvStore.Get(pr.GetHash()); err != nil || dataPair == nil {
 					log.Errorw("update-in-memory--key-retrieval-failed", log.Fields{"key": pr.GetHash(), "error": err})
 				} else {
-					pr.UpdateData(dataPair.Value, pr.GetBranch())
+					data := reflect.New(reflect.TypeOf(pr.GetData()).Elem())
+
+					if err := proto.Unmarshal(dataPair.Value.([]byte), data.Interface().(proto.Message)); err != nil {
+						log.Errorw("update-in-memory--unmarshal-failed", log.Fields{"key": pr.GetHash(), "error": err})
+					} else {
+						pr.UpdateData(data.Interface(), pr.GetBranch())
+					}
 				}
 
 			default:
diff --git a/protos/device.proto b/protos/device.proto
index 902a58c..0770060 100644
--- a/protos/device.proto
+++ b/protos/device.proto
@@ -144,7 +144,7 @@
     uint32 crc = 4;
 
     // Download state
-    ImageDownloadState state = 5;
+    ImageDownloadState download_state = 5;
 
     // Downloaded version
     string image_version = 6;
diff --git a/protos/voltha.proto b/protos/voltha.proto
index faf57a1..fdff7f6 100644
--- a/protos/voltha.proto
+++ b/protos/voltha.proto
@@ -146,7 +146,7 @@
     // Get more information on a given physical device
     rpc UpdateLogLevel(Logging) returns(google.protobuf.Empty) {
         option (google.api.http) = {
-            get: "/api/v1/devices/{id}"
+            get: "/api/v1/logs"
         };
     }
 
diff --git a/python/Makefile b/python/Makefile
index d6ae216..737ae89 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -89,7 +89,7 @@
 
 FETCH_IMAGE_LIST = $(shell echo $(FETCH_BUILD_IMAGE_LIST) $(FETCH_COMPOSE_IMAGE_LIST) $(FETCH_K8S_IMAGE_LIST) | tr ' ' '\n' | sort -u)
 
-.PHONY: $(DIRS) $(DIRS_CLEAN) $(DIRS_FLAKE8) flake8 base openolt ponsim_olt ponsim_onu protos cli kafka common start stop tag push pull
+.PHONY: $(DIRS) $(DIRS_CLEAN) $(DIRS_FLAKE8) flake8 base openolt ponsim_olt ponsim_onu protos cli ofagent kafka common start stop tag push pull
 
 # This should to be the first and default target in this Makefile
 help:
@@ -111,6 +111,7 @@
 	@echo "adapter_ponsim_olt       : Build the ponsim olt adapter docker container"
 	@echo "adapter_ponsim_onu       : Build the ponsim olt adapter docker container"
 	@echo "adapter_openolt       : Build the openolt adapter docker container"
+	@echo "ofagent      : Build the openolt adapter docker container"
 	@echo "tag          : Tag a set of images"
 	@echo "push         : Push the docker images to an external repository"
 	@echo "pull         : Pull the docker images from a repository"
@@ -145,7 +146,7 @@
 
 build: protoc protos containers
 
-containers: base adapter_ponsim_olt adapter_ponsim_onu adapter_openolt
+containers: base adapter_ponsim_olt adapter_ponsim_onu adapter_openolt ofagent
 
 base:
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-base:${TAG} -f docker/Dockerfile.base .
@@ -155,9 +156,13 @@
 
 adapter_openolt:
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-adapter-openolt:${TAG} -f docker/Dockerfile.adapter_openolt .
+
 adapter_ponsim_onu:
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-adapter-ponsim-onu:${TAG} -f docker/Dockerfile.adapter_ponsim_onu .
 
+ofagent:
+	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-ofagent:${TAG} -f docker/Dockerfile.ofagent .
+
 cli:
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-cli:${TAG} -f docker/Dockerfile.cli .
 
diff --git a/rw_core/core/device_agent.go b/rw_core/core/device_agent.go
index b50ca94..31720c1 100644
--- a/rw_core/core/device_agent.go
+++ b/rw_core/core/device_agent.go
@@ -316,7 +316,7 @@
 		}
 		// Save the image
 		clonedImg := proto.Clone(img).(*voltha.ImageDownload)
-		clonedImg.State = voltha.ImageDownload_DOWNLOAD_REQUESTED
+		clonedImg.DownloadState = voltha.ImageDownload_DOWNLOAD_REQUESTED
 		cloned := proto.Clone(device).(*voltha.Device)
 		if cloned.ImageDownloads == nil {
 			cloned.ImageDownloads = []*voltha.ImageDownload{clonedImg}
@@ -363,7 +363,7 @@
 		cloned := proto.Clone(device).(*voltha.Device)
 		for _, image := range cloned.ImageDownloads {
 			if image.Id == img.Id && image.Name == img.Name {
-				image.State = voltha.ImageDownload_DOWNLOAD_CANCELLED
+				image.DownloadState = voltha.ImageDownload_DOWNLOAD_CANCELLED
 			}
 		}
 
@@ -488,15 +488,15 @@
 		clonedImages := make([]*voltha.ImageDownload, len(cloned.ImageDownloads))
 		for _, image := range cloned.ImageDownloads {
 			if image.Id == img.Id && image.Name == img.Name {
-				if image.State != voltha.ImageDownload_DOWNLOAD_CANCELLED {
+				if image.DownloadState != voltha.ImageDownload_DOWNLOAD_CANCELLED {
 					clonedImages = append(clonedImages, img)
 				}
 			}
 		}
 		cloned.ImageDownloads = clonedImages
 		// Set the Admin state to enabled if required
-		if (img.State != voltha.ImageDownload_DOWNLOAD_REQUESTED &&
-			img.State != voltha.ImageDownload_DOWNLOAD_STARTED) ||
+		if (img.DownloadState != voltha.ImageDownload_DOWNLOAD_REQUESTED &&
+			img.DownloadState != voltha.ImageDownload_DOWNLOAD_STARTED) ||
 			(img.ImageState != voltha.ImageDownload_IMAGE_ACTIVATING){
 			cloned.AdminState = voltha.AdminState_ENABLED
 		}
diff --git a/rw_core/core/grpc_nbi_api_handler.go b/rw_core/core/grpc_nbi_api_handler.go
index 4d88459..c65178d 100644
--- a/rw_core/core/grpc_nbi_api_handler.go
+++ b/rw_core/core/grpc_nbi_api_handler.go
@@ -521,11 +521,11 @@
 func (handler *APIHandler) GetImageDownloadStatus(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
 	log.Debugw("getImageDownloadStatus-request", log.Fields{"img": *img})
 	if isTestMode(ctx) {
-		resp := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
+		resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
 		return resp, nil
 	}
 
-	failedresponse := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
+	failedresponse := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}
 
 	if handler.competeForTransaction() {
 		if txn, err := handler.acquireTransaction(ctx); err != nil {
@@ -560,12 +560,12 @@
 func (handler *APIHandler) GetImageDownload(ctx context.Context, img *voltha.ImageDownload) (*voltha.ImageDownload, error) {
 	log.Debugw("GetImageDownload-request", log.Fields{"img": *img})
 	if isTestMode(ctx) {
-		resp := &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
+		resp := &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_SUCCEEDED}
 		return resp, nil
 	}
 
 	if download, err := handler.deviceMgr.getImageDownload(ctx, img); err != nil {
-		return &voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
+		return &voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN}, err
 	} else {
 		return download, nil
 	}
@@ -581,7 +581,7 @@
 	if downloads, err := handler.deviceMgr.listImageDownloads(ctx, id.Id); err != nil {
 		failedResp := &voltha.ImageDownloads{
 			Items:[]*voltha.ImageDownload{
-				&voltha.ImageDownload{State: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
+				&voltha.ImageDownload{DownloadState: voltha.ImageDownload_DOWNLOAD_UNKNOWN},
 		},
 		}
 		return failedResp, err