Moving BBSim entrypoint in the cmd folder, building bbsimctl in the container

Change-Id: I4823578ed17c40b13c1c9a561e7aa7e6640f7f19
diff --git a/.dockerignore b/.dockerignore
index a00ce81..7beaa60 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,3 +1,2 @@
 build/*
-cmd/*
 deployments/*
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 8b13789..dbc2a92 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,4 @@
+vendor
 
+/bbsim
+/bbsimctl
diff --git a/Makefile b/Makefile
index b2595bd..50f281f 100644
--- a/Makefile
+++ b/Makefile
@@ -39,7 +39,7 @@
 	gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml
 
 docker-build: # @HELP Build a docker container
-	docker build --build-arg GIT_STATUS=${GIT_STATUS} -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} -f build/package/Dockerfile .
+	docker build -t ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG} -f build/package/Dockerfile .
 
 docker-push: # @HELP Push a docker container to a registry
 	docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}bbsim:${DOCKER_TAG}
@@ -69,15 +69,15 @@
     		-X main.commitHash=$(shell git log --pretty=format:%H -n 1) \
     		-X main.gitStatus=${GIT_STATUS} \
     		-X main.version=${VERSION}" \
-    	-o ./cmd/bbsim ./internal/bbsim
+    	./cmd/bbsim
 
 build-bbsimctl:
 	GO111MODULE=on go build -i -v -mod vendor \
-        	-ldflags "-X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +”%Y/%m/%d-%H:%M:%S”) \
-        		-X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
-        		-X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
-        		-X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
-        	./cmd/bbsimctl
+		-ldflags "-X github.com/opencord/bbsim/internal/bbsimctl/config.BuildTime=$(shell date +”%Y/%m/%d-%H:%M:%S”) \
+			-X github.com/opencord/bbsim/internal/bbsimctl/config.CommitHash=$(shell git log --pretty=format:%H -n 1) \
+			-X github.com/opencord/bbsim/internal/bbsimctl/config.GitStatus=${GIT_STATUS} \
+			-X github.com/opencord/bbsim/internal/bbsimctl/config.Version=${VERSION}" \
+		./cmd/bbsimctl
 
 api/openolt/openolt.pb.go: api/openolt/openolt.proto
 	@protoc -I . \
diff --git a/README.md b/README.md
index 6a5dcbb..2cfd0fa 100644
--- a/README.md
+++ b/README.md
@@ -62,7 +62,8 @@
 ## Control API
 
 BBSim comes with a gRPC interface to control the internal state.
-This interface can be queried using `bbsimctl` (the tool can be build with `make build`):
+This interface can be queried using `bbsimctl` (the tool can be build with `make build`
+and it's available inside the `bbsim` container):
 
 ```bash
 $ ./bbsimctl --help
diff --git a/build/package/Dockerfile b/build/package/Dockerfile
index 2b1484e..46af0e3 100644
--- a/build/package/Dockerfile
+++ b/build/package/Dockerfile
@@ -47,13 +47,7 @@
 # copy and build
 COPY . ./
 RUN go mod vendor -v  # we can't vendor dependencies unless the code is there
-ARG GIT_STATUS=Clean
-RUN GO111MODULE=on go build -mod vendor\
-    -ldflags "-X main.buildTime=$(date +”%Y/%m/%d-%H:%M:%S”) \
-    		-X main.commitHash=$(git log --pretty=format:%H -n 1) \
-    		-X main.gitStatus=${GIT_STATUS}\
-    		-X main.version=$(cat VERSION)" \
-    -o ./cmd/bbsim ./internal/bbsim
+RUN make build
 
 # runtime parent
 FROM golang:1.12-stretch
@@ -73,6 +67,8 @@
 && touch /var/lib/dhcp/dhcpd.leases
 
 WORKDIR /app
-COPY --from=builder /go/src/github.com/opencord/bbsim/cmd/bbsim /app/bbsim
+COPY --from=builder /go/src/github.com/opencord/bbsim/bbsim /app/bbsim
+COPY --from=builder /go/src/github.com/opencord/bbsim/bbsimctl /usr/bin/bbsimctl
 RUN chmod a+x /app/bbsim
+RUN chmod a+x /usr/bin/bbsimctl
 CMD [ '/app/bbsim' ]
\ No newline at end of file
diff --git a/cmd/.gitignore b/cmd/.gitignore
deleted file mode 100644
index c96a04f..0000000
--- a/cmd/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
\ No newline at end of file
diff --git a/internal/bbsim/bbsim.go b/cmd/bbsim/bbsim.go
similarity index 93%
rename from internal/bbsim/bbsim.go
rename to cmd/bbsim/bbsim.go
index 7c47e91..7d8ea06 100644
--- a/internal/bbsim/bbsim.go
+++ b/cmd/bbsim/bbsim.go
@@ -19,6 +19,7 @@
 import (
 	"flag"
 	"github.com/opencord/bbsim/api/bbsim"
+	"github.com/opencord/bbsim/internal/bbsim/api"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
 	log "github.com/sirupsen/logrus"
 	"google.golang.org/grpc"
@@ -29,6 +30,16 @@
 	"sync"
 )
 
+type CliOptions struct {
+	OltID        int
+	NumNniPerOlt int
+	NumPonPerOlt int
+	NumOnuPerPon int
+	STag         int
+	CTagInit     int
+	profileCpu   *string
+}
+
 func getOpts() *CliOptions {
 
 	olt_id := flag.Int("olt_id", 0, "Number of OLT devices to be emulated (default is 1)")
@@ -63,7 +74,7 @@
 		log.Fatalf("APIServer failed to listen: %v", err)
 	}
 	grpcServer := grpc.NewServer()
-	bbsim.RegisterBBSimServer(grpcServer, BBSimServer{})
+	bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
 
 	reflection.Register(grpcServer)
 
diff --git a/cmd/bbsimctl/bbsimctl.go b/cmd/bbsimctl/bbsimctl.go
new file mode 100644
index 0000000..29b45c5
--- /dev/null
+++ b/cmd/bbsimctl/bbsimctl.go
@@ -0,0 +1,63 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package main
+
+import (
+	"fmt"
+	"github.com/jessevdk/go-flags"
+	"github.com/opencord/bbsim/internal/bbsimctl/commands"
+	"github.com/opencord/bbsim/internal/bbsimctl/config"
+	"os"
+	"path"
+)
+
+func main() {
+
+	parser := flags.NewNamedParser(path.Base(os.Args[0]),
+		flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
+	_, err := parser.AddGroup("Global Options", "", &config.GlobalOptions)
+	if err != nil {
+		panic(err)
+	}
+
+	commands.RegisterConfigCommands(parser)
+	commands.RegisterOltCommands(parser)
+	commands.RegisterONUCommands(parser)
+
+	_, err = parser.ParseArgs(os.Args[1:])
+	if err != nil {
+		_, ok := err.(*flags.Error)
+		if ok {
+			real := err.(*flags.Error)
+			if real.Type == flags.ErrHelp {
+				os.Stdout.WriteString(err.Error() + "\n")
+				return
+			}
+		}
+
+		//corderror, ok := err.(corderrors.CordCtlError)
+		//if ok {
+		//	if corderror.ShouldDumpStack() || config.GlobalOptions.Debug {
+		//		os.Stderr.WriteString("\n" + corderror.Stack())
+		//	}
+		//}
+
+		fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err.Error())
+
+		os.Exit(1)
+	}
+}
diff --git a/internal/bbsim/grpc_api_server.go b/internal/bbsim/api/grpc_api_server.go
similarity index 99%
rename from internal/bbsim/grpc_api_server.go
rename to internal/bbsim/api/grpc_api_server.go
index 20b6164..27b0ce5 100644
--- a/internal/bbsim/grpc_api_server.go
+++ b/internal/bbsim/api/grpc_api_server.go
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package main
+package api
 
 import (
 	"context"
diff --git a/internal/bbsim/types.go b/internal/bbsim/types.go
deleted file mode 100644
index 76c23cd..0000000
--- a/internal/bbsim/types.go
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright 2018-present Open Networking Foundation
-
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
-
- * http://www.apache.org/licenses/LICENSE-2.0
-
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package main
-
-// General
-
-type CliOptions struct {
-	OltID        int
-	NumNniPerOlt int
-	NumPonPerOlt int
-	NumOnuPerPon int
-	STag         int
-	CTagInit     int
-	profileCpu   *string
-}
diff --git a/vendor/.gitignore b/vendor/.gitignore
deleted file mode 100644
index c96a04f..0000000
--- a/vendor/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
\ No newline at end of file