blob: 0bfe39c510287263837a34d2866e7de3302e8da0 [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.
14*/
15
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
26 "github.com/google/gopacket/layers"
27 "github.com/gorilla/mux"
28 "voltha-go-controller/internal/pkg/application"
29 app "voltha-go-controller/internal/pkg/application"
30 "voltha-go-controller/internal/pkg/of"
Tinoj Joseph1d108322022-07-13 10:07:39 +053031 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053032)
33
34//SubscriberDeviceInfo - Subcriber Device Info
35type SubscriberDeviceInfo struct {
36 ID string `json:"id"`
37 NasPortID string `json:"nasPortId"`
38 UplinkPort int `json:"uplinkPort"`
39 Slot int `json:"slot"`
40 HardwareIdentifier string `json:"hardwareIdentifier"`
41 IPAddress net.IP `json:"ipAddress"`
42 NasID string `json:"nasId"`
43 CircuitID string `json:"circuitId"`
44 RemoteID string `json:"remoteId"`
45 UniTagList []UniTagInformation `json:"uniTagList"`
46}
47
48//UniTagInformation - Service information
49type UniTagInformation struct {
50 UniTagMatch int `json:"uniTagMatch"`
51 PonCTag int `json:"ponCTag"`
52 PonSTag int `json:"ponSTag"`
53 UsPonCTagPriority int `json:"usPonCTagPriority"`
54 UsPonSTagPriority int `json:"usPonSTagPriority"`
55 DsPonCTagPriority int `json:"dsPonCTagPriority"`
56 DsPonSTagPriority int `json:"dsPonSTagPriority"`
57 TechnologyProfileID int `json:"technologyProfileId"`
58 UpstreamBandwidthProfile string `json:"upstreamBandwidthProfile"`
59 DownstreamBandwidthProfile string `json:"downstreamBandwidthProfile"`
60 UpstreamOltBandwidthProfile string `json:"upstreamOltBandwidthProfile"`
61 DownstreamOltBandwidthProfile string `json:"downstreamOltBandwidthProfile"`
62 ServiceName string `json:"serviceName"`
63 EnableMacLearning bool `json:"enableMacLearning"`
64 ConfiguredMacAddress string `json:"configuredMacAddress"`
65 IsDhcpRequired bool `json:"isDhcpRequired"`
66 IsIgmpRequired bool `json:"isIgmpRequired"`
67 IsPppoeRequired bool `json:"isPppoeRequired"`
68}
69
Tinoj Josephec742f62022-09-29 19:11:10 +053070
71func init() {
72 // Setup this package so that it's log level can be modified at run time
73 var err error
74 logger, err = log.AddPackageWithDefaultParam()
75 if err != nil {
76 panic(err)
77 }
78}
79
Naveen Sampath04696f72022-06-13 15:19:14 +053080// SubscriberHandle handle SubscriberInfo Requests
81type SubscriberHandle struct {
82}
83
84// ServeHTTP to serve http request
85func (sh *SubscriberHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
86 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
87 switch r.Method {
88 case "POST":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053089 sh.AddSubscriberInfo(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053090 case "DELETE":
Tinoj Joseph07cc5372022-07-18 22:53:51 +053091 sh.DelSubscriberInfo(context.Background(), w, r)
Naveen Sampath04696f72022-06-13 15:19:14 +053092 default:
93 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
94 }
95}
96
97// AddSubscriberInfo to add service
Tinoj Joseph07cc5372022-07-18 22:53:51 +053098func (sh *SubscriberHandle) AddSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +053099
100 // Get the payload to process the request
101 d := new(bytes.Buffer)
102 if _, err := d.ReadFrom(r.Body); err != nil {
103 logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
104 return
105 }
106
107 // Unmarshal the request into service configuration structure
108 req := &SubscriberDeviceInfo{}
109 if err := json.Unmarshal(d.Bytes(), req); err != nil {
110 logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()})
111 http.Error(w, err.Error(), http.StatusConflict)
112 return
113 }
114 logger.Debugw(ctx, "Received-northbound-add-service-request", log.Fields{"req": req})
115
116 //vsCfgList := getVoltServiceFromSrvInfo(req)
117
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530118 go addAllService(cntx, req)
Naveen Sampath04696f72022-06-13 15:19:14 +0530119}
120
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530121func addAllService(cntx context.Context, srvInfo *SubscriberDeviceInfo) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530122
123 //vsCfgList := getVoltServiceFromSrvInfo(srvInfo)
124
125 for _, uniTagInfo := range srvInfo.UniTagList {
126 var vs application.VoltServiceCfg
127
128 svcname := srvInfo.ID + "_"
129 svcname = svcname + srvInfo.NasPortID + "-"
130 svcname = svcname + strconv.Itoa(uniTagInfo.UniTagMatch) + "-"
131 svcname = svcname + strconv.Itoa(uniTagInfo.PonSTag) + "-"
132 svcname = svcname + strconv.Itoa(uniTagInfo.PonCTag) + "-"
133 vs.Name = svcname + strconv.Itoa(uniTagInfo.TechnologyProfileID)
134
135 vs.Port = srvInfo.NasPortID
136 vs.SVlan = of.VlanType(uniTagInfo.PonSTag)
137 vs.CVlan = of.VlanType(uniTagInfo.PonCTag)
138 vs.UniVlan = of.VlanType(uniTagInfo.UniTagMatch)
139 vs.TechProfileID = uint16(uniTagInfo.TechnologyProfileID)
140 vs.UsMeterProfile = uniTagInfo.UpstreamBandwidthProfile
141 vs.DsMeterProfile = uniTagInfo.DownstreamBandwidthProfile
142 vs.IgmpEnabled = uniTagInfo.IsIgmpRequired
143 //vs.McastService = uniTagInfo.IsIgmpRequired
144 if vs.IgmpEnabled {
145 vs.MvlanProfileName = "mvlan" + strconv.Itoa(uniTagInfo.PonSTag)
146 }
Tinoj Josephec742f62022-09-29 19:11:10 +0530147 if uniTagInfo.UsPonSTagPriority == -1 {
148 vs.Pbits = append(vs.Pbits, of.PbitMatchAll)
Naveen Sampath04696f72022-06-13 15:19:14 +0530149 // Process the p-bits received in the request
Tinoj Josephec742f62022-09-29 19:11:10 +0530150 } else {
151 if uniTagInfo.UsPonSTagPriority < 8 {
152 vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.UsPonCTagPriority))
153 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530154
Tinoj Josephec742f62022-09-29 19:11:10 +0530155 if uniTagInfo.UsPonSTagPriority < 8 && uniTagInfo.UsPonSTagPriority != uniTagInfo.DsPonSTagPriority {
156 vs.Pbits = append(vs.Pbits, of.PbitType(uniTagInfo.DsPonCTagPriority))
157 }
Naveen Sampath04696f72022-06-13 15:19:14 +0530158 }
159
160 /*
161 var err error
162 if vs.MacAddr, err = net.ParseMAC(srvInfo.HardwareIdentifier); err != nil {
163 vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00")
164 }*/
165
166 vs.MacAddr, _ = net.ParseMAC("00:00:00:00:00:00")
167 if len(vs.Pbits) == 0 {
168 vs.Pbits = append(vs.Pbits, of.PbitMatchNone)
169 }
170
171 vnetName := strconv.FormatUint(uint64(vs.SVlan), 10) + "-"
172 vnetName = vnetName + strconv.FormatUint(uint64(vs.CVlan), 10) + "-"
173 vnetName = vnetName + strconv.FormatUint(uint64(vs.UniVlan), 10)
174
175 vnetcfg := app.VnetConfig{
176 Name: vnetName,
177 SVlan: vs.SVlan,
178 CVlan: vs.CVlan,
179 UniVlan: vs.UniVlan,
180 SVlanTpid: layers.EthernetTypeDot1Q,
181 DhcpRelay: uniTagInfo.IsDhcpRequired,
182 //MacLearning: req.MacLearning,
183 //ONTEtherTypeClassification: req.ONTEtherTypeClassification,
184 //VlanControl: app.VlanControl(req.VlanControl), //TODO
185 }
186 if uniTagInfo.UsPonSTagPriority < 8 {
187 vnetcfg.UsDhcpPbit = append(vnetcfg.UsDhcpPbit, of.PbitType(uniTagInfo.UsPonSTagPriority))
188 }
189
190 if vs.CVlan != of.VlanAny && vs.SVlan != of.VlanAny {
191 vnetcfg.VlanControl = app.ONUCVlanOLTSVlan
192 } else if vs.CVlan == of.VlanAny && vs.UniVlan == of.VlanAny {
193 vnetcfg.VlanControl = app.OLTSVlan
194 }
195
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530196 if err := app.GetApplication().AddVnet(cntx, vnetcfg, nil); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530197 logger.Errorw(ctx, "AddVnet Failed", log.Fields{"VnetName": vnetName, "Error": err})
198 }
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530199 if err := app.GetApplication().AddService(cntx, vs, nil); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +0530200 logger.Errorw(ctx, "AddService Failed", log.Fields{"Service": vs.Name, "Error": err})
201 }
202
203 }
204}
205
206// DelSubscriberInfo to delete service
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530207func (sh *SubscriberHandle) DelSubscriberInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Naveen Sampath04696f72022-06-13 15:19:14 +0530208
209 vars := mux.Vars(r)
210 id := vars["id"]
211
212 logger.Debugw(ctx, "Received-northbound-del-service-request", log.Fields{"req": id})
213
214 // HTTP response with 202 accepted for service delete request
215 w.WriteHeader(http.StatusAccepted)
216
217 logger.Warnw(ctx, "northbound-del-service-req", log.Fields{"ServiceName": id})
Tinoj Joseph07cc5372022-07-18 22:53:51 +0530218 go app.GetApplication().DelServiceWithPrefix(cntx, id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530219}