Akash Soni | a824697 | 2023-01-03 10:37:08 +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 | "context" |
| 21 | "encoding/json" |
| 22 | "net/http" |
| 23 | "strconv" |
| 24 | |
| 25 | app "voltha-go-controller/internal/pkg/application" |
| 26 | "voltha-go-controller/internal/pkg/of" |
| 27 | common "voltha-go-controller/internal/pkg/types" |
| 28 | "voltha-go-controller/log" |
| 29 | |
| 30 | "github.com/gorilla/mux" |
| 31 | ) |
| 32 | |
| 33 | // McastConfig structure |
| 34 | type McastConfig struct { |
| 35 | MvlanProfileID string `json:"mvlanProfile"` |
| 36 | IgmpProfileID string `json:"igmpProfile"` |
| 37 | IgmpProxyIP string `json:"igmpProxyIp"` |
| 38 | OltSerialNum string `json:"oltSerialNum"` |
| 39 | } |
| 40 | |
| 41 | // MvlanProfileCfg structure |
| 42 | type MvlanProfileCfg struct { |
| 43 | *MvlanProfile `json:"mvlanProfile"` |
| 44 | } |
| 45 | |
| 46 | // Mvlan - configurations |
| 47 | type Mvlan struct { |
| 48 | IngressVlan int `json:"ingressvlan"` |
| 49 | EgressVlan int `json:"egressvlan"` |
| 50 | EgressInnerVlan int `json:"egressinnervlan"` |
| 51 | } |
| 52 | |
| 53 | // MvlanProfile structure |
| 54 | type MvlanProfile struct { |
| 55 | Name string `json:"name"` |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 56 | Groups map[string][]string `json:"groups"` |
| 57 | Proxy map[string]common.MulticastGroupProxy `json:"proxy"` |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 58 | OLTSerialNum []string `json:"oltserialnum"` |
| 59 | ActiveChannelsPerSub int `json:"ActiveChannelsPerSub"` |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 60 | Mvlan of.VlanType `json:"mvlan"` |
| 61 | PonVlan of.VlanType `json:"ponVlan"` |
| 62 | IsChannelBasedGroup bool `json:"isChannelBasedGroup"` |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // IGMPCfg structure |
| 66 | type IGMPCfg struct { |
| 67 | AppID struct { |
| 68 | IgmpApp struct { |
| 69 | Parameters common.IGMPConfig `json:"igmpproxy"` |
| 70 | } `json:"org.opencord.igmpproxy"` |
| 71 | } `json:"apps"` |
| 72 | } |
| 73 | |
| 74 | // MulticastHandle struct |
| 75 | type MulticastHandle struct { |
| 76 | } |
| 77 | |
| 78 | // ServeHTTP to serve http request |
| 79 | func (iph *MulticastHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 80 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 81 | switch r.Method { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 82 | case cPost: |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 83 | iph.AddMvlanInfo(context.Background(), w, r) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 84 | case cDelete: |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 85 | iph.DelMvlanInfo(context.Background(), w, r) |
| 86 | default: |
| 87 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // AddMvlanInfo to add igmp proxy info |
| 92 | func (iph *MulticastHandle) AddMvlanInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 93 | // Get the payload to process the request |
| 94 | d := new(bytes.Buffer) |
| 95 | if _, err := d.ReadFrom(r.Body); err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 96 | logger.Errorw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 97 | w.WriteHeader(http.StatusInternalServerError) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 98 | return |
| 99 | } |
| 100 | |
| 101 | // Unmarshal the request into service configuration structure |
| 102 | req := &Mvlan{} |
| 103 | if err := json.Unmarshal(d.Bytes(), req); err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 104 | logger.Errorw(ctx, "Failed to Unmarshal Adding Mvlan Info", log.Fields{"req": req, "Reason": err.Error()}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 105 | http.Error(w, err.Error(), http.StatusConflict) |
| 106 | return |
| 107 | } |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 108 | logger.Infow(ctx, "Received-northbound-add-mvlanInfo-request", log.Fields{"req": req}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 109 | |
| 110 | go iph.addMvlan(cntx, w, req) |
| 111 | } |
| 112 | |
| 113 | // DelMvlanInfo to delete igmp proxy info |
| 114 | func (iph *MulticastHandle) DelMvlanInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 115 | vars := mux.Vars(r) |
| 116 | egressvlan := vars["egressvlan"] |
| 117 | |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 118 | name := "mvlan" + egressvlan |
| 119 | // HTTP response with 202 accepted for service delete request |
| 120 | w.WriteHeader(http.StatusAccepted) |
| 121 | |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 122 | logger.Infow(ctx, "Request for DelMvlanInfo for mvlan", log.Fields{"name": name}) |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 123 | var voltAppIntr app.VoltAppInterface |
| 124 | voltApp := app.GetApplication() |
| 125 | voltAppIntr = voltApp |
| 126 | err := voltAppIntr.DelMvlanProfile(cntx, name) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 127 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 128 | logger.Errorw(cntx, "Failed to delete Mvlan profile", log.Fields{"name": name, "Error": err.Error()}) |
| 129 | w.WriteHeader(http.StatusNotFound) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 130 | return |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func (iph *MulticastHandle) addMvlan(cntx context.Context, w http.ResponseWriter, req *Mvlan) { |
| 135 | var config MvlanProfile |
| 136 | var groups []string |
| 137 | |
| 138 | groups = append(groups, "225.0.0.0-239.255.255.255") |
| 139 | config.Name = "mvlan" + strconv.Itoa(req.EgressVlan) |
| 140 | config.Mvlan = of.VlanType(req.EgressVlan) |
| 141 | config.PonVlan = of.VlanType(req.EgressInnerVlan) |
| 142 | config.Groups = make(map[string][]string) |
| 143 | config.Groups["default"] = groups |
| 144 | |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 145 | logger.Infow(ctx, "northbound-add-mvlan-received", log.Fields{"Config": config}) |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 146 | var voltAppIntr app.VoltAppInterface |
| 147 | voltApp := app.GetApplication() |
| 148 | voltAppIntr = voltApp |
| 149 | if err := voltAppIntr.AddMvlanProfile(cntx, config.Name, config.Mvlan, config.PonVlan, config.Groups, |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 150 | config.IsChannelBasedGroup, config.OLTSerialNum, |
| 151 | 255, config.Proxy); err != nil { |
| 152 | logger.Errorw(ctx, "northbound-add-mvlan-failed", log.Fields{"mvlan": config.Name, "Reason": err.Error()}) |
| 153 | http.Error(w, err.Error(), http.StatusConflict) |
| 154 | return |
| 155 | } |
| 156 | logger.Debugw(ctx, "northbound-add-mvlan-successful", log.Fields{"mvlan": config.Name}) |
| 157 | } |