Merge "Test updates for ECS, PMS and SDNC"
[nonrtric.git] / dmaap-mediator-producer / internal / config / registrator.go
1 // -
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
21 package config
22
23 import (
24         "encoding/json"
25         "fmt"
26         "net/url"
27
28         log "github.com/sirupsen/logrus"
29
30         "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
31         "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient"
32 )
33
34 const registerTypePath = "/data-producer/v1/info-types/"
35 const registerProducerPath = "/data-producer/v1/info-producers/"
36
37 type Registrator interface {
38         RegisterTypes(types []*jobs.Type) error
39         RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo)
40 }
41
42 type RegistratorImpl struct {
43         infoCoordinatorAddress string
44 }
45
46 func NewRegistratorImpl(infoCoordAddr string) *RegistratorImpl {
47         return &RegistratorImpl{
48                 infoCoordinatorAddress: infoCoordAddr,
49         }
50 }
51
52 func (r RegistratorImpl) RegisterTypes(jobTypes []*jobs.Type) error {
53         for _, jobType := range jobTypes {
54                 body := fmt.Sprintf(`{"info_job_data_schema": %v}`, jobType.Schema)
55                 if error := restclient.Put(r.infoCoordinatorAddress+registerTypePath+url.PathEscape(jobType.TypeId), []byte(body)); error != nil {
56                         return error
57                 }
58                 log.Debugf("Registered type: %v", jobType)
59         }
60         return nil
61 }
62
63 func (r RegistratorImpl) RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo) error {
64         if body, marshalErr := json.Marshal(producerInfo); marshalErr == nil {
65                 if putErr := restclient.Put(r.infoCoordinatorAddress+registerProducerPath+url.PathEscape(producerId), []byte(body)); putErr != nil {
66                         return putErr
67                 }
68                 log.Debugf("Registered producer: %v", producerId)
69                 return nil
70         } else {
71                 return marshalErr
72         }
73 }