Merge "Adding scope of RICPlatform that are under Apache License"
[ric-plt/rtmgr.git] / pkg / nbi / httprestful.go
index 336e615..9606288 100644 (file)
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+
+   This source code is part of the near-RT RIC (RAN Intelligent Controller)
+   platform project (RICP).
+
 ==================================================================================
 */
 /*
 ==================================================================================
 */
 /*
 
 package nbi
 
 
 package nbi
 
+//noinspection GoUnresolvedReference,GoUnresolvedReference,GoUnresolvedReference,GoUnresolvedReference,GoUnresolvedReference,GoUnresolvedReference
 import (
 import (
+       "encoding/json"
+       "errors"
        "fmt"
        "fmt"
-       "os"
-       "time"
        "net/url"
        "net/url"
-       "strconv"
-       "errors"
-       "encoding/json"
-       "routing-manager/pkg/rtmgr"
-       "routing-manager/pkg/rpe"
-       "routing-manager/pkg/sdl"
+       "os"
        "routing-manager/pkg/models"
        "routing-manager/pkg/restapi"
        "routing-manager/pkg/restapi/operations"
        "routing-manager/pkg/models"
        "routing-manager/pkg/restapi"
        "routing-manager/pkg/restapi/operations"
-       "github.com/go-openapi/runtime/middleware"
        "routing-manager/pkg/restapi/operations/handle"
        "routing-manager/pkg/restapi/operations/handle"
-       loads "github.com/go-openapi/loads"
+       "routing-manager/pkg/rpe"
+       "routing-manager/pkg/rtmgr"
+       "routing-manager/pkg/sdl"
+       "strconv"
+       "time"
+
+       "github.com/go-openapi/loads"
+       "github.com/go-openapi/runtime/middleware"
 )
 
 //var myClient = &http.Client{Timeout: 1 * time.Second}
 
 type HttpRestful struct {
 )
 
 //var myClient = &http.Client{Timeout: 1 * time.Second}
 
 type HttpRestful struct {
-       NbiEngine
+       Engine
        LaunchRest                   LaunchRestHandler
        RecvXappCallbackData         RecvXappCallbackDataHandler
        ProvideXappHandleHandlerImpl ProvideXappHandleHandlerImpl
        LaunchRest                   LaunchRestHandler
        RecvXappCallbackData         RecvXappCallbackDataHandler
        ProvideXappHandleHandlerImpl ProvideXappHandleHandlerImpl
@@ -76,8 +83,8 @@ func recvXappCallbackData(dataChannel <-chan *models.XappCallbackData) (*[]rtmgr
                xappData = <-dataChannel
        }
        if nil != xappData {
                xappData = <-dataChannel
        }
        if nil != xappData {
-                var xapps []rtmgr.XApp
-                err := json.Unmarshal([]byte(xappData.XApps), &xapps)
+               var xapps []rtmgr.XApp
+               err := json.Unmarshal([]byte(xappData.XApps), &xapps)
                return &xapps, err
        } else {
                rtmgr.Logger.Info("No data")
                return &xapps, err
        } else {
                rtmgr.Logger.Info("No data")
@@ -85,17 +92,16 @@ func recvXappCallbackData(dataChannel <-chan *models.XappCallbackData) (*[]rtmgr
 
        rtmgr.Logger.Debug("Nothing received on the Http interface")
        return nil, nil
 
        rtmgr.Logger.Debug("Nothing received on the Http interface")
        return nil, nil
-
 }
 
 func validateXappCallbackData(callbackData *models.XappCallbackData) error {
        if len(callbackData.XApps) == 0 {
 }
 
 func validateXappCallbackData(callbackData *models.XappCallbackData) error {
        if len(callbackData.XApps) == 0 {
-               return fmt.Errorf("Invalid Data field: \"%s\"", callbackData.XApps)
+               return fmt.Errorf("invalid Data field: \"%s\"", callbackData.XApps)
        }
        var xapps []rtmgr.XApp
        }
        var xapps []rtmgr.XApp
-        err := json.Unmarshal([]byte(callbackData.XApps), &xapps)
-        if err != nil {
-               return fmt.Errorf("Unmarshal failed: \"%s\"", err.Error())
+       err := json.Unmarshal([]byte(callbackData.XApps), &xapps)
+       if err != nil {
+               return fmt.Errorf("unmarshal failed: \"%s\"", err.Error())
        }
        return nil
 }
        }
        return nil
 }
@@ -106,10 +112,10 @@ func provideXappHandleHandlerImpl(datach chan<- *models.XappCallbackData, data *
        }
        err := validateXappCallbackData(data)
        if err != nil {
        }
        err := validateXappCallbackData(data)
        if err != nil {
-               rtmgr.Logger.Warn("XApp callback data validation failed: "+err.Error())
+               rtmgr.Logger.Warn("XApp callback data validation failed: " + err.Error())
                return err
        } else {
                return err
        } else {
-               datach<-data
+               datach <- data
                return nil
        }
 }
                return nil
        }
 }
@@ -126,7 +132,7 @@ func validateXappSubscriptionData(data *models.XappSubscriptionData) error {
 }
 
 func provideXappSubscriptionHandleImpl(subchan chan<- *models.XappSubscriptionData,
 }
 
 func provideXappSubscriptionHandleImpl(subchan chan<- *models.XappSubscriptionData,
-                                       data *models.XappSubscriptionData) error {
+       data *models.XappSubscriptionData) error {
        rtmgr.Logger.Debug("Invoked provideXappSubscriptionHandleImpl")
        err := validateXappSubscriptionData(data)
        if err != nil {
        rtmgr.Logger.Debug("Invoked provideXappSubscriptionHandleImpl")
        err := validateXappSubscriptionData(data)
        if err != nil {
@@ -141,7 +147,7 @@ func provideXappSubscriptionHandleImpl(subchan chan<- *models.XappSubscriptionDa
 
 func subscriptionExists(data *models.XappSubscriptionData) bool {
        present := false
 
 func subscriptionExists(data *models.XappSubscriptionData) bool {
        present := false
-       sub := rtmgr.Subscription{SubID:*data.SubscriptionID, Fqdn:*data.Address, Port:*data.Port,}
+       sub := rtmgr.Subscription{SubID: *data.SubscriptionID, Fqdn: *data.Address, Port: *data.Port}
        for _, elem := range rtmgr.Subs {
                if elem == sub {
                        present = true
        for _, elem := range rtmgr.Subs {
                if elem == sub {
                        present = true
@@ -152,7 +158,7 @@ func subscriptionExists(data *models.XappSubscriptionData) bool {
 }
 
 func deleteXappSubscriptionHandleImpl(subdelchan chan<- *models.XappSubscriptionData,
 }
 
 func deleteXappSubscriptionHandleImpl(subdelchan chan<- *models.XappSubscriptionData,
-                                        data *models.XappSubscriptionData) error {
+       data *models.XappSubscriptionData) error {
        rtmgr.Logger.Debug("Invoked deleteXappSubscriptionHandleImpl")
        err := validateXappSubscriptionData(data)
        if err != nil {
        rtmgr.Logger.Debug("Invoked deleteXappSubscriptionHandleImpl")
        err := validateXappSubscriptionData(data)
        if err != nil {
@@ -161,8 +167,8 @@ func deleteXappSubscriptionHandleImpl(subdelchan chan<- *models.XappSubscription
        }
 
        if !subscriptionExists(data) {
        }
 
        if !subscriptionExists(data) {
-               rtmgr.Logger.Warn("Subscription not found: %d", *data.SubscriptionID)
-               err := fmt.Errorf("Subscription not found: %d", *data.SubscriptionID)
+               rtmgr.Logger.Warn("subscription not found: %d", *data.SubscriptionID)
+               err := fmt.Errorf("subscription not found: %d", *data.SubscriptionID)
                return err
        }
 
                return err
        }
 
@@ -171,40 +177,40 @@ func deleteXappSubscriptionHandleImpl(subdelchan chan<- *models.XappSubscription
 }
 
 func launchRest(nbiif *string, datach chan<- *models.XappCallbackData, subchan chan<- *models.XappSubscriptionData,
 }
 
 func launchRest(nbiif *string, datach chan<- *models.XappCallbackData, subchan chan<- *models.XappSubscriptionData,
-                                                               subdelchan chan<- *models.XappSubscriptionData) {
-        swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
-        if err != nil {
-                //log.Fatalln(err)
-                rtmgr.Logger.Error(err.Error())
-                os.Exit(1)
-        }
+       subdelchan chan<- *models.XappSubscriptionData) {
+       swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
+       if err != nil {
+               //log.Fatalln(err)
+               rtmgr.Logger.Error(err.Error())
+               os.Exit(1)
+       }
        nbiUrl, err := url.Parse(*nbiif)
        if err != nil {
                rtmgr.Logger.Error(err.Error())
                os.Exit(1)
        }
        nbiUrl, err := url.Parse(*nbiif)
        if err != nil {
                rtmgr.Logger.Error(err.Error())
                os.Exit(1)
        }
-        api := operations.NewRoutingManagerAPI(swaggerSpec)
-        server := restapi.NewServer(api)
-        defer server.Shutdown()
-
-        server.Port, err = strconv.Atoi(nbiUrl.Port())
-        if err != nil {
-               rtmgr.Logger.Error("Invalid NBI RestAPI port")
-               os.Exit(1)
-        }
-        server.Host = "0.0.0.0"
-        // set handlers
-        api.HandleProvideXappHandleHandler = handle.ProvideXappHandleHandlerFunc(
-                func(params handle.ProvideXappHandleParams) middleware.Responder {
-                rtmgr.Logger.Info("Data received on Http interface")
-               err := provideXappHandleHandlerImpl(datach, params.XappCallbackData)
-               if err != nil {
-                       rtmgr.Logger.Error("Invalid XApp callback data: "+err.Error())
-                       return handle.NewProvideXappHandleBadRequest()
-               } else {
-                       return handle.NewGetHandlesOK()
-               }
-        })
+       api := operations.NewRoutingManagerAPI(swaggerSpec)
+       server := restapi.NewServer(api)
+       defer server.Shutdown()
+
+       server.Port, err = strconv.Atoi(nbiUrl.Port())
+       if err != nil {
+               rtmgr.Logger.Error("Invalid NBI RestAPI port")
+               os.Exit(1)
+       }
+       server.Host = "0.0.0.0"
+       // set handlers
+       api.HandleProvideXappHandleHandler = handle.ProvideXappHandleHandlerFunc(
+               func(params handle.ProvideXappHandleParams) middleware.Responder {
+                       rtmgr.Logger.Info("Data received on Http interface")
+                       err := provideXappHandleHandlerImpl(datach, params.XappCallbackData)
+                       if err != nil {
+                               rtmgr.Logger.Error("Invalid XApp callback data: " + err.Error())
+                               return handle.NewProvideXappHandleBadRequest()
+                       } else {
+                               return handle.NewGetHandlesOK()
+                       }
+               })
        api.HandleProvideXappSubscriptionHandleHandler = handle.ProvideXappSubscriptionHandleHandlerFunc(
                func(params handle.ProvideXappSubscriptionHandleParams) middleware.Responder {
                        err := provideXappSubscriptionHandleImpl(subchan, params.XappSubscriptionData)
        api.HandleProvideXappSubscriptionHandleHandler = handle.ProvideXappSubscriptionHandleHandlerFunc(
                func(params handle.ProvideXappSubscriptionHandleParams) middleware.Responder {
                        err := provideXappSubscriptionHandleImpl(subchan, params.XappSubscriptionData)
@@ -223,80 +229,75 @@ func launchRest(nbiif *string, datach chan<- *models.XappCallbackData, subchan c
                                return handle.NewGetHandlesOK()
                        }
                })
                                return handle.NewGetHandlesOK()
                        }
                })
-        // start to serve API
-        rtmgr.Logger.Info("Starting the HTTP Rest service")
-        if err := server.Serve(); err != nil {
-                rtmgr.Logger.Error(err.Error())
-        }
+       // start to serve API
+       rtmgr.Logger.Info("Starting the HTTP Rest service")
+       if err := server.Serve(); err != nil {
+               rtmgr.Logger.Error(err.Error())
+       }
 }
 
 }
 
-func httpGetXapps(xmurl string) (*[]rtmgr.XApp, error) {
-        rtmgr.Logger.Info("Invoked httpgetter.fetchXappList: " + xmurl)
-        r, err := myClient.Get(xmurl)
-        if err != nil {
-                return nil, err
-        }
-        defer r.Body.Close()
-
-        if r.StatusCode == 200 {
-                rtmgr.Logger.Debug("http client raw response: %v", r)
-                var xapps []rtmgr.XApp
-                err = json.NewDecoder(r.Body).Decode(&xapps)
-                if err != nil {
-                        rtmgr.Logger.Warn("Json decode failed: " + err.Error())
-                }
-                rtmgr.Logger.Info("HTTP GET: OK")
-                rtmgr.Logger.Debug("httpgetter.fetchXappList returns: %v", xapps)
-                return &xapps, err
-        }
-        rtmgr.Logger.Warn("httpgetter got an unexpected http status code: %v", r.StatusCode)
-        return nil, nil
+func httpGetXApps(xmurl string) (*[]rtmgr.XApp, error) {
+       rtmgr.Logger.Info("Invoked httpgetter.fetchXappList: " + xmurl)
+       r, err := myClient.Get(xmurl)
+       if err != nil {
+               return nil, err
+       }
+       defer r.Body.Close()
+
+       if r.StatusCode == 200 {
+               rtmgr.Logger.Debug("http client raw response: %v", r)
+               var xapps []rtmgr.XApp
+               err = json.NewDecoder(r.Body).Decode(&xapps)
+               if err != nil {
+                       rtmgr.Logger.Warn("Json decode failed: " + err.Error())
+               }
+               rtmgr.Logger.Info("HTTP GET: OK")
+               rtmgr.Logger.Debug("httpgetter.fetchXappList returns: %v", xapps)
+               return &xapps, err
+       }
+       rtmgr.Logger.Warn("httpgetter got an unexpected http status code: %v", r.StatusCode)
+       return nil, nil
 }
 
 }
 
-func retrieveStartupData(xmurl string, nbiif string, fileName string, configfile string, sdlEngine sdl.SdlEngine) error {
-        var readErr error
-        var maxRetries = 10
-
-                for i := 1; i <= maxRetries; i++ {
-                        time.Sleep(2 * time.Second)
-
-                        xappData, err := httpGetXapps(xmurl)
-
-                        if xappData != nil && err == nil {
-                               pcData, confErr := rtmgr.GetPlatformComponents(configfile)
-                               if confErr != nil {
-                                       rtmgr.Logger.Error(confErr.Error())
-                                       return confErr
-                               }
-
-                                rtmgr.Logger.Info("Recieved intial xapp data and platform data, writing into SDL.")
-                               // Combine the xapps data and platform data before writing to the SDL
-                               ricData := &rtmgr.RicComponents{Xapps: *xappData, Pcs: *pcData}
-
-                                writeErr := sdlEngine.WriteAll(fileName, ricData)
-                                if writeErr != nil {
-                                        rtmgr.Logger.Error(writeErr.Error())
-                                }
-                                // post subscription req to appmgr
-                                readErr = PostSubReq(xmurl, nbiif)
-                                if readErr == nil {
-                                        return nil
-                                }
-                        } else if err == nil {
-                                readErr = errors.New("Unexpected HTTP status code")
-                        } else {
-                                rtmgr.Logger.Warn("cannot get xapp data due to: " + err.Error())
-                                readErr = err
-                        }
-                }
-        return readErr
+func retrieveStartupData(xmurl string, nbiif string, fileName string, configfile string, sdlEngine sdl.Engine) error {
+       var readErr error
+       var maxRetries = 10
+       for i := 1; i <= maxRetries; i++ {
+               time.Sleep(2 * time.Second)
+               xappData, err := httpGetXApps(xmurl)
+               if xappData != nil && err == nil {
+                       pcData, confErr := rtmgr.GetPlatformComponents(configfile)
+                       if confErr != nil {
+                               rtmgr.Logger.Error(confErr.Error())
+                               return confErr
+                       }
+                       rtmgr.Logger.Info("Recieved intial xapp data and platform data, writing into SDL.")
+                       // Combine the xapps data and platform data before writing to the SDL
+                       ricData := &rtmgr.RicComponents{XApps: *xappData, Pcs: *pcData}
+                       writeErr := sdlEngine.WriteAll(fileName, ricData)
+                       if writeErr != nil {
+                               rtmgr.Logger.Error(writeErr.Error())
+                       }
+                       // post subscription req to appmgr
+                       readErr = PostSubReq(xmurl, nbiif)
+                       if readErr == nil {
+                               return nil
+                       }
+               } else if err == nil {
+                       readErr = errors.New("unexpected HTTP status code")
+               } else {
+                       rtmgr.Logger.Warn("cannot get xapp data due to: " + err.Error())
+                       readErr = err
+               }
+       }
+       return readErr
 }
 
 func (r *HttpRestful) Initialize(xmurl string, nbiif string, fileName string, configfile string,
 }
 
 func (r *HttpRestful) Initialize(xmurl string, nbiif string, fileName string, configfile string,
-                                sdlEngine sdl.SdlEngine, rpeEngine rpe.RpeEngine, triggerSBI chan<- bool) error {
+       sdlEngine sdl.Engine, rpeEngine rpe.Engine, triggerSBI chan<- bool) error {
        err := r.RetrieveStartupData(xmurl, nbiif, fileName, configfile, sdlEngine)
        if err != nil {
        err := r.RetrieveStartupData(xmurl, nbiif, fileName, configfile, sdlEngine)
        if err != nil {
-               rtmgr.Logger.Error("Exiting as nbi failed to get the intial startup data from the xapp manager: " + err.Error())
+               rtmgr.Logger.Error("Exiting as nbi failed to get the initial startup data from the xapp manager: " + err.Error())
                return err
        }
 
                return err
        }
 
@@ -314,7 +315,7 @@ func (r *HttpRestful) Initialize(xmurl string, nbiif string, fileName string, co
                        if err != nil {
                                rtmgr.Logger.Error("cannot get data from rest api dute to: " + err.Error())
                        } else if data != nil {
                        if err != nil {
                                rtmgr.Logger.Error("cannot get data from rest api dute to: " + err.Error())
                        } else if data != nil {
-                               sdlEngine.WriteXapps(fileName, data)
+                               sdlEngine.WriteXApps(fileName, data)
                                triggerSBI <- true
                        }
                }
                                triggerSBI <- true
                        }
                }
@@ -346,8 +347,8 @@ func (r *HttpRestful) Terminate() error {
 }
 
 func addSubscription(subs *rtmgr.SubscriptionList, xappSubData *models.XappSubscriptionData) bool {
 }
 
 func addSubscription(subs *rtmgr.SubscriptionList, xappSubData *models.XappSubscriptionData) bool {
-       var b bool = false
-       sub := rtmgr.Subscription{SubID:*xappSubData.SubscriptionID, Fqdn:*xappSubData.Address, Port:*xappSubData.Port,}
+       var b = false
+       sub := rtmgr.Subscription{SubID: *xappSubData.SubscriptionID, Fqdn: *xappSubData.Address, Port: *xappSubData.Port}
        for _, elem := range *subs {
                if elem == sub {
                        rtmgr.Logger.Warn("rtmgr.addSubscription: Subscription already present: %v", elem)
        for _, elem := range *subs {
                if elem == sub {
                        rtmgr.Logger.Warn("rtmgr.addSubscription: Subscription already present: %v", elem)
@@ -362,8 +363,8 @@ func addSubscription(subs *rtmgr.SubscriptionList, xappSubData *models.XappSubsc
 
 func delSubscription(subs *rtmgr.SubscriptionList, xappSubData *models.XappSubscriptionData) bool {
        rtmgr.Logger.Debug("Deleteing the subscription from the subscriptions list")
 
 func delSubscription(subs *rtmgr.SubscriptionList, xappSubData *models.XappSubscriptionData) bool {
        rtmgr.Logger.Debug("Deleteing the subscription from the subscriptions list")
-       var present bool = false
-       sub := rtmgr.Subscription{SubID:*xappSubData.SubscriptionID, Fqdn:*xappSubData.Address, Port:*xappSubData.Port,}
+       var present = false
+       sub := rtmgr.Subscription{SubID: *xappSubData.SubscriptionID, Fqdn: *xappSubData.Address, Port: *xappSubData.Port}
        for i, elem := range *subs {
                if elem == sub {
                        present = true
        for i, elem := range *subs {
                if elem == sub {
                        present = true