blob: e7e25c0e03cc91e7cdbd1f71bb55bd8d94d9f475 [file] [log] [blame]
Akash Sonia8246972023-01-03 10:37:08 +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"
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
34type 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
42type MvlanProfileCfg struct {
43 *MvlanProfile `json:"mvlanProfile"`
44}
45
46// Mvlan - configurations
47type Mvlan struct {
48 IngressVlan int `json:"ingressvlan"`
49 EgressVlan int `json:"egressvlan"`
50 EgressInnerVlan int `json:"egressinnervlan"`
51}
52
53// MvlanProfile structure
54type MvlanProfile struct {
55 Name string `json:"name"`
Akash Sonia8246972023-01-03 10:37:08 +053056 Groups map[string][]string `json:"groups"`
57 Proxy map[string]common.MulticastGroupProxy `json:"proxy"`
Akash Sonia8246972023-01-03 10:37:08 +053058 OLTSerialNum []string `json:"oltserialnum"`
59 ActiveChannelsPerSub int `json:"ActiveChannelsPerSub"`
vinokuma926cb3e2023-03-29 11:41:06 +053060 Mvlan of.VlanType `json:"mvlan"`
61 PonVlan of.VlanType `json:"ponVlan"`
62 IsChannelBasedGroup bool `json:"isChannelBasedGroup"`
Akash Sonia8246972023-01-03 10:37:08 +053063}
64
65// IGMPCfg structure
66type 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
75type MulticastHandle struct {
76}
77
78// ServeHTTP to serve http request
79func (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 {
vinokuma926cb3e2023-03-29 11:41:06 +053082 case cPost:
Akash Sonia8246972023-01-03 10:37:08 +053083 iph.AddMvlanInfo(context.Background(), w, r)
vinokuma926cb3e2023-03-29 11:41:06 +053084 case cDelete:
Akash Sonia8246972023-01-03 10:37:08 +053085 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
92func (iph *MulticastHandle) AddMvlanInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Sonia8246972023-01-03 10:37:08 +053093 // Get the payload to process the request
94 d := new(bytes.Buffer)
95 if _, err := d.ReadFrom(r.Body); err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053096 logger.Errorw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
97 w.WriteHeader(http.StatusInternalServerError)
Akash Sonia8246972023-01-03 10:37:08 +053098 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 Chhabra8c3f1662023-07-19 13:15:16 +0530104 logger.Errorw(ctx, "Failed to Unmarshal Adding Mvlan Info", log.Fields{"req": req, "Reason": err.Error()})
Akash Sonia8246972023-01-03 10:37:08 +0530105 http.Error(w, err.Error(), http.StatusConflict)
106 return
107 }
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530108 logger.Infow(ctx, "Received-northbound-add-mvlanInfo-request", log.Fields{"req": req})
Akash Sonia8246972023-01-03 10:37:08 +0530109
110 go iph.addMvlan(cntx, w, req)
111}
112
113// DelMvlanInfo to delete igmp proxy info
114func (iph *MulticastHandle) DelMvlanInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Sonia8246972023-01-03 10:37:08 +0530115 vars := mux.Vars(r)
116 egressvlan := vars["egressvlan"]
117
Akash Sonia8246972023-01-03 10:37:08 +0530118 name := "mvlan" + egressvlan
119 // HTTP response with 202 accepted for service delete request
120 w.WriteHeader(http.StatusAccepted)
121
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530122 logger.Infow(ctx, "Request for DelMvlanInfo for mvlan", log.Fields{"name": name})
Akash Soni6f369452023-09-19 11:18:28 +0530123 var voltAppIntr app.VoltAppInterface
124 voltApp := app.GetApplication()
125 voltAppIntr = voltApp
126 err := voltAppIntr.DelMvlanProfile(cntx, name)
Akash Sonia8246972023-01-03 10:37:08 +0530127 if err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530128 logger.Errorw(cntx, "Failed to delete Mvlan profile", log.Fields{"name": name, "Error": err.Error()})
129 w.WriteHeader(http.StatusNotFound)
Akash Sonia8246972023-01-03 10:37:08 +0530130 return
131 }
132}
133
134func (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 Chhabra8c3f1662023-07-19 13:15:16 +0530145 logger.Infow(ctx, "northbound-add-mvlan-received", log.Fields{"Config": config})
Akash Soni6f369452023-09-19 11:18:28 +0530146 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 Sonia8246972023-01-03 10:37:08 +0530150 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}