blob: a0cc8222ea3bb8406c50cdedecb56b21e5b0f9bc [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 app "voltha-go-controller/internal/pkg/application"
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053024 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
Akash Sonia8246972023-01-03 10:37:08 +053025 "voltha-go-controller/log"
26)
27
28type NetConfig struct {
29 App Apps `json:"apps"`
30}
31
32type Apps struct {
Akash Sonia8246972023-01-03 10:37:08 +053033 IgmpProxy *IgmpProxy `json:"org.opencord.igmpproxy"`
34 McastInfo *McastInfo `json:"org.onosproject.core"`
vinokuma926cb3e2023-03-29 11:41:06 +053035 SubscriberBW SubscriberBW `json:"org.opencord.sadis"`
Akash Sonia8246972023-01-03 10:37:08 +053036}
37type SubscriberBW struct {
38 Bandwidthprofile BWEnteries `json:"bandwidthprofile"`
39 Subscriber Subscriber `json:"sadis"`
40}
41
42type BWEnteries struct {
43 BWInfo []BWProfile `json:"entries"`
44}
45type Subscriber struct {
46 SubscriberInfo []SubscriberDeviceInfo `json:"entries"`
47}
48
49type McastInfo struct {
50 Multicast *Mvlan `json:"multicast"`
51}
52
53type NetConfigHandle struct {
54}
55
56func init() {
57 // Setup this package so that it's log level can be modified at run time
58 var err error
59 logger, err = log.AddPackageWithDefaultParam()
60 if err != nil {
61 panic(err)
62 }
63}
64
65// ServeHTTP to serve http request
66func (nch *NetConfigHandle) NetConfigServeHTTP(w http.ResponseWriter, r *http.Request) {
67 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
68 switch r.Method {
vinokuma926cb3e2023-03-29 11:41:06 +053069 case cPost:
Akash Sonia8246972023-01-03 10:37:08 +053070 nch.AddNetConfigInfo(ctx, w, r)
71 default:
72 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053073 err := errorCodes.ErrOperationNotSupported
74 http.Error(w, err.Error(), http.StatusBadRequest)
Akash Sonia8246972023-01-03 10:37:08 +053075 }
76}
77
78// Populate the network configuration information
79func (nch *NetConfigHandle) AddNetConfigInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Akash Sonia8246972023-01-03 10:37:08 +053080 // Get the payload to process the request
81 d := new(bytes.Buffer)
82 if _, err := d.ReadFrom(r.Body); err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053083 logger.Errorw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()})
84 w.WriteHeader(http.StatusInternalServerError)
Akash Sonia8246972023-01-03 10:37:08 +053085 return
86 }
87
88 // Unmarshal the request into Network configuration structure
89 req := &NetConfig{}
90 if err := json.Unmarshal(d.Bytes(), req); err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053091 logger.Errorw(ctx, "Failed to Unmarshal Adding Network Config", log.Fields{"req": req, "Reason": err.Error()})
Akash Sonia8246972023-01-03 10:37:08 +053092 http.Error(w, err.Error(), http.StatusConflict)
93 return
94 }
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +053095 logger.Infow(ctx, "Received-northbound-network-configuration-request", log.Fields{"req": req})
Akash Sonia8246972023-01-03 10:37:08 +053096
Akash Soni6f369452023-09-19 11:18:28 +053097 var voltAppIntr app.VoltAppInterface
98 voltApp := app.GetApplication()
99 voltAppIntr = voltApp
Akash Sonia8246972023-01-03 10:37:08 +0530100 for _, bwprofile := range req.App.SubscriberBW.Bandwidthprofile.BWInfo {
101 metercfg := app.VoltMeter{
102 Name: bwprofile.ID,
103 Cir: bwprofile.CommittedInformationRate,
104 Cbs: bwprofile.CommittedBurstSize,
105 Pir: bwprofile.PeakInformationRate,
106 Pbs: bwprofile.PeakBurstSize,
107 Air: bwprofile.AssuredInformationRate,
108 Gir: bwprofile.GuaranteedInformationRate,
109 Eir: bwprofile.ExceededInformationRate,
110 Ebs: bwprofile.ExceededBurstSize,
111 }
Akash Soni6f369452023-09-19 11:18:28 +0530112 voltAppIntr.AddMeterProf(cntx, metercfg)
Akash Sonia8246972023-01-03 10:37:08 +0530113 }
114
vinokuma926cb3e2023-03-29 11:41:06 +0530115 for i := range req.App.SubscriberBW.Subscriber.SubscriberInfo {
116 addAllService(cntx, &req.App.SubscriberBW.Subscriber.SubscriberInfo[i])
Akash Sonia8246972023-01-03 10:37:08 +0530117 }
118
119 if req.App.McastInfo != nil {
120 mch := MulticastHandle{}
121 mch.addMvlan(cntx, w, req.App.McastInfo.Multicast)
122 }
123
124 if req.App.IgmpProxy != nil {
125 iph := IgmpProxyHandle{}
126 iph.addIgmpProxy(cntx, w, req.App.IgmpProxy)
127 }
128}