blob: 83ed43f2c375c45a7724a385dcbe5a7fd8212ece [file] [log] [blame]
elinuxhenrikcce95ff2021-09-05 17:27:02 +02001// -
2// ========================LICENSE_START=================================
3// O-RAN-SC
4// %%
5// Copyright (C) 2021: Nordix Foundation
6// %%
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
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
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===================================
19//
20
21package config
22
23import (
elinuxhenrika77cd652021-09-06 10:56:21 +020024 "encoding/json"
elinuxhenrikcce95ff2021-09-05 17:27:02 +020025 "fmt"
26 "net/url"
27
28 log "github.com/sirupsen/logrus"
29
elinuxhenrikcce95ff2021-09-05 17:27:02 +020030 "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
31)
32
33const registerTypePath = "/data-producer/v1/info-types/"
elinuxhenrika77cd652021-09-06 10:56:21 +020034const registerProducerPath = "/data-producer/v1/info-producers/"
elinuxhenrik382870d2021-09-23 11:09:09 +020035const typeSchema = `{"type": "object","properties": {},"additionalProperties": false}`
elinuxhenrikcce95ff2021-09-05 17:27:02 +020036
elinuxhenrik65a53d22021-09-29 15:41:26 +020037type TypeDefinition struct {
38 Id string `json:"id"`
39 DmaapTopicURL string `json:"dmaapTopicUrl"`
40}
41
42type ProducerRegistrationInfo struct {
43 InfoProducerSupervisionCallbackUrl string `json:"info_producer_supervision_callback_url"`
44 SupportedInfoTypes []string `json:"supported_info_types"`
45 InfoJobCallbackUrl string `json:"info_job_callback_url"`
46}
47
elinuxhenrikcce95ff2021-09-05 17:27:02 +020048type Registrator interface {
elinuxhenrik65a53d22021-09-29 15:41:26 +020049 RegisterTypes(types []TypeDefinition) error
elinuxhenrika77cd652021-09-06 10:56:21 +020050 RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo)
elinuxhenrikcce95ff2021-09-05 17:27:02 +020051}
52
53type RegistratorImpl struct {
54 infoCoordinatorAddress string
elinuxhenrik65a53d22021-09-29 15:41:26 +020055 httpClient restclient.HTTPClient
elinuxhenrikcce95ff2021-09-05 17:27:02 +020056}
57
elinuxhenrik65a53d22021-09-29 15:41:26 +020058func NewRegistratorImpl(infoCoordAddr string, client restclient.HTTPClient) *RegistratorImpl {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020059 return &RegistratorImpl{
60 infoCoordinatorAddress: infoCoordAddr,
elinuxhenrik65a53d22021-09-29 15:41:26 +020061 httpClient: client,
elinuxhenrikcce95ff2021-09-05 17:27:02 +020062 }
63}
64
elinuxhenrik65a53d22021-09-29 15:41:26 +020065func (r RegistratorImpl) RegisterTypes(jobTypes []TypeDefinition) error {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020066 for _, jobType := range jobTypes {
elinuxhenrik382870d2021-09-23 11:09:09 +020067 body := fmt.Sprintf(`{"info_job_data_schema": %v}`, typeSchema)
elinuxhenrik65a53d22021-09-29 15:41:26 +020068 if error := restclient.Put(r.infoCoordinatorAddress+registerTypePath+url.PathEscape(jobType.Id), []byte(body), r.httpClient); error != nil {
elinuxhenrikcce95ff2021-09-05 17:27:02 +020069 return error
70 }
71 log.Debugf("Registered type: %v", jobType)
72 }
73 return nil
74}
elinuxhenrika77cd652021-09-06 10:56:21 +020075
76func (r RegistratorImpl) RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo) error {
77 if body, marshalErr := json.Marshal(producerInfo); marshalErr == nil {
elinuxhenrik65a53d22021-09-29 15:41:26 +020078 if putErr := restclient.Put(r.infoCoordinatorAddress+registerProducerPath+url.PathEscape(producerId), []byte(body), r.httpClient); putErr != nil {
elinuxhenrika77cd652021-09-06 10:56:21 +020079 return putErr
80 }
81 log.Debugf("Registered producer: %v", producerId)
82 return nil
83 } else {
84 return marshalErr
85 }
86}