Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 14 | */ |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 15 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 16 | package onosnbi |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 17 | |
| 18 | import ( |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 19 | "context" |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 20 | "encoding/json" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 21 | "net/http" |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 22 | "strconv" |
| 23 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 24 | cntlr "voltha-go-controller/internal/pkg/controller" |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 25 | errorCodes "voltha-go-controller/internal/pkg/errorcodes" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 26 | "voltha-go-controller/internal/pkg/of" |
| 27 | "voltha-go-controller/log" |
| 28 | |
| 29 | "github.com/gorilla/mux" |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // FlowHandle struct to handle flow related REST calls |
| 33 | type FlowHandle struct { |
| 34 | } |
| 35 | |
| 36 | // FlowHandle struct to handle flow related REST calls |
| 37 | type PendingFlowHandle struct { |
| 38 | } |
| 39 | |
| 40 | type TrafficSelector struct { |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | type TrafficTreatment struct { |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
| 47 | type 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 | |
| 59 | func (fh *FlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 60 | 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 Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 66 | err := errorCodes.ErrOperationNotSupported |
| 67 | http.Error(w, err.Error(), http.StatusBadRequest) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 68 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func (pfh *PendingFlowHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 72 | 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 Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 78 | err := errorCodes.ErrOperationNotSupported |
| 79 | http.Error(w, err.Error(), http.StatusBadRequest) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 80 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func (pfh *PendingFlowHandle) GetPendingFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 84 | logger.Debug(ctx, "Received Get Pending Flows request") |
| 85 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 86 | flows, err := cntlr.GetController().GetAllPendingFlows() |
| 87 | if err != nil { |
| 88 | logger.Errorw(ctx, "Failed to get Pending flows", log.Fields{"Error": err}) |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 89 | w.WriteHeader(http.StatusInternalServerError) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 90 | return |
| 91 | } |
| 92 | flowResp := ConvertFlowsToFlowEntry(flows) |
| 93 | FlowRespJSON, err := json.Marshal(flowResp) |
| 94 | if err != nil { |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 95 | logger.Errorw(ctx, "Failed to marshal pending flow response", log.Fields{"Flows": flows, "Error": err}) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 96 | 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 Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 103 | logger.Errorw(ctx, "Failed to write Pending Flow response", log.Fields{"Flows": flows, "Error": err}) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 104 | w.WriteHeader(http.StatusInternalServerError) |
| 105 | } |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func (fh *FlowHandle) GetFlows(cntx context.Context, w http.ResponseWriter, r *http.Request) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 109 | vars := mux.Vars(r) |
| 110 | deviceID := vars["deviceId"] |
| 111 | flowIDStr := vars["flowId"] |
Hitesh Chhabra | 3eb9d62 | 2023-09-07 19:23:15 +0530 | [diff] [blame] | 112 | var flowID uint64 |
| 113 | var parseErr error |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 114 | |
Hitesh Chhabra | 3eb9d62 | 2023-09-07 19:23:15 +0530 | [diff] [blame] | 115 | 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 Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 123 | } |
Hitesh Chhabra | 3eb9d62 | 2023-09-07 19:23:15 +0530 | [diff] [blame] | 124 | |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 125 | var flowResp FlowEntry |
| 126 | if len(deviceID) > 0 && len(flowIDStr) > 0 { |
| 127 | flow, err := fh.getFlow(deviceID, flowID) |
| 128 | if err != nil { |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 129 | logger.Errorw(ctx, "Failed to Fetch flow", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err}) |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 130 | http.Error(w, err.Error(), http.StatusBadRequest) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 131 | return |
| 132 | } |
| 133 | flowResp = ConvertFlowToFlowEntry(flow) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 134 | // flowResp = append(flowResp, flow) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 135 | } else { |
| 136 | flows, err := fh.getAllFlows(deviceID) |
| 137 | if err != nil { |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 138 | logger.Errorw(ctx, "Failed to Fetch flows", log.Fields{"DeviceID": deviceID, "Error": err}) |
Hitesh Chhabra | 7d249a0 | 2023-07-04 21:33:49 +0530 | [diff] [blame] | 139 | http.Error(w, err.Error(), http.StatusBadRequest) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 140 | return |
| 141 | } |
| 142 | flowResp = ConvertFlowsToFlowEntry(flows) |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 143 | // ..flowResp = append(flowResp, flows...) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 144 | } |
| 145 | FlowRespJSON, err := json.Marshal(flowResp) |
| 146 | if err != nil { |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 147 | logger.Errorw(ctx, "Failed to marshal flow response", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err}) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 148 | 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 Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 155 | logger.Errorw(ctx, "Failed to write flow response", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "Error": err}) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 156 | w.WriteHeader(http.StatusInternalServerError) |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 157 | return |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 158 | } |
Hitesh Chhabra | af80b83 | 2023-07-21 15:27:20 +0530 | [diff] [blame] | 159 | logger.Debugw(ctx, "Request for getting Flow specific to flowID and deviceID", log.Fields{"FlowID": flowID, "DeviceID": deviceID, "flowResp": flowResp}) |
Tinoj Joseph | ec742f6 | 2022-09-29 19:11:10 +0530 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | func (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 | |
| 169 | func (fh *FlowHandle) getFlow(deviceID string, flowID uint64) (*of.VoltSubFlow, error) { |
| 170 | return cntlr.GetController().GetFlow(deviceID, flowID) |
| 171 | } |