blob: 8067306cec2395371a6ddbd5f8aec2dd823f3e7f [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
Akash Sonia8246972023-01-03 10:37:08 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package nbi
17
18import (
19 "bytes"
Tinoj Joseph07cc5372022-07-18 22:53:51 +053020 "context"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 "encoding/json"
22 "net"
23 "net/http"
24 "strconv"
25
Naveen Sampath04696f72022-06-13 15:19:14 +053026 "voltha-go-controller/internal/pkg/application"
27 app "voltha-go-controller/internal/pkg/application"
28 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053029 "voltha-go-controller/log"
Akash Sonia8246972023-01-03 10:37:08 +053030
31 "github.com/google/gopacket/layers"
32 "github.com/gorilla/mux"
Naveen Sampath04696f72022-06-13 15:19:14 +053033)
34
35//SubscriberDeviceInfo - Subcriber Device Info
36type SubscriberDeviceInfo struct {
37 ID string `json:"id"`
38 NasPortID string `json:"nasPortId"`
39 UplinkPort int `json:"uplinkPort"`
40 Slot int `json:"slot"`
41 HardwareIdentifier string `json:"hardwareIdentifier"`
Akash Soni87a19072023-02-28 00:46:59 +053042 IPAddress string `json:"ipAddress"`
Naveen Sampath04696f72022-06-13 15:19:14 +053043 NasID string `json:"nasId"`
44 CircuitID string `json:"circuitId"`
45 RemoteID string `json:"remoteId"`
Tinoj Joseph50d722c2022-12-06 22:53:22 +053046 NniDhcpTrapVid int `json:"nniDhcpTrapVid"`
Naveen Sampath04696f72022-06-13 15:19:14 +053047 UniTagList []UniTagInformation `json:"uniTagList"`
48}
49
50//UniTagInformation - Service information
51type UniTagInformation struct {
52 UniTagMatch int `json:"uniTagMatch"`
53 PonCTag int `json:"ponCTag"`
54 PonSTag int `json:"ponSTag"`
55 UsPonCTagPriority int `json:"usPonCTagPriority"`
56 UsPonSTagPriority int `json:"usPonSTagPriority"`
57 DsPonCTagPriority int `json:"dsPonCTagPriority"`
58 DsPonSTagPriority int `json:"dsPonSTagPriority"`
59 TechnologyProfileID int `json:"technologyProfileId"`
60 UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"`
61 DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"`
62 UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"`
63 DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"`
64 ServiceName string `json:"serviceName"`
65 EnableMacLearning bool `json:"enableMacLearning"`
66 ConfiguredMacAddress string `json:"configuredMacAddress"`
67 IsDhcpRequired bool `json:"isDhcpRequired"`
68 IsIgmpRequired bool `json:"isIgmpRequired"`
69 IsPppoeRequired bool `json:"isPppoeRequired"`
70}
71
Tinoj Josephec742f62022-09-29 19:11:10 +053072func init() {
Akash Sonia8246972023-01-03 10:37:08 +053073 // Setup this package so that it's log level can be modified at run time
74 var err error
75 logger, err = log.AddPackageWithDefaultParam()
76 if err != nil {
77 panic(err)
78 }
Tinoj Josephec742f62022-09-29 19:11:10 +053079}
80
Naveen Sampath04696f72022-06-13 15:19:14 +053081// SubscriberHandle handle SubscriberInfo Requests
82type SubscriberHandle struct {
83}
84
85// ServeHTTP to serve http request
86func (sh *SubscriberHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
87 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
88 switch r.Method {
89 case "POST":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053090 sh.AddSubscriberInfo(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053091 case "DELETE":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053092 sh.DelSubscriberInfo(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053093 default:
94 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
95 }
96}
97
98// AddSubscriberInfo to add service
Tinoj Joseph07cc5372022-07-18 22:53:51 +053099func (sh *SubscriberHandle) AddSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530100
101 // Get the payload to process the request
102 d := new(bytes.Buffer)
Akash Sonia8246972023-01-03 10:37:08 +0530103 if _, err := d.ReadFrom(r.Body); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530104 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
105 return
106 }
107
108 // Unmarshal the request into service configuration structure
109 req := &SubscriberDeviceInfo{}
110 if err := json.Unmarshal(d.Bytes(), req); err != nil {
111 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
112 http.Error(w, err.Error(), http.StatusConflict)
113 return
114 }
115 logger.Debugw(ctx, "Received-northbound-add-service-request", log.Fields{"req": req})
116
117 //vsCfgList := getVoltServiceFromSrvInfo(req)
118
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530119 go addAllService(cntx, req)
Naveen Sampath04696f72022-06-13 15:19:14 +0530120}
121
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530122func addAllService(cntx context.Context, srvInfo *SubscriberDeviceInfo) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530123
124 //vsCfgList := getVoltServiceFromSrvInfo(srvInfo)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530125 va := app.GetApplication()
126 if len(srvInfo.UniTagList) == 0 {
127 logger.Debugw(ctx, "Received OLT configuration", log.Fields{"req": srvInfo})
Akash Sonia8246972023-01-03 10:37:08 +0530128 err := va.AddDeviceConfig(cntx, srvInfo.ID, srvInfo.HardwareIdentifier, srvInfo.NasID, srvInfo.IPAddress, srvInfo.UplinkPort, srvInfo.NniDhcpTrapVid)
129 if err != nil {
130 logger.Warnw(ctx, "Device config addition failed :", log.Fields{"Reason": err.Error()})
131 }
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530132 return
133 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530134 for _, uniTagInfo := range srvInfo.UniTagList {
135 var vs application.VoltServiceCfg
136
137 svcname := srvInfo.ID + "_"
138 svcname = svcname + srvInfo.NasPortID + "-"
139 svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-"
140 svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-"
141 svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-"
142 vs.Name = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID)
143
144 vs.Port = srvInfo.NasPortID
145 vs.SVlan = of.VlanType(uniTagInfo.PonSTag)
146 vs.CVlan = of.VlanType(uniTagInfo.PonCTag)
147 vs.UniVlan = of.VlanType(uniTagInfo.UniTagMatch)
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530148 vs.UsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority)
149 vs.UsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority)
150 vs.DsPonCTagPriority = of.PbitType(uniTagInfo.UsPonCTagPriority)
151 vs.DsPonSTagPriority = of.PbitType(uniTagInfo.UsPonSTagPriority)
Naveen Sampath04696f72022-06-13 15:19:14 +0530152 vs.TechProfileID = uint16(uniTagInfo.TechnologyProfileID)
153 vs.UsMeterProfile = uniTagInfo.UpstreamBandwidthProfile
154 vs.DsMeterProfile = uniTagInfo.DownstreamBandwidthProfile
155 vs.IgmpEnabled = uniTagInfo.IsIgmpRequired
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530156 vs.ServiceType = uniTagInfo.ServiceName
157
158 if uniTagInfo.ServiceName == app.DPU_MGMT_TRAFFIC ||
Akash Sonia8246972023-01-03 10:37:08 +0530159 uniTagInfo.ServiceName == app.DPU_ANCP_TRAFFIC ||
160 uniTagInfo.ServiceName == app.FTTB_SUBSCRIBER_TRAFFIC {
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530161 vs.UniVlan = vs.CVlan
162 vs.Pbits = append(vs.Pbits, of.PbitMatchAll)
163 } else {
Akash Sonia8246972023-01-03 10:37:08 +0530164 if uniTagInfo.UsPonSTagPriority == -1 {
165 vs.Pbits = append(vs.Pbits, of.PbitMatchAll)
166 // Process the p-bits received in the request
167 } else {
168 if uniTagInfo.UsPonSTagPriority < 8 {
169 vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.UsPonCTagPriority))
170 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530171
Akash Sonia8246972023-01-03 10:37:08 +0530172 if uniTagInfo.UsPonSTagPriority < 8 && uniTagInfo.UsPonSTagPriority != uniTagInfo.DsPonSTagPriority {
173 vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.DsPonCTagPriority))
174 }
Tinoj Josephec742f62022-09-29 19:11:10 +0530175 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530176 }
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530177 //vs.McastService = uniTagInfo.IsIgmpRequired
178 if vs.IgmpEnabled {
179 vs.MvlanProfileName = "mvlan" + strconv.Itoa(uniTagInfo.PonSTag)
180 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 /*
Akash Sonia8246972023-01-03 10:37:08 +0530182 var err error
183 if vs.MacAddr, err = net.ParseMAC(srvInfo.HardwareIdentifier); err != nil {
184 vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00")
185 }*/
Naveen Sampath04696f72022-06-13 15:19:14 +0530186
187 vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00")
188 if len(vs.Pbits) == 0 {
189 vs.Pbits = append(vs.Pbits, of.PbitMatchNone)
190 }
191
192 vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-"
193 vnetName = vnetName + strconv.FormatUint(uint64(vs.CVlan), 10) + "-"
194 vnetName = vnetName + strconv.FormatUint(uint64(vs.UniVlan), 10)
195
196 vnetcfg := app.VnetConfig{
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530197 Name: vnetName,
198 SVlan: vs.SVlan,
199 CVlan: vs.CVlan,
200 UniVlan: vs.UniVlan,
201 SVlanTpid: layers.EthernetTypeDot1Q,
202 DhcpRelay: uniTagInfo.IsDhcpRequired,
203 VnetType: uniTagInfo.ServiceName,
204 UsPonCTagPriority: vs.UsPonCTagPriority,
205 UsPonSTagPriority: vs.UsPonSTagPriority,
206 DsPonCTagPriority: vs.UsPonCTagPriority,
207 DsPonSTagPriority: vs.UsPonSTagPriority,
Naveen Sampath04696f72022-06-13 15:19:14 +0530208 //ONTEtherTypeClassification: req.ONTEtherTypeClassification,
209 //VlanControl: app.VlanControl(req.VlanControl), //TODO
210 }
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530211 if uniTagInfo.EnableMacLearning {
212 vnetcfg.MacLearning = app.Learn
213 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530214 if uniTagInfo.UsPonSTagPriority < 8 {
215 vnetcfg.UsDhcpPbit = append(vnetcfg.UsDhcpPbit, of.PbitType(uniTagInfo.UsPonSTagPriority))
216 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530217 if vs.CVlan != of.VlanAny && vs.SVlan != of.VlanAny {
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530218 if uniTagInfo.ServiceName == app.DPU_MGMT_TRAFFIC ||
Akash Sonia8246972023-01-03 10:37:08 +0530219 uniTagInfo.ServiceName == app.DPU_ANCP_TRAFFIC {
Tinoj Joseph50d722c2022-12-06 22:53:22 +0530220 vnetcfg.VlanControl = app.ONUCVlan
221 } else if uniTagInfo.ServiceName == app.FTTB_SUBSCRIBER_TRAFFIC {
222 vnetcfg.VlanControl = app.OLTSVlan
223 } else {
224 vnetcfg.VlanControl = app.ONUCVlanOLTSVlan
225 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530226 } else if vs.CVlan == of.VlanAny && vs.UniVlan == of.VlanAny {
227 vnetcfg.VlanControl = app.OLTSVlan
228 }
229
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530230 if err := app.GetApplication().AddVnet(cntx, vnetcfg, nil); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530231 logger.Errorw(ctx, "AddVnet Failed", log.Fields{"VnetName": vnetName, "Error": err})
232 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530233 if err := app.GetApplication().AddService(cntx, vs, nil); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530234 logger.Errorw(ctx, "AddService Failed", log.Fields{"Service": vs.Name, "Error": err})
235 }
236
237 }
238}
239
240// DelSubscriberInfo to delete service
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530241func (sh *SubscriberHandle) DelSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530242
243 vars := mux.Vars(r)
244 id := vars["id"]
245
246 logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id})
247
248 // HTTP response with 202 accepted for service delete request
249 w.WriteHeader(http.StatusAccepted)
250
251 logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530252 go app.GetApplication().DelServiceWithPrefix(cntx, id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530253}