VOL-4273 Increase uni-id range to 256 UNIs for Logical UNI Port number creation

Change-Id: I8a680550f23f4f442db9aee01c32192526943ba1
diff --git a/VERSION b/VERSION
index a8907c0..a50da18 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-7.0.2
+7.0.3
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index db0d551..688ccd8 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -32,19 +32,20 @@
 
     OpenFlow port number corresponding to PON UNI
 
-     20        12              4      0
-    +--+--------+--------------+------+
-    |0 | pon id |    onu id    |uni id|
-    +--+--------+--------------+------+
+     24        16          8          0
+    +--+--------+----------+----------+
+    |0 | pon id |  onu id  |  uni id  |
+    +--+--------+----------+----------+
 
     pon id = 8 bits = 256 PON ports
     onu id = 8 bits = 256 ONUs per PON port
+    uni id = 8 bits = 256 UNIs per ONU
 
 Logical (OF) NNI port number
 
     OpenFlow port number corresponding to PON NNI
 
-     20                             0
+     24                             0
     +--+----------------------------+
     |1 |                    intf_id |
     +--+----------------------------+
@@ -64,7 +65,7 @@
 
 const (
 	// Number of bits for the physical UNI of the ONUs
-	bitsForUniID = 4
+	bitsForUniID = 8
 	// Number of bits for the ONU ID
 	bitsForONUID = 8
 	// Number of bits for PON ID
@@ -81,26 +82,24 @@
 	ponIntfMarkerPos = 28
 	// Value of marker used to distinguish PON port type of OF port
 	ponIntfMarkerValue = 0x2
-	// Number of bits for NNI ID
-	bitsforNNIID = 20
-	// minNniIntPortNum is used to store start range of nni port number (1 << 20) 1048576
-	minNniIntPortNum = (1 << bitsforNNIID)
-	// maxNniPortNum is used to store the maximum range of nni port number ((1 << 21)-1) 2097151
-	maxNniPortNum = ((1 << (bitsforNNIID + 1)) - 1)
-	// minPonIntfPortNum stores the minimum pon port number
+	// minNniPortNum is used to store start range of nni port number (1 << 24) 16777216
+	minNniPortNum = (1 << nniUniDiffPos)
+	// maxNniPortNum is used to store the maximum range of nni port number ((1 << 25)-1) 33554431
+	maxNniPortNum = ((1 << (nniUniDiffPos + 1)) - 1)
+	// minPonIntfPortNum stores the minimum pon port number (536870912)
 	minPonIntfPortNum = ponIntfMarkerValue << ponIntfMarkerPos
-	// maxPonIntfPortNum stores the maximum pon port number
-	maxPonIntfPortNum = (ponIntfMarkerValue << ponIntfMarkerPos) | (1 << bitsForPONID)
+	// maxPonIntfPortNum stores the maximum pon port number (536871167)
+	maxPonIntfPortNum = ((ponIntfMarkerValue << ponIntfMarkerPos) | (1 << bitsForPONID)) - 1
 	upstream          = "upstream"
 	downstream        = "downstream"
+	//Technology Profiles ID start value
+	TpIDStart = 64
+	//Technology Profiles ID end value
+	TpIDEnd = 256
+	//Number of Technology Profiles can be defined.
+	TpRange = TpIDEnd - TpIDStart
 )
 
-//MinUpstreamPortID value
-var MinUpstreamPortID = 0xfffd
-
-//MaxUpstreamPortID value
-var MaxUpstreamPortID = 0xfffffffd
-
 var controllerPorts = []uint32{0xfffd, 0x7ffffffd, 0xfffffffd}
 
 //MkUniPortNum returns new UNIportNum based on intfID, inuID and uniID
@@ -159,11 +158,11 @@
 
 //IntfIDFromNniPortNum returns Intf ID derived from portNum
 func IntfIDFromNniPortNum(ctx context.Context, portNum uint32) (uint32, error) {
-	if portNum < minNniIntPortNum || portNum > maxNniPortNum {
+	if portNum < minNniPortNum || portNum > maxNniPortNum {
 		logger.Errorw(ctx, "nniportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum})
 		return uint32(0), status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", portNum)
 	}
-	return (portNum & 0xFFFF), nil
+	return (portNum & (minNniPortNum - 1)), nil
 }
 
 //IntfIDFromPonPortNum returns Intf ID derived from portNum
@@ -172,7 +171,7 @@
 		logger.Errorw(ctx, "ponportnumber-is-not-in-valid-range", log.Fields{"portnum": portNum})
 		return uint32(0), status.Errorf(codes.InvalidArgument, "invalid-pon-port-number:%d", portNum)
 	}
-	return (portNum & 0x7FFF), nil
+	return (portNum & ((1 << ponIntfMarkerPos) - 1)), nil
 }
 
 //IntfIDToPortTypeName returns port type derived from the intfId
diff --git a/pkg/platform/platform_test.go b/pkg/platform/platform_test.go
index 676ab0c..71c864a 100644
--- a/pkg/platform/platform_test.go
+++ b/pkg/platform/platform_test.go
@@ -41,10 +41,10 @@
 		want uint32
 	}{
 		// TODO: Add test cases.
-		{"MkUniPortNum-1", args{1, 1, 1}, ((1 * 4096) + (1 * 16) + 1)},
-		{"MkUniPortNum-2", args{4, 5, 6}, ((4 * 4096) + (5 * 16) + 6)},
+		{"MkUniPortNum-1", args{1, 1, 1}, ((1 * 65536) + (1 * 256) + 1)},
+		{"MkUniPortNum-2", args{4, 5, 6}, ((4 * 65536) + (5 * 256) + 6)},
 		// Negative test cases to cover the log.warn
-		{"MkUniPortNum-3", args{4, 130, 6}, ((4 * 4096) + (130 * 16) + 6)},
+		{"MkUniPortNum-3", args{4, 130, 6}, ((4 * 65536) + (130 * 256) + 6)},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -67,8 +67,8 @@
 		want uint32
 	}{
 		// TODO: Add test cases.
-		{"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 16) & 255)},
-		{"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 16) & 255)},
+		{"OnuIDFromPortNum-1", args{portNum: 8096}, ((8096 / 256) & 255)},
+		{"OnuIDFromPortNum-2", args{portNum: 9095}, ((9095 / 256) & 255)},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -91,9 +91,10 @@
 		want uint32
 	}{
 		// TODO: Add test cases.
-		{"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 4096) & 15)},
-		// Negative Testcase
-		{"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 4096) & 15)},
+		{"IntfIDFromUniPortNum-1", args{portNum: 8096}, ((8096 / 65536) & 255)},
+		{"IntfIDFromUniPortNum-2", args{portNum: 1024}, ((1024 / 65536) & 255)},
+		{"IntfIDFromUniPortNum-3", args{portNum: 66560}, ((66560 / 65536) & 255)},
+		{"IntfIDFromUniPortNum-4", args{portNum: 16712193}, ((16712193 / 65536) & 255)},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -117,8 +118,8 @@
 	}{
 
 		// TODO: Add test cases.
-		{"UniIDFromPortNum-1", args{portNum: 8096}, (8096 & 15)},
-		{"UniIDFromPortNum-2", args{portNum: 1024}, (1024 & 15)},
+		{"UniIDFromPortNum-1", args{portNum: 8096}, (8096 & 255)},
+		{"UniIDFromPortNum-2", args{portNum: 1024}, (1024 & 255)},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -142,7 +143,7 @@
 		want uint32
 	}{
 		// TODO: Add test cases.
-		{"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 20)) + 120)},
+		{"IntfIDToPortNo-1", args{intfID: 120, intfType: voltha.Port_ETHERNET_NNI}, (uint32(math.Pow(2, 24)) + 120)},
 		{"IntfIDToPortNo-2", args{intfID: 1024, intfType: voltha.Port_ETHERNET_UNI}, 0},
 		{"IntfIDToPortNo-3", args{intfID: 456, intfType: voltha.Port_PON_OLT}, (uint32(2*math.Pow(2, 28)) + 456)},
 		{"IntfIDToPortNo-4", args{intfID: 28, intfType: voltha.Port_PON_ONU}, 0},
@@ -161,6 +162,39 @@
 	}
 }
 
+func TestPortNoToIntfID(t *testing.T) {
+	type args struct {
+		portNo   uint32
+		intfType voltha.Port_PortType
+	}
+	tests := []struct {
+		name string
+		args args
+		want uint32
+	}{
+		// TODO: Add test cases.
+		{"PortNoToIntfID-1", args{portNo: 16777216, intfType: voltha.Port_ETHERNET_NNI}, 0},
+		{"PortNoToIntfID-2", args{portNo: 16777217, intfType: voltha.Port_ETHERNET_NNI}, 1},
+		{"PortNoToIntfID-3", args{portNo: 16777218, intfType: voltha.Port_ETHERNET_NNI}, 2},
+		{"PortNoToIntfID-4", args{portNo: 1024, intfType: voltha.Port_ETHERNET_UNI}, 0},
+		{"PortNoToIntfID-5", args{portNo: 536870912, intfType: voltha.Port_PON_OLT}, 0},
+		{"PortNoToIntfID-6", args{portNo: 536871167, intfType: voltha.Port_PON_OLT}, 255},
+		{"PortNoToIntfID-7", args{portNo: 28, intfType: voltha.Port_PON_ONU}, 0},
+		{"PortNoToIntfID-8", args{portNo: 45, intfType: voltha.Port_UNKNOWN}, 0},
+		{"PortNoToIntfID-9", args{portNo: 45, intfType: voltha.Port_VENET_OLT}, 0},
+		{"PortNoToIntfID-10", args{portNo: 45, intfType: voltha.Port_VENET_ONU}, 0},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := PortNoToIntfID(tt.args.portNo, tt.args.intfType); got != tt.want {
+				t.Errorf("PortNoToIntfID() = %v, want %v", got, tt.want)
+			} else {
+				t.Logf("Expected %v , Actual %v \n", tt.want, got)
+			}
+		})
+	}
+}
+
 func TestIntfIDFromNniPortNum(t *testing.T) {
 	type args struct {
 		portNum uint32
@@ -172,19 +206,19 @@
 		want    uint32
 		wantErr error
 	}{
-		// TODO: Add test cases.
+		// TODO: Add test cases. min 16777216, max 33554432
 		{"IntfIDFromNniPortNum-01", args{portNum: 8081}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 8081)},
 		{"IntfIDFromNniPortNum-02", args{portNum: 9090}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 9090)},
 		{"IntfIDFromNniPortNum-03", args{portNum: 0}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 0)},
 		{"IntfIDFromNniPortNum-04", args{portNum: 65535}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 65535)},
-		{"IntfIDFromNniPortNum-05", args{portNum: 1048575}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 1048575)},
-		{"IntfIDFromNniPortNum-06", args{portNum: 1048576}, 0, nil},
-		{"IntfIDFromNniPortNum-07", args{portNum: 1048577}, 1, nil},
-		{"IntfIDFromNniPortNum-08", args{portNum: 1048578}, 2, nil},
-		{"IntfIDFromNniPortNum-09", args{portNum: 1048579}, 3, nil},
-		{"IntfIDFromNniPortNum-10", args{portNum: 2097150}, 65534, nil},
-		{"IntfIDFromNniPortNum-11", args{portNum: 2097151}, 65535, nil},
-		{"IntfIDFromNniPortNum-12", args{portNum: 3000000}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 3000000)},
+		{"IntfIDFromNniPortNum-05", args{portNum: 16777215}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 16777215)},
+		{"IntfIDFromNniPortNum-06", args{portNum: 16777216}, 0, nil},
+		{"IntfIDFromNniPortNum-07", args{portNum: 16777217}, 1, nil},
+		{"IntfIDFromNniPortNum-08", args{portNum: 16777218}, 2, nil},
+		{"IntfIDFromNniPortNum-09", args{portNum: 16777219}, 3, nil},
+		{"IntfIDFromNniPortNum-10", args{portNum: 33554430}, 16777214, nil},
+		{"IntfIDFromNniPortNum-11", args{portNum: 33554431}, 16777215, nil},
+		{"IntfIDFromNniPortNum-12", args{portNum: 33554432}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 33554432)},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -197,6 +231,41 @@
 	}
 }
 
+func TestIntfIDFromPonPortNum(t *testing.T) {
+	type args struct {
+		portNum uint32
+	}
+
+	tests := []struct {
+		name    string
+		args    args
+		want    uint32
+		wantErr error
+	}{
+		// TODO: Add test cases. min 16777216, max 33554432
+		{"IntfIDFromPonPortNum-02", args{portNum: 9090}, 0, status.Errorf(codes.InvalidArgument, "pon-port-number-out-of-range:%d", 9090)},
+		{"IntfIDFromPonPortNum-03", args{portNum: 0}, 0, status.Errorf(codes.InvalidArgument, "pon-port-number-out-of-range:%d", 0)},
+		{"IntfIDFromPonPortNum-04", args{portNum: 65535}, 0, status.Errorf(codes.InvalidArgument, "pon-port-number-out-of-range:%d", 65535)},
+		{"IntfIDFromPonPortNum-05", args{portNum: 16777215}, 0, status.Errorf(codes.InvalidArgument, "pon-port-number-out-of-range:%d", 16777215)},
+		{"IntfIDFromPonPortNum-01", args{portNum: 536870911}, 0, status.Errorf(codes.InvalidArgument, "pon-port-number-out-of-range:%d", 536870911)},
+		{"IntfIDFromPonPortNum-06", args{portNum: 536870912}, 0, nil},
+		{"IntfIDFromPonPortNum-07", args{portNum: 536870913}, 1, nil},
+		{"IntfIDFromPonPortNum-08", args{portNum: 536870914}, 2, nil},
+		{"IntfIDFromPonPortNum-09", args{portNum: 536870915}, 3, nil},
+		{"IntfIDFromPonPortNum-10", args{portNum: 536871166}, 254, nil},
+		{"IntfIDFromPonPortNum-11", args{portNum: 536871167}, 255, nil},
+		{"IntfIDFromPonPortNum-12", args{portNum: 536871168}, 0, status.Errorf(codes.InvalidArgument, "nni-port-number-out-of-range:%d", 536871168)},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			got, err := IntfIDFromPonPortNum(context.Background(), tt.args.portNum)
+			if got != tt.want {
+				t.Errorf("IntfIDFromPonPortNum(): FOR[%v] WANT[%v and %v] GOT[%v and %v]",
+					tt.args.portNum, tt.want, tt.wantErr, got, err)
+			}
+		})
+	}
+}
 func TestIntfIDToPortTypeName(t *testing.T) {
 	type args struct {
 		intfID uint32
@@ -208,7 +277,7 @@
 		want voltha.Port_PortType
 	}{
 		// TODO: Add test cases.
-		{"IntfIDToPortTypeName-1", args{intfID: 1048576}, voltha.Port_ETHERNET_NNI},
+		{"IntfIDToPortTypeName-1", args{intfID: 16777216}, voltha.Port_ETHERNET_NNI},
 		{"IntfIDToPortTypeName-2", args{intfID: 1000}, voltha.Port_ETHERNET_UNI},
 		{"IntfIDToPortTypeName-2", args{intfID: input}, voltha.Port_PON_OLT},
 	}
@@ -235,7 +304,7 @@
 		uniID  uint32
 	}{
 		// TODO: Add test cases.
-		{"ExtractAccessFromFlow-1", args{inPort: 100, outPort: 1048576}, 100, 0, 6, 4},
+		{"ExtractAccessFromFlow-1", args{inPort: 1540, outPort: 16777216}, 1540, 0, 6, 4},
 		{"ExtractAccessFromFlow-2", args{inPort: 1048576, outPort: 10}, 10, 0, 0, 10},
 	}
 	for _, tt := range tests {
@@ -268,11 +337,12 @@
 		want bool
 	}{
 		// TODO: Add test cases.
-		{"TestIsUpstream-1", args{outPort: 65533}, true},
-		{"TestIsUpstream-2", args{outPort: 1048576}, true},
-		{"TestIsUpstream-3", args{outPort: 1048577}, true},
-		{"TestIsUpstream-4", args{outPort: 1048578}, true},
-		{"TestIsUpstream-6", args{outPort: 1000}, false},
+		{"TestIsUpstream-1", args{outPort: 2147483645}, true}, //controller bound
+		{"TestIsUpstream-2", args{outPort: 16777215}, false},
+		{"TestIsUpstream-3", args{outPort: 16777216}, true},
+		{"TestIsUpstream-4", args{outPort: 16777217}, true},
+		{"TestIsUpstream-5", args{outPort: 33554431}, true},
+		{"TestIsUpstream-6", args{outPort: 33554432}, false},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -293,10 +363,10 @@
 		want bool
 	}{
 		// TODO: Add test cases.
-		{"IsControllerBoundFlow-1", args{outPort: 65533}, true},
-		{"IsControllerBoundFlow-2", args{outPort: 65536}, false},
-		{"IsControllerBoundFlow-3", args{outPort: 65537}, false},
-		{"IsControllerBoundFlow-4", args{outPort: 65538}, false},
+		{"IsControllerBoundFlow-1", args{outPort: 2147483645}, true},
+		{"IsControllerBoundFlow-2", args{outPort: 2147483646}, false},
+		{"IsControllerBoundFlow-3", args{outPort: 4294967293}, true},
+		{"IsControllerBoundFlow-4", args{outPort: 4294967294}, false},
 		{"IsControllerBoundFlow-5", args{outPort: 65539}, false},
 		{"IsControllerBoundFlow-6", args{outPort: 1000}, false},
 	}