blob: 232b457896e0f37655b69a6b793ddd6e6df0d732 [file] [log] [blame]
Tinoj Josephec742f62022-09-29 19:11:10 +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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Tinoj Josephec742f62022-09-29 19:11:10 +053015
vinokuma926cb3e2023-03-29 11:41:06 +053016package onosnbi
Tinoj Josephec742f62022-09-29 19:11:10 +053017
18import (
vinokuma926cb3e2023-03-29 11:41:06 +053019 "context"
Tinoj Josephec742f62022-09-29 19:11:10 +053020 "encoding/json"
vinokuma926cb3e2023-03-29 11:41:06 +053021 "net/http"
Tinoj Josephec742f62022-09-29 19:11:10 +053022 "strconv"
23
Tinoj Josephec742f62022-09-29 19:11:10 +053024 cntlr "voltha-go-controller/internal/pkg/controller"
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053025 errorCodes "voltha-go-controller/internal/pkg/errorcodes"
vinokuma926cb3e2023-03-29 11:41:06 +053026 "voltha-go-controller/internal/pkg/of"
27 "voltha-go-controller/log"
28
29 "github.com/gorilla/mux"
Tinoj Josephec742f62022-09-29 19:11:10 +053030)
31
32// FlowHandle struct to handle flow related REST calls
33type FlowHandle struct {
34}
35
36// FlowHandle struct to handle flow related REST calls
37type PendingFlowHandle struct {
38}
39
40type TrafficSelector struct {
Tinoj Josephec742f62022-09-29 19:11:10 +053041}
42
43type TrafficTreatment struct {
Tinoj Josephec742f62022-09-29 19:11:10 +053044}
45
46/*
47type FlowEntry struct {
48 TrafficSelector
49 TrafficTreatment
50 FlowID int
51 AppID int
52 GroupID int
53 Priority int
54 DeviceID string
55 TimeOut int
56 TableID int
57}*/
58
59func (fh *FlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vinokuma926cb3e2023-03-29 11:41:06 +053060 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
61 switch r.Method {
62 case cGet:
63 fh.GetFlows(context.Background(), w, r)
64 default:
65 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053066 err := errorCodes.ErrOperationNotSupported
67 http.Error(w, err.Error(), http.StatusBadRequest)
vinokuma926cb3e2023-03-29 11:41:06 +053068 }
Tinoj Josephec742f62022-09-29 19:11:10 +053069}
70
71func (pfh *PendingFlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vinokuma926cb3e2023-03-29 11:41:06 +053072 logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL})
73 switch r.Method {
74 case cGet:
75 pfh.GetPendingFlows(context.Background(), w, r)
76 default:
77 logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053078 err := errorCodes.ErrOperationNotSupported
79 http.Error(w, err.Error(), http.StatusBadRequest)
vinokuma926cb3e2023-03-29 11:41:06 +053080 }
Tinoj Josephec742f62022-09-29 19:11:10 +053081}
82
83func (pfh *PendingFlowHandle) GetPendingFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053084 logger.Debug(ctx, "Received Get Pending Flows request")
85
Tinoj Josephec742f62022-09-29 19:11:10 +053086 flows, err := cntlr.GetController().GetAllPendingFlows()
87 if err != nil {
88 logger.Errorw(ctx, "Failed to get Pending flows", log.Fields{"Error": err})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +053089 w.WriteHeader(http.StatusInternalServerError)
Tinoj Josephec742f62022-09-29 19:11:10 +053090 return
91 }
92 flowResp := ConvertFlowsToFlowEntry(flows)
93 FlowRespJSON, err := json.Marshal(flowResp)
94 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +053095 logger.Errorw(ctx, "Failed to marshal pending flow response", log.Fields{"Flows": flows, "Error": err})
Tinoj Josephec742f62022-09-29 19:11:10 +053096 w.WriteHeader(http.StatusInternalServerError)
97 return
98 }
99
100 w.Header().Add("Content-Type", "application/json")
101 _, err = w.Write(FlowRespJSON)
102 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530103 logger.Errorw(ctx, "Failed to write Pending Flow response", log.Fields{"Flows": flows, "Error": err})
Tinoj Josephec742f62022-09-29 19:11:10 +0530104 w.WriteHeader(http.StatusInternalServerError)
105 }
Tinoj Josephec742f62022-09-29 19:11:10 +0530106}
107
108func (fh *FlowHandle) GetFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) {
vinokuma926cb3e2023-03-29 11:41:06 +0530109 vars := mux.Vars(r)
110 deviceID := vars["deviceId"]
111 flowIDStr := vars["flowId"]
Hitesh Chhabra3eb9d622023-09-07 19:23:15 +0530112 var flowID uint64
113 var parseErr error
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530114
Hitesh Chhabra3eb9d622023-09-07 19:23:15 +0530115 logger.Debugw(ctx, "Received Get Flows specific to flowID and deviceID", log.Fields{"flowId": flowIDStr, "DeviceID": deviceID})
116 if len(flowIDStr) > 0 {
117 flowID, parseErr = strconv.ParseUint(flowIDStr, 10, 64)
118 if parseErr != nil {
119 logger.Errorw(ctx, "Failed to parse flowIDStr from string to uint64", log.Fields{"flowIDStr": flowIDStr, "Reason": parseErr.Error()})
120 w.WriteHeader(http.StatusInternalServerError)
121 return
122 }
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530123 }
Hitesh Chhabra3eb9d622023-09-07 19:23:15 +0530124
Tinoj Josephec742f62022-09-29 19:11:10 +0530125 var flowResp FlowEntry
126 if len(deviceID) > 0 && len(flowIDStr) > 0 {
127 flow, err := fh.getFlow(deviceID, flowID)
128 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530129 logger.Errorw(ctx, "Failed to Fetch flow", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +0530130 http.Error(w, err.Error(), http.StatusBadRequest)
Tinoj Josephec742f62022-09-29 19:11:10 +0530131 return
132 }
133 flowResp = ConvertFlowToFlowEntry(flow)
vinokuma926cb3e2023-03-29 11:41:06 +0530134 // flowResp = append(flowResp, flow)
Tinoj Josephec742f62022-09-29 19:11:10 +0530135 } else {
136 flows, err := fh.getAllFlows(deviceID)
137 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530138 logger.Errorw(ctx, "Failed to Fetch flows", log.Fields{"DeviceID": deviceID, "Error": err})
Hitesh Chhabra7d249a02023-07-04 21:33:49 +0530139 http.Error(w, err.Error(), http.StatusBadRequest)
Tinoj Josephec742f62022-09-29 19:11:10 +0530140 return
141 }
142 flowResp = ConvertFlowsToFlowEntry(flows)
vinokuma926cb3e2023-03-29 11:41:06 +0530143 // ..flowResp = append(flowResp, flows...)
Tinoj Josephec742f62022-09-29 19:11:10 +0530144 }
145 FlowRespJSON, err := json.Marshal(flowResp)
146 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530147 logger.Errorw(ctx, "Failed to marshal flow response", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err})
Tinoj Josephec742f62022-09-29 19:11:10 +0530148 w.WriteHeader(http.StatusInternalServerError)
149 return
150 }
151
152 w.Header().Add("Content-Type", "application/json")
153 _, err = w.Write(FlowRespJSON)
154 if err != nil {
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530155 logger.Errorw(ctx, "Failed to write flow response", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err})
Tinoj Josephec742f62022-09-29 19:11:10 +0530156 w.WriteHeader(http.StatusInternalServerError)
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530157 return
Tinoj Josephec742f62022-09-29 19:11:10 +0530158 }
Hitesh Chhabraaf80b832023-07-21 15:27:20 +0530159 logger.Debugw(ctx, "Request for getting Flow specific to flowID and deviceID", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "flowResp": flowResp})
Tinoj Josephec742f62022-09-29 19:11:10 +0530160}
161
162func (fh *FlowHandle) getAllFlows(deviceID string) ([]*of.VoltSubFlow, error) {
163 if len(deviceID) == 0 {
164 return cntlr.GetController().GetAllFlows()
165 }
166 return cntlr.GetController().GetFlows(deviceID)
167}
168
169func (fh *FlowHandle) getFlow(deviceID string, flowID uint64) (*of.VoltSubFlow, error) {
170 return cntlr.GetController().GetFlow(deviceID, flowID)
171}