Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "encoding/json" |
| 21 | "net/http" |
| 22 | "strings" |
| 23 | "strconv" |
| 24 | |
| 25 | app "voltha-go-controller/internal/pkg/application" |
| 26 | "voltha-go-controller/internal/pkg/of" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame^] | 27 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // IgmpProxy - configurations |
| 31 | type IgmpProxy struct { |
| 32 | FastLeave string `json:"fastleave"` |
| 33 | LastQueryInterval int `json:"lastqueryinterval"` |
| 34 | MaxResp int `json:"maxresp"` |
| 35 | EnableIgmpProvisioning string `json:"enableigmpprovisioning"` |
| 36 | GlobalConnectPointMode string `json:"globalconnectpointmode"` |
| 37 | GlobalConnectPoint string `json:"globalconnectpoint"` |
| 38 | SourceDeviceAndPort string `json:"sourcedeviceandport"` |
| 39 | OutgoingIgmpVlanID int `json:"outgoingigmpvlanid"` |
| 40 | OutgoingIgmpInnerVlanID int `json:"outgoingigmpinnervlanid"` |
| 41 | OutgoingIgmpWithV3 string `json:"outgoingigmpwithv3"` |
| 42 | IgmpCos int `json:"igmpcos"` |
| 43 | IgmpUniCos int `json:"igmpunicos"` |
| 44 | PeriodicQuery string `json:"periodicquery"` |
| 45 | KeepAliveInterval int `json:"keepaliveinterval"` |
| 46 | KeepAliveCount int `json:"keepalivecount"` |
| 47 | RequestDsIgmpPackets bool `json:"requestdsigmppackets"` |
| 48 | } |
| 49 | |
| 50 | // IgmpProxyHandle struct |
| 51 | type IgmpProxyHandle struct { |
| 52 | } |
| 53 | |
| 54 | // ServeHTTP to serve http request |
| 55 | func (iph *IgmpProxyHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 56 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 57 | switch r.Method { |
| 58 | case "POST": |
| 59 | iph.AddIgmpProxyInfo(w, r) |
| 60 | case "DELETE": |
| 61 | iph.DelIgmpProxyInfo(w, r) |
| 62 | default: |
| 63 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // AddIgmpProxyInfo to add igmp proxy info |
| 68 | func (iph *IgmpProxyHandle) AddIgmpProxyInfo(w http.ResponseWriter, r *http.Request) { |
| 69 | |
| 70 | // Get the payload to process the request |
| 71 | d := new(bytes.Buffer) |
| 72 | if _, err := d.ReadFrom(r.Body); err != nil { |
| 73 | logger.Warnw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // Unmarshal the request into service configuration structure |
| 78 | req := &IgmpProxy{} |
| 79 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
| 80 | logger.Warnw(ctx, "Unmarshal Failed", log.Fields{"Reason": err.Error()}) |
| 81 | http.Error(w, err.Error(), http.StatusConflict) |
| 82 | return |
| 83 | } |
| 84 | if mvp := app.GetApplication().GetMvlanProfileByTag(of.VlanType(req.OutgoingIgmpVlanID)); mvp == nil { |
| 85 | logger.Errorw(ctx, "MVLAN ID not configured", log.Fields{"mvlan": req.OutgoingIgmpVlanID}) |
| 86 | http.Error(w, "MVLAN profile does not exists", http.StatusConflict) |
| 87 | return |
| 88 | } |
| 89 | logger.Debugw(ctx, "Received-northbound-add-service-request", log.Fields{"req": req}) |
| 90 | |
| 91 | go iph.addIgmpProxy(w, req) |
| 92 | } |
| 93 | |
| 94 | // DelIgmpProxyInfo to delete igmp proxy info |
| 95 | func (iph *IgmpProxyHandle) DelIgmpProxyInfo(w http.ResponseWriter, r *http.Request) { |
| 96 | |
| 97 | } |
| 98 | |
| 99 | func (iph *IgmpProxyHandle) addIgmpProxy(w http.ResponseWriter, req *IgmpProxy) { |
| 100 | var config McastConfig |
| 101 | |
| 102 | config.OltSerialNum = req.SourceDeviceAndPort |
| 103 | //config.MvlanProfileID = mvp.Name |
| 104 | //config.IgmpProfileID = |
| 105 | //config.IgmpProxyIP = |
| 106 | var splits = strings.Split(req.SourceDeviceAndPort, "/") |
| 107 | config.OltSerialNum = splits[0] |
| 108 | config.MvlanProfileID = "mvlan" + strconv.Itoa(req.OutgoingIgmpVlanID) |
| 109 | |
| 110 | logger.Errorw(ctx, "IgmpProxy", log.Fields{"config":config}) |
| 111 | |
| 112 | if err := app.GetApplication().AddMcastConfig(config.MvlanProfileID, config.IgmpProfileID, |
| 113 | config.IgmpProxyIP, config.OltSerialNum); err != nil { |
| 114 | logger.Errorw(ctx, "northbound-add-mcast-config-failed", log.Fields{"config": config, "Error": err}) |
| 115 | http.Error(w, err.Error(), http.StatusConflict) |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |