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 | app "voltha-go-controller/internal/pkg/application" |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 24 | errorCodes "voltha-go-controller/internal/pkg/errorcodes" |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 25 | "voltha-go-controller/log" |
| 26 | ) |
| 27 | |
| 28 | type NetConfig struct { |
| 29 | App Apps `json:"apps"` |
| 30 | } |
| 31 | |
| 32 | type Apps struct { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 33 | IgmpProxy *IgmpProxy `json:"org.opencord.igmpproxy"` |
| 34 | McastInfo *McastInfo `json:"org.onosproject.core"` |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 35 | SubscriberBW SubscriberBW `json:"org.opencord.sadis"` |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 36 | } |
| 37 | type SubscriberBW struct { |
| 38 | Bandwidthprofile BWEnteries `json:"bandwidthprofile"` |
| 39 | Subscriber Subscriber `json:"sadis"` |
| 40 | } |
| 41 | |
| 42 | type BWEnteries struct { |
| 43 | BWInfo []BWProfile `json:"entries"` |
| 44 | } |
| 45 | type Subscriber struct { |
| 46 | SubscriberInfo []SubscriberDeviceInfo `json:"entries"` |
| 47 | } |
| 48 | |
| 49 | type McastInfo struct { |
| 50 | Multicast *Mvlan `json:"multicast"` |
| 51 | } |
| 52 | |
| 53 | type NetConfigHandle struct { |
| 54 | } |
| 55 | |
| 56 | func 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 |
| 66 | func (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 { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 69 | case cPost: |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 70 | nch.AddNetConfigInfo(ctx, w, r) |
| 71 | default: |
| 72 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 73 | err := errorCodes.ErrOperationNotSupported |
| 74 | http.Error(w, err.Error(), http.StatusBadRequest) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | // Populate the network configuration information |
| 79 | func (nch *NetConfigHandle) AddNetConfigInfo(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 80 | // Get the payload to process the request |
| 81 | d := new(bytes.Buffer) |
| 82 | if _, err := d.ReadFrom(r.Body); err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 83 | logger.Errorw(ctx, "Error reading buffer", log.Fields{"Reason": err.Error()}) |
| 84 | w.WriteHeader(http.StatusInternalServerError) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 85 | 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 Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 91 | logger.Errorw(ctx, "Failed to Unmarshal Adding Network Config", log.Fields{"req": req, "Reason": err.Error()}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 92 | http.Error(w, err.Error(), http.StatusConflict) |
| 93 | return |
| 94 | } |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 95 | logger.Infow(ctx, "Received-northbound-network-configuration-request", log.Fields{"req": req}) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 96 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 97 | var voltAppIntr app.VoltAppInterface |
| 98 | voltApp := app.GetApplication() |
| 99 | voltAppIntr = voltApp |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 100 | 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 Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 112 | voltAppIntr.AddMeterProf(cntx, metercfg) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 113 | } |
| 114 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 115 | for i := range req.App.SubscriberBW.Subscriber.SubscriberInfo { |
| 116 | addAllService(cntx, &req.App.SubscriberBW.Subscriber.SubscriberInfo[i]) |
Akash Soni | a824697 | 2023-01-03 10:37:08 +0530 | [diff] [blame] | 117 | } |
| 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 | } |