2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
28 log "github.com/sirupsen/logrus"
30 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
33 const registerTypePath = "/data-producer/v1/info-types/"
34 const registerProducerPath = "/data-producer/v1/info-producers/"
36 type TypeDefinition struct {
37 Identity string `json:"id"`
38 DMaaPTopicURL string `json:"dmaapTopicUrl"`
39 KafkaInputTopic string `json:"kafkaInputTopic"`
40 TypeSchema interface{}
43 func (td TypeDefinition) IsKafkaType() bool {
44 return td.KafkaInputTopic != ""
47 func (td TypeDefinition) IsDMaaPType() bool {
48 return td.DMaaPTopicURL != ""
51 type ProducerRegistrationInfo struct {
52 InfoProducerSupervisionCallbackUrl string `json:"info_producer_supervision_callback_url"`
53 SupportedInfoTypes []string `json:"supported_info_types"`
54 InfoJobCallbackUrl string `json:"info_job_callback_url"`
57 type Registrator interface {
58 RegisterTypes(types []TypeDefinition) error
59 RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo)
62 type RegistratorImpl struct {
63 infoCoordinatorAddress string
64 httpClient restclient.HTTPClient
67 func NewRegistratorImpl(infoCoordAddr string, client restclient.HTTPClient) *RegistratorImpl {
68 return &RegistratorImpl{
69 infoCoordinatorAddress: infoCoordAddr,
74 func (r RegistratorImpl) RegisterTypes(jobTypes []TypeDefinition) error {
75 for _, jobType := range jobTypes {
76 body := fmt.Sprintf(`{"info_job_data_schema": %v}`, jobType.TypeSchema)
77 if error := restclient.Put(r.infoCoordinatorAddress+registerTypePath+url.PathEscape(jobType.Identity), []byte(body), r.httpClient); error != nil {
80 log.Debugf("Registered type: %v", jobType)
85 func (r RegistratorImpl) RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo) error {
86 if body, marshalErr := json.Marshal(producerInfo); marshalErr == nil {
87 if putErr := restclient.Put(r.infoCoordinatorAddress+registerProducerPath+url.PathEscape(producerId), []byte(body), r.httpClient); putErr != nil {
90 log.Debugf("Registered producer: %v", producerId)