seba-365 tweaking to match mongodb driver refactor

Change-Id: I9be3f9fb9dcc6a1d1879623cc62bd3c99f8b2eae
diff --git a/cmd/AbstractOLT/AbstractOLT.go b/cmd/AbstractOLT/AbstractOLT.go
index e94a218..5da82d6 100644
--- a/cmd/AbstractOLT/AbstractOLT.go
+++ b/cmd/AbstractOLT/AbstractOLT.go
@@ -32,8 +32,9 @@
 	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
 	"gerrit.opencord.org/abstract-olt/models"
 	"github.com/grpc-ecosystem/grpc-gateway/runtime"
-	"github.com/mongodb/mongo-go-driver/bson"
 	"github.com/mongodb/mongo-go-driver/mongo"
+	"github.com/mongodb/mongo-go-driver/mongo/options"
+	"github.com/mongodb/mongo-go-driver/x/bsonx"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/credentials"
@@ -189,6 +190,7 @@
 	h := flag.Bool("h", false, "Show usage")
 	help := flag.Bool("help", false, "Show usage")
 	dummy := flag.Bool("dummy", false, "Run in dummy mode where YAML is not sent to XOS")
+	grpc := flag.Bool("grpc", false, "Use XOS GRPC interface instead of TOSCA")
 
 	useMongo := flag.Bool("useMongo", false, "use mongo db for backup/restore")
 	mongodb := flag.String("mongodb", "mongodb://foundry:foundry@localhost:27017", "connect string for mongodb backup/restore")
@@ -205,6 +207,7 @@
       -log_file [default $WORKING_DIR/AbstractOLT.log] LOG_FILE
       -mongo [default false] use mongodb for backup restore
       -mongodb [default mongodb://foundry:foundry@localhost:27017] connect string for mongodb - Required if mongo == true
+      -grpc [default false] tell AbstractOLT to use XOS GRPC interface instead of TOSCA
       -h(elp) print this usage
 
 `
@@ -214,8 +217,9 @@
 	settings.SetDebug(*debugPtr)
 	settings.SetMongo(*useMongo)
 	settings.SetMongodb(*mongodb)
+	settings.SetGrpc(*grpc)
 	fmt.Println("Startup Params: debug:", *debugPtr, " Authentication:", *useAuthentication, " SSL:", *useSsl, "Cert Directory", *certDirectory,
-		"ListenAddress:", *listenAddress, " grpc port:", *grpcPort, " rest port:", *restPort, "Logging to ", *logFile)
+		"ListenAddress:", *listenAddress, " grpc port:", *grpcPort, " rest port:", *restPort, "Logging to ", *logFile, "Use XOS GRPC ", *grpc)
 
 	file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 	if err != nil {
@@ -257,35 +261,41 @@
 
 	// infinite loop
 	if *useMongo {
-		client, err := mongo.NewClient(*mongodb)
+		clientOptions := options.Client()
+		creds := options.Credential{AuthMechanism: "SCRAM-SHA-256", AuthSource: "AbstractOLT", Username: "seba", Password: "seba"}
+		clientOptions.SetAuth(creds)
+
+		client, err := mongo.NewClientWithOptions(*mongodb, clientOptions)
+
 		client.Connect(context.Background())
+		fmt.Println(client)
 		defer client.Disconnect(context.Background())
 		if err != nil {
 			log.Fatalf("unable to connect to mongodb with %v\n", err)
 		}
-		collection := client.Database("AbstractOLT").Collection("backup")
+		collection := client.Database("AbstractOLT").Collection("backups")
 		cur, err := collection.Find(context.Background(), nil)
 		if err != nil {
 			log.Fatalf("Unable to connect to collection with %v\n", err)
 		}
 		defer cur.Close(context.Background())
 		for cur.Next(context.Background()) {
-			elem := bson.NewDocument()
-			err := cur.Decode(elem)
+			doc := bsonx.Doc{}
+			err := cur.Decode(&doc)
 			if err != nil {
 				log.Fatal(err)
 			}
-			clli := elem.LookupElement("_id").Value()
-			body := elem.LookupElement("body").Value()
-			_, bodyBin := (*body).Binary()
+			clli := doc.LookupElement("_id").Value
+			body := doc.LookupElement("body").Value
+			_, bodyBin := (body).Binary()
 
 			chassisHolder := models.ChassisHolder{}
 			err = chassisHolder.Deserialize(bodyBin)
 			if err != nil {
-				log.Printf("Deserialize threw an error for clli %s %v\n", (*clli).StringValue(), err)
+				log.Printf("Deserialize threw an error for clli %s %v\n", (clli).StringValue(), err)
 			} else {
 				chassisMap := models.GetChassisMap()
-				(*chassisMap)[(*clli).StringValue()] = &chassisHolder
+				(*chassisMap)[(clli).StringValue()] = &chassisHolder
 
 			}
 		}
diff --git a/internal/pkg/impl/ouput.go b/internal/pkg/impl/ouput.go
index 739076b..d6943d0 100644
--- a/internal/pkg/impl/ouput.go
+++ b/internal/pkg/impl/ouput.go
@@ -24,7 +24,7 @@
 	"gerrit.opencord.org/abstract-olt/models"
 	"github.com/mongodb/mongo-go-driver/bson"
 	"github.com/mongodb/mongo-go-driver/mongo"
-	"github.com/mongodb/mongo-go-driver/options"
+	"github.com/mongodb/mongo-go-driver/mongo/options"
 	context "golang.org/x/net/context"
 )
 
@@ -38,7 +38,11 @@
 		defer done(myChan, true)
 		chassisMap := models.GetChassisMap()
 		if settings.GetMongo() {
-			client, err := mongo.NewClient(settings.GetMongodb())
+			clientOptions := options.Client()
+			creds := options.Credential{AuthMechanism: "SCRAM-SHA-256", AuthSource: "AbstractOLT", Username: "seba", Password: "seba"}
+			clientOptions.SetAuth(creds)
+
+			client, err := mongo.NewClientWithOptions(settings.GetMongodb(), clientOptions)
 			client.Connect(context.Background())
 			if err != nil {
 				log.Printf("client connect to mongo db @%s failed with %v\n", settings.GetMongodb(), err)
@@ -46,15 +50,21 @@
 			defer client.Disconnect(context.Background())
 			for clli, chassisHolder := range *chassisMap {
 				json, _ := (chassisHolder).Serialize()
-				collection := client.Database("AbstractOLT").Collection("backup")
-				doc := bson.NewDocument(bson.EC.String("_id", clli))
-				filter := bson.NewDocument(bson.EC.String("_id", clli))
-				doc.Append(bson.EC.Binary("body", json))
-
-				updateDoc := bson.NewDocument(bson.EC.SubDocument("$set", doc))
+				collection := client.Database("AbstractOLT").Collection("backups")
 				//update or insert if not existent
 				upsert := true
-				res, err := collection.UpdateOne(context.Background(), filter, updateDoc, &options.UpdateOptions{Upsert: &upsert})
+				res, err := collection.UpdateOne(context.Background(),
+					bson.D{
+						{"_id", clli},
+					},
+					bson.D{
+						{
+							"$set", bson.D{
+								{"body", json},
+							},
+						},
+					}, &options.UpdateOptions{Upsert: &upsert})
+
 				if err != nil {
 					log.Printf("collection.UpdateOne failed with %v\n", err)
 				} else {
diff --git a/internal/pkg/settings/Settings.go b/internal/pkg/settings/Settings.go
index b4eb128..6df5eaa 100644
--- a/internal/pkg/settings/Settings.go
+++ b/internal/pkg/settings/Settings.go
@@ -19,6 +19,7 @@
 var debug = false
 var dummy = false
 var mongo = false
+var grpc = true
 var mongodb = ""
 
 /*
@@ -48,15 +49,44 @@
 func GetDummy() bool {
 	return dummy
 }
+
+/*
+SetMongo - sets useMongo mode
+*/
 func SetMongo(useMongo bool) {
 	mongo = useMongo
 }
+
+/*GetMongo - returns the value of mongo
+ */
 func GetMongo() bool {
 	return mongo
 }
+
+/*
+SetMongodb - sets the connection string for mongo db used for backups
+*/
 func SetMongodb(connectString string) {
 	mongodb = connectString
 }
+
+/*
+GetMongodb - returns the connection string used for connecting to mongo db
+*/
 func GetMongodb() string {
 	return mongodb
 }
+
+/*
+SetGrpc - sets useGrpc mode
+*/
+func SetGrpc(useGrpc bool) {
+	grpc = useGrpc
+}
+
+/*
+GetGrpc - returns the value of grpc
+*/
+func GetGrpc() bool {
+	return grpc
+}