[AETHER-748] Apps update does not work properly

- apps-update target has been renamed in apps
- update targets have been merged together with the app-name target. Updates are
performed with respect of the versions defined in Makefile.vars.
- remote update is used to sync up with the remote avoiding pull which can return -1
if we are not on a branch and updates only the branch we are on.
- checkout origin/{version} is used to avoid stale branches.
- checkout {version} is used to switch on local branches or specific commit

Additionally, updates ONOS to the latest published artifacts

Change-Id: I9caebb76219b2e8da05ca6f0858ee73478a92533
diff --git a/Makefile b/Makefile
index cac1126..b84e837 100644
--- a/Makefile
+++ b/Makefile
@@ -129,8 +129,11 @@
 		exit 1; \
 	fi
 
+	# Updates the repo and avoids any stale branches
+	cd ${TRELLIS_CONTROL_ROOT} && git remote update
+
 	# Try the git checkout first otherwise we download the review
-	if ! (cd ${TRELLIS_CONTROL_ROOT} && git checkout ${TRELLIS_CONTROL_VERSION}); then \
+	if ! (cd ${TRELLIS_CONTROL_ROOT} && (git checkout origin/${TRELLIS_CONTROL_VERSION} || git checkout ${TRELLIS_CONTROL_VERSION})); then \
 	if ! (cd ${TRELLIS_CONTROL_ROOT} && git fetch ${TRELLIS_CONTROL_REPO} ${TRELLIS_CONTROL_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the trellis-control repository"; \
 	fi \
@@ -139,15 +142,6 @@
 trellis-control-build: mvn_settings.xml local-apps trellis-control  ## : Builds trellis-control using local app or mvn
 	@./app-build.sh $@
 
-trellis-control-update: ## : downloads commits, files, and refs from remote trellis-control
-	cd ${TRELLIS_CONTROL_ROOT} && git fetch
-
-	# Try to pull - but fails if we have not checked a branch
-	if ! (cd ${TRELLIS_CONTROL_ROOT} && git pull); then \
-		echo "Unable to pull from the trellis-control repository"; \
-		exit 1; \
-	fi
-
 trellis-t3: ## : Checkout trellis-t3 code
 	if [ ! -d "trellis-t3" ]; then \
 		git clone ${TRELLIS_T3_REPO}; \
@@ -159,7 +153,9 @@
 		exit 1; \
 	fi
 
-	if ! (cd ${TRELLIS_T3_ROOT} && git checkout ${TRELLIS_T3_VERSION}); then \
+	cd ${TRELLIS_T3_ROOT} && git remote update
+
+	if ! (cd ${TRELLIS_T3_ROOT} && (git checkout origin/${TRELLIS_T3_VERSION} || git checkout ${TRELLIS_T3_VERSION})); then \
 	if ! (cd ${TRELLIS_T3_ROOT} && git fetch ${TRELLIS_T3_REPO} ${TRELLIS_T3_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the trellis-t3 repository"; \
 	fi \
@@ -168,14 +164,6 @@
 trellis-t3-build: mvn_settings.xml local-apps trellis-t3  ## : Builds trellis-t3 using local app or mvn
 	@./app-build.sh $@
 
-trellis-t3-update: ## : downloads commits, files, and refs from remote trellis-t3
-	cd ${TRELLIS_T3_ROOT} && git fetch
-
-	if ! (cd ${TRELLIS_T3_ROOT} && git pull); then \
-		echo "Unable to pull from the trellis-t3 repository"; \
-		exit 1; \
-	fi
-
 fabric-tofino: ## : Checkout fabric-tofino code
 	if [ ! -d "fabric-tofino" ]; then \
 		git clone ${FABRIC_TOFINO_REPO}; \
@@ -187,7 +175,9 @@
 		exit 1; \
 	fi
 
-	if ! (cd ${FABRIC_TOFINO_ROOT} && git checkout ${FABRIC_TOFINO_VERSION}); then \
+	cd ${FABRIC_TOFINO_ROOT} && git remote update
+
+	if ! (cd ${FABRIC_TOFINO_ROOT} && (git checkout origin/${FABRIC_TOFINO_VERSION} || git checkout ${FABRIC_TOFINO_VERSION})); then \
 	if ! (cd ${FABRIC_TOFINO_ROOT} && git fetch ${FABRIC_TOFINO_REPO} ${FABRIC_TOFINO_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the fabric-tofino repository"; \
 		exit 1; \
@@ -197,14 +187,6 @@
 fabric-tofino-build: mvn_settings.xml local-apps fabric-tofino  ## : Builds fabric-tofino using local app or mvn
 	@./app-build.sh $@
 
-fabric-tofino-update: ## : downloads commits, files, and refs from remote fabric-tofino
-	cd ${FABRIC_TOFINO_ROOT} && git fetch
-
-	if ! (cd ${FABRIC_TOFINO_ROOT} && git pull); then \
-		echo "Unable to pull from the fabric-tofino repository"; \
-		exit 1; \
-	fi
-
 up4: ## : Checkout up4 code
 	if [ ! -d "up4" ]; then \
 		git clone ${UP4_REPO}; \
@@ -216,7 +198,9 @@
 		exit 1; \
 	fi
 
-	if ! (cd ${UP4_ROOT} && git checkout ${UP4_VERSION}); then \
+	cd ${UP4_ROOT} && git remote update
+
+	if ! (cd ${UP4_ROOT} && (git checkout origin/${UP4_VERSION} || git checkout ${UP4_VERSION})); then \
 	if ! (cd ${UP4_ROOT} && git fetch ${UP4_REPO} ${UP4_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the up4 repository"; \
 		exit 1; \
@@ -226,14 +210,6 @@
 up4-build: mvn_settings.xml local-apps up4  ## : Builds up4 using local app
 	@./app-build.sh $@
 
-up4-update: ## : downloads commits, files, and refs from remote up4
-	cd ${UP4_ROOT} && git fetch
-
-	if ! (cd ${UP4_ROOT} && git pull); then \
-		echo "Unable to pull from the up4 repository"; \
-		exit 1; \
-	fi
-
 kafka-onos: ## : Checkout kafka-onos code
 	if [ ! -d "kafka-onos" ]; then \
 		git clone ${KAFKA_ONOS_REPO}; \
@@ -245,7 +221,9 @@
 		exit 1; \
 	fi
 
-	if ! (cd ${KAFKA_ONOS_ROOT} && git checkout ${KAFKA_ONOS_VERSION}); then \
+	cd ${KAFKA_ONOS_ROOT} && git remote update
+
+	if ! (cd ${KAFKA_ONOS_ROOT} && (git checkout origin/${KAFKA_ONOS_VERSION} || git checkout ${KAFKA_ONOS_VERSION})); then \
 	if ! (cd ${KAFKA_ONOS_ROOT} && git fetch ${KAFKA_ONOS_REPO} ${KAFKA_ONOS_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the kafka-onos repository"; \
 	fi \
@@ -254,14 +232,6 @@
 kafka-onos-build: mvn_settings.xml local-apps kafka-onos  ## : Builds kafka-onos using local app or mvn
 	@./app-build.sh $@
 
-kafka-onos-update: ## : downloads commits, files, and refs from remote kafka-onos
-	cd ${KAFKA_ONOS_ROOT} && git fetch
-
-	if ! (cd ${KAFKA_ONOS_ROOT} && git pull); then \
-		echo "Unable to pull from the kafka-onos repository"; \
-		exit 1; \
-	fi
-
 fabric-tna: ## : Checkout fabric-tna code
 	if [ ! -d "fabric-tna" ]; then \
 		git clone ${FABRIC_TNA_REPO}; \
@@ -273,7 +243,9 @@
 		exit 1; \
 	fi
 
-	if ! (cd ${FABRIC_TNA_ROOT} && git checkout ${FABRIC_TNA_VERSION}); then \
+	cd ${FABRIC_TNA_ROOT} && git remote update
+
+	if ! (cd ${FABRIC_TNA_ROOT} && (git checkout origin/${FABRIC_TNA_VERSION} || git checkout ${FABRIC_TNA_VERSION})); then \
 	if ! (cd ${FABRIC_TNA_ROOT} && git fetch ${FABRIC_TNA_REPO} ${FABRIC_TNA_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the fabric-tna repository"; \
 		exit 1; \
@@ -283,15 +255,7 @@
 fabric-tna-build: mvn_settings.xml local-apps fabric-tna  ## : Builds fabric-tna using local app
 	@./app-build.sh $@
 
-fabric-tna-update: ## : downloads commits, files, and refs from remote fabric-tna
-	cd ${FABRIC_TNA_ROOT} && git fetch
-
-	if ! (cd ${FABRIC_TNA_ROOT} && git pull); then \
-		echo "Unable to pull from the fabric-tna repository"; \
-		exit 1; \
-	fi
-
-apps-update: trellis-control-update trellis-t3-update fabric-tofino-update up4-update kafka-onos-update fabric-tna-update ## : downloads commits, files, and refs from remotes
+apps: trellis-control trellis-t3 fabric-tofino up4 kafka-onos fabric-tna ## : downloads commits, files, and refs from remotes
 
 apps-build: trellis-control-build trellis-t3-build fabric-tofino-build up4-build kafka-onos-build fabric-tna-build ## : Build the onos apps
 
@@ -306,22 +270,16 @@
 		exit 1; \
 	fi
 
+	cd ${ONOS_ROOT} && git remote update
+
 	# In case of failure, we do not proceed because we cannot build with mvn
-	if ! (cd ${ONOS_ROOT} && git checkout ${ONOS_VERSION}); then \
+	if ! (cd ${ONOS_ROOT} && (git checkout origin/${ONOS_VERSION} || git checkout ${ONOS_VERSION})); then \
 	if ! (cd ${ONOS_ROOT} && git fetch ${ONOS_REPO} ${ONOS_VERSION} && git checkout FETCH_HEAD); then \
 		echo "Unable to fetch the changes from the onos repository"; \
 		exit 1; \
 	fi \
 	fi
 
-onos-update: ## : downloads commits, files, and refs from remote onos
-	cd ${ONOS_ROOT} && git fetch
-
-	if ! (cd ${ONOS_ROOT} && git pull); then \
-		echo "Unable to pull from the onos repository"; \
-		exit 1; \
-	fi
-
 onos-build: onos ## : Builds the tost-onos docker image
 	# Set some env variables
 	cd ${ONOS_ROOT} && \
diff --git a/Makefile.vars b/Makefile.vars
index 4fd5451..debc49c 100644
--- a/Makefile.vars
+++ b/Makefile.vars
@@ -26,7 +26,7 @@
 # (2) Checkout Local source code (local branch not yet pushed);
 # (3) Fetch from Gerrit/Github (pending review in the form of refs/changes/... or pending pull request);
 
-export ONOS_VERSION            ?= 930b0cdde44c268052cd4690652405fdc95ef924 # onos-2.2
+export ONOS_VERSION            ?= 2.2.7-b2 # onos-2.2
 export TRELLIS_CONTROL_VERSION ?= 3.0.0
 export TRELLIS_T3_VERSION      ?= 3.0.0
 export FABRIC_TOFINO_VERSION   ?= e1cf4551b4ab74bb82a7cecf19d197698ffbc1cb
diff --git a/README.md b/README.md
index a6f8d36..67decdb 100644
--- a/README.md
+++ b/README.md
@@ -60,16 +60,12 @@
 
 ## Update
 
-Use `update` targets to downloads commits, files, and refs from remotes.
+Use `apps` or `appname` targets to downloads commits, files, and refs from remotes.
 
-```sh
-# Update up4 app.
-make up4-update
-```
 
 ```sh
 # Update one by one all the apps.
-make apps-update
+make apps
 ```
 
 ## Clean
diff --git a/app-build.sh b/app-build.sh
index 473eb29..917d5ad 100755
--- a/app-build.sh
+++ b/app-build.sh
@@ -120,7 +120,7 @@
 	if [ ! -f "$7" ]; then
 		cd "$2" || exit 1
 		# Verify for the last time if the VERSION is a checkout object or a review
-		if ! (git checkout "$5"); then
+		if ! (git checkout "origin/$5" || git checkout "$5"); then
 			if ! (git fetch "$8" "$5" && git checkout FETCH_HEAD); then
 				exit 1
 			fi