Commit b6f5b0b5 authored by Yann Garcia's avatar Yann Garcia
Browse files

Bug fixed in meep-sss when URL is used instead of host/port addressing

parent 4251290b
Loading
Loading
Loading
Loading
+53 −32
Original line number Diff line number Diff line
@@ -310,17 +310,41 @@ func (tm *IotMgr) RegisterIotPlatformInfo(iotPlatformInfo IotPlatformInfo) (err

	if iotPlatformInfo.Enabled {
		iotPlatformInfo.oneM2M = nil
		//		if len(iotPlatformInfo.UserTransportInfo) == 0 || iotPlatformInfo.UserTransportInfo[0].Endpoint == nil || len(iotPlatformInfo.UserTransportInfo[0].Endpoint.Addresses) == 0 {
		if len(iotPlatformInfo.CustomServicesTransportInfo) == 0 || iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint == nil || len(iotPlatformInfo.UserTransportInfo[0].Endpoint.Addresses) == 0 {
			log.Warn("RegisterIotPlatformInfo: Cannot use provided CustomServicesTransportInfo")
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo: ", iotPlatformInfo.CustomServicesTransportInfo)
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo[0].Endpoint: ", iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint)
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo[0].Endpoint.Addresses: ", iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses)
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo[0].Endpoint.Uris: ", iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Uris)
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo[0].Protocol: ", iotPlatformInfo.CustomServicesTransportInfo[0].Protocol)
		log.Debug("RegisterIotPlatformInfo: CustomServicesTransportInfo[0].Name: ", iotPlatformInfo.CustomServicesTransportInfo[0].Name)
		if len(iotPlatformInfo.CustomServicesTransportInfo) == 0 ||
			iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint == nil ||
			len(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses) == 0 &&
				len(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Uris) == 0 {
			log.Error("RegisterIotPlatformInfo: Cannot use provided CustomServicesTransportInfo")
		} else {
			// FIXME FSCOM How to get the CSE_ID. For the time being, iotPlatformInfo.UserTransportInfo[0].Name is the CSE-resourceID
			// TODO FSCOM Add notification support?
			pltf, err := sssmgr.NewSssMgr(tm.name, tm.namespace, iotPlatformInfo.CustomServicesTransportInfo[0].Protocol, iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses[0].Host, int(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses[0].Port), iotPlatformInfo.IotPlatformId, iotPlatformInfo.CustomServicesTransportInfo[0].Name, nil, nil, nil)
			var pltf *sssmgr.SssMgr = nil
			var err error
			if len(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses) != 0 {
				log.Debug("RegisterIotPlatformInfo: Using CustomServicesTransportInfo[0].Endpoint.Addresses: ", iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses)
				pltf, err = sssmgr.NewSssMgr(tm.name, tm.namespace, iotPlatformInfo.CustomServicesTransportInfo[0].Protocol, iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses[0].Host, int(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Addresses[0].Port), iotPlatformInfo.IotPlatformId, iotPlatformInfo.CustomServicesTransportInfo[0].Name, nil, nil, nil)
				if err != nil {
					log.Error("RegisterIotPlatformInfo: ", err)
					iotPlatformInfo.oneM2M = nil
					return err
				}
			} else if len(iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Uris) != 0 {
				log.Debug("RegisterIotPlatformInfo: Using CustomServicesTransportInfo[0].Endpoint.Uris: ", iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Uris)
				pltf, err = sssmgr.NewSssMgr_with_uri(tm.name, tm.namespace, iotPlatformInfo.CustomServicesTransportInfo[0].Protocol, iotPlatformInfo.CustomServicesTransportInfo[0].Endpoint.Uris[0], iotPlatformInfo.IotPlatformId, iotPlatformInfo.CustomServicesTransportInfo[0].Name, nil, nil, nil)
				if err != nil {
					log.Error("RegisterIotPlatformInfo: ", err)
					iotPlatformInfo.oneM2M = nil
					return err
				}
			} else {
				log.Error("RegisterIotPlatformInfo: Cannot use provided CustomServicesTransportInfo")
				return errors.New("Cannot use provided CustomServicesTransportInfo")
			}
			// No error, create the IoT platform
			log.Info("RegisterIotPlatformInfo: IoT pltf created")
			iotPlatformInfo.oneM2M = pltf
			// Create the an AE for this ETSI MEC platform
@@ -337,19 +361,16 @@ func (tm *IotMgr) RegisterIotPlatformInfo(iotPlatformInfo IotPlatformInfo) (err
				iotPlatformInfo.oneM2M = nil
				return err
			} else {
				registeredIotPlatformsMap[iotPlatformInfo.IotPlatformId] = iotPlatformInfo
				registeredIotPlatformsAEMap[iotPlatformInfo.IotPlatformId] = ae
				if tm.refreshTicker == nil {
					log.Info("RegisterIotPlatformInfo: Start RefreshTicker")
					tm.startRefreshTicker()
				}
				log.Info("RegisterIotPlatformInfo: iotPlatformId: ", registeredIotPlatformsMap[iotPlatformInfo.IotPlatformId])
			}
		}
	}
		registeredIotPlatformsMap[iotPlatformInfo.IotPlatformId] = iotPlatformInfo

		log.Info("RegisterIotPlatformInfo: iotPlatformId: ", registeredIotPlatformsMap[iotPlatformInfo.IotPlatformId])

	} // else, Skip disabled platform

	if profiling {
		now := time.Now()
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ type SssMgrBindingProtocol interface {
type SssMgrBindingProtocolContext struct {
	host    string
	port    int
	path    string
	name    string
	to      string
	from    string
+31 −21
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package sssmgr

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
@@ -31,19 +32,19 @@ func NewSssMgrHttp() (http_mgr *SssMgrHttp) {
}

func (http_mgr *SssMgrHttp) init(tm *SssMgr, notify func(map[string]interface{})) (err error) {
	log.Info(">>> init: cse_name: ", tm.cse_name, " host_id: ", tm.hostId)
	log.Info(">>> http.init: cse_name: ", tm.cse_name, " host_id: ", tm.hostId, " port: ", tm.port)

	http_mgr.notify = notify

	go func() {
		log.Info("Init: Starting OneM2M Notification server on /" + tm.cse_name + ", port: " + tm.hostId)
		http.HandleFunc("/"+tm.cse_name, http_mgr.handleRoot)
		//http.HandleFunc("/"+tm.cse_name, http_mgr.handleRoot)
		//err := http.ListenAndServe(":31122", nil)
		err := http.ListenAndServe(":"+tm.hostId, nil)
		if err != nil {
			log.Error(err.Error())
			return
		}
		// err := http.ListenAndServe(":"+tm.hostId, nil)
		// if err != nil {
		// 	log.Error(err.Error())
		// 	return
		// }
		log.Info("<<< Init: Terminating OneM2M Notification server")
	}()

@@ -114,8 +115,13 @@ func (http_mgr *SssMgrHttp) send(p_ctx SssMgrBindingProtocolContext) (err error,
		s = s + ";ty=" + strconv.Itoa(p_ctx.ty)
	}
	headers["Content-Type"] = []string{s}
	// Build the url
	url := "http://" + p_ctx.host + ":" + strconv.Itoa(p_ctx.port) + "/" + p_ctx.to
	// Build the URL
	var url string
	if p_ctx.path != "" {
		url = "https://" + p_ctx.host + ":" + strconv.Itoa(p_ctx.port) + "/" + p_ctx.path + "/" + p_ctx.to
	} else {
		url = "https://" + p_ctx.host + ":" + strconv.Itoa(p_ctx.port) + "/" + p_ctx.to
	}
	// Set the method
	method := ""
	if p_ctx.op == 1 {
@@ -167,9 +173,10 @@ func (http_mgr *SssMgrHttp) send(p_ctx SssMgrBindingProtocolContext) (err error,

	return nil, resp
}

func sendRequest(method string, url string, headers http.Header, body io.Reader, vars map[string]string, query map[string]string, code int) ([]byte, error) {
	//log.Debug(">>> sendRequest: url: ", url)
	//log.Debug(">>> sendRequest: headers: ", headers)
	log.Debug(">>> sendRequest: url: ", url)
	log.Debug(">>> sendRequest: headers: ", headers)

	req, err := http.NewRequest(method, url, body)
	if err != nil || req == nil {
@@ -188,23 +195,26 @@ func sendRequest(method string, url string, headers http.Header, body io.Reader,
	req.Header = headers
	req.Close = true

	//log.Debug("sendRequest: req: ", req)
	rr, err := http.DefaultClient.Do(req)
	log.Debug("sendRequest: req: ", req)
	// Skip TLS verification for development/testing
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	rr, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	// Check the status code is what we expect.
	//log.Debug("sendRequest: rr: ", rr)
	log.Debug("sendRequest: rr: ", rr)
	log.Debug("sendRequest: rr.Body: ", rr.Body)
	if status := rr.StatusCode; status != code {
		s := fmt.Sprintf("Wrong status code - got %v want %v", status, code)
		responseData, _ := ioutil.ReadAll(rr.Body)
		log.Debug("sendRequest: responseData: ", string(responseData))
		s := fmt.Sprintf("Wrong status code - got %v want %v:\r\n%s", status, code, string(responseData))
		return nil, errors.New(s)
	}
	responseData, err := ioutil.ReadAll(rr.Body)
	if err != nil {
		return nil, err
	}
	//log.Debug("sendRequest: responseData: ", responseData)

	return responseData, nil
	return ioutil.ReadAll(rr.Body)
}
+100 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package sssmgr
import (
	"encoding/json"
	"errors"
	"net/url"
	"reflect"
	"strconv"
	"strings"
@@ -38,6 +39,7 @@ type SssMgr struct {
	protocol             SssMgrBindingProtocol
	host                 string
	port                 int
	path                 string
	cse_name             string
	hostId               string
	mutex                sync.Mutex
@@ -107,6 +109,15 @@ func NewSssMgr(name string, namespace string, bindingProtocol string, host strin
	if name == "" {
		err = errors.New("Missing connector name")
		return nil, err
	} else if host == "" {
		err = errors.New("Missing host address")
		return nil, err
	} else if hostId == "" {
		err = errors.New("Missing host identifier")
		return nil, err
	} else if cse_name == "" {
		err = errors.New("Missing cse_name")
		return nil, err
	}

	// Create new Traffic Manager
@@ -144,13 +155,91 @@ func NewSssMgr(name string, namespace string, bindingProtocol string, host strin
		log.Error(err.Error())
		return nil, err
	}
	if hostId == "" {
		err := errors.New("hostId not set")

	err = tm.protocol.init(tm, tm.notify)
	if err != nil {
		log.Error(err.Error())
		return nil, err
	}

	tm.sss_discovery_notify = sss_discovery_notify
	tm.sss_status_notify = sss_status_notify
	tm.sss_data_notify = sss_data_notify

	tm.init()

	return tm, nil
}

func NewSssMgr_with_uri(name string, namespace string, bindingProtocol string, uri string, hostId string, cse_name string, sss_discovery_notify func(map[string]interface{}), sss_status_notify func(map[string]interface{}), sss_data_notify func(map[string]interface{})) (tm *SssMgr, err error) {
	if name == "" {
		err = errors.New("Missing connector name")
		return nil, err
	} else if uri == "" {
		err = errors.New("Missing URI")
		return nil, err
	} else if hostId == "" {
		err = errors.New("Missing host identifier")
		return nil, err
	} else if cse_name == "" {
		err = errors.New("Missing cse_name")
		return nil, err
	}

	u, err := url.Parse(uri)
	if err != nil {
		return nil, err
	}
	log.Debug("NewSssMgr_with_uri: uri: ", u)
	log.Debug("NewSssMgr_with_uri: host: ", u.Host)
	log.Debug("NewSssMgr_with_uri: port: ", u.Port())
	log.Debug("NewSssMgr_with_uri: path: ", u.Path)
	log.Debug("NewSssMgr_with_uri: query: ", u.Query())
	log.Debug("NewSssMgr_with_uri: scheme: ", u.Scheme)
	log.Debug("NewSssMgr_with_uri: raw: ", u.RawQuery)

	// Create new Traffic Manager
	tm = new(SssMgr)
	tm.name = name
	if namespace != "" {
		tm.namespace = namespace
	} else {
		tm.namespace = "default"
	}

	tm.bindingProtocol = bindingProtocol
	tm.host = u.Hostname()
	tm.port, err = strconv.Atoi(u.Port())
	tm.path = u.Path
	if err != nil {
		tm.port = 443
		log.Debug("NewSssMgr_with_uri: Force port to ", tm.port)
	}
	tm.path = u.Path
	tm.cse_name = cse_name
	tm.hostId = hostId
	tm.protocol = nil
	if tm.bindingProtocol == "MQTT" {
		if tm.host == "" {
			err := errors.New("Host not set for MQTTP protocol")
			log.Error(err.Error())
			return nil, err
		}
	if cse_name == "" {
		err := errors.New("cse_name not set")
		if tm.port == 0 {
			tm.port = 1883
		}
		tm.protocol = NewSssMgrMqtt()
	} else if tm.bindingProtocol == "REST_HTTP" {
		if tm.port == 0 {
			if u.Scheme == "https" {
				tm.port = 443
			} else {
				tm.port = 80
			}
		}
		tm.protocol = NewSssMgrHttp()
	} else {
		err := errors.New("Binding protocol not set")
		log.Error(err.Error())
		return nil, err
	}
@@ -348,6 +437,7 @@ func (tm *SssMgr) populateSensors(iotPlatformInfo IotPlatformInfo, type_ string)
	var ctx = SssMgrBindingProtocolContext{
		host: iotPlatformInfo.Address,
		port: iotPlatformInfo.Port,
		path: tm.path,
		name: iotPlatformInfo.Name,
		to:   iotPlatformInfo.Name,
		from: "Admin", // FIXME FSCOM How to get it
@@ -564,6 +654,7 @@ func (tm *SssMgr) OneM2M_create(sensor SensorDiscoveryInfo, path string) (sensor
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[sensor.IotPlatformId].Address,
		port: registeredIotPlatformsMap[sensor.IotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[sensor.IotPlatformId].Name,
		to:   registeredIotPlatformsMap[sensor.IotPlatformId].Name,
		from: sensor.IotPlatformId,
@@ -652,6 +743,7 @@ func (tm *SssMgr) OneM2M_discovery(type_ string, iotPlatformId string) (sensorRe
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[iotPlatformId].Address,
		port: registeredIotPlatformsMap[iotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[iotPlatformId].Name,
		to:   registeredIotPlatformsMap[iotPlatformId].Name,
		from: "Admin", // FIXME FSCOM How to get it
@@ -743,6 +835,7 @@ func (tm *SssMgr) OneM2M_get(path string, iotPlatformId string) (sensorResp Sens
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[iotPlatformId].Address,
		port: registeredIotPlatformsMap[iotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[iotPlatformId].Name,
		to:   path,
		from: "Admin", // FIXME FSCOM How to get it
@@ -805,6 +898,7 @@ func (tm *SssMgr) OneM2M_subscribe(iotPlatformId string, path string) (subscript
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[iotPlatformId].Address,
		port: registeredIotPlatformsMap[iotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[iotPlatformId].Name,
		to:   path,
		from: registeredIotPlatformsMap[iotPlatformId].IotPlatformId,
@@ -899,6 +993,7 @@ func (tm *SssMgr) OneM2M_delete(sensor SensorDiscoveryInfo) (err error) {
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[sensor.IotPlatformId].Address,
		port: registeredIotPlatformsMap[sensor.IotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[sensor.IotPlatformId].Name,
		to:   sensor.SensorIdentifier,
		from: sensor.IotPlatformId,
@@ -949,6 +1044,7 @@ func (tm *SssMgr) OneM2M_delete_subscription(subId string) (err error) {
	var ctx = SssMgrBindingProtocolContext{
		host: registeredIotPlatformsMap[subscriptionListPerSubId[subId].IotPlatformId].Address,
		port: registeredIotPlatformsMap[subscriptionListPerSubId[subId].IotPlatformId].Port,
		path: tm.path,
		name: registeredIotPlatformsMap[subscriptionListPerSubId[subId].IotPlatformId].Name,
		to:   subscriptionListPerSubId[subId].SensorIdentifier,
		from: subscriptionListPerSubId[subId].IotPlatformId,