elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 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 ( |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 24 | "encoding/json" |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 25 | "fmt" |
| 26 | "net/url" |
| 27 | |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 30 | "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" |
| 31 | ) |
| 32 | |
| 33 | const registerTypePath = "/data-producer/v1/info-types/" |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 34 | const registerProducerPath = "/data-producer/v1/info-producers/" |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 35 | const typeSchema = `{"type": "object","properties": {},"additionalProperties": false}` |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 36 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 37 | type TypeDefinition struct { |
| 38 | Id string `json:"id"` |
| 39 | DmaapTopicURL string `json:"dmaapTopicUrl"` |
| 40 | } |
| 41 | |
| 42 | type 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 | |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 48 | type Registrator interface { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 49 | RegisterTypes(types []TypeDefinition) error |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 50 | RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo) |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | type RegistratorImpl struct { |
| 54 | infoCoordinatorAddress string |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 55 | httpClient restclient.HTTPClient |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 56 | } |
| 57 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 58 | func NewRegistratorImpl(infoCoordAddr string, client restclient.HTTPClient) *RegistratorImpl { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 59 | return &RegistratorImpl{ |
| 60 | infoCoordinatorAddress: infoCoordAddr, |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 61 | httpClient: client, |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 65 | func (r RegistratorImpl) RegisterTypes(jobTypes []TypeDefinition) error { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 66 | for _, jobType := range jobTypes { |
elinuxhenrik | 382870d | 2021-09-23 11:09:09 +0200 | [diff] [blame] | 67 | body := fmt.Sprintf(`{"info_job_data_schema": %v}`, typeSchema) |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 68 | if error := restclient.Put(r.infoCoordinatorAddress+registerTypePath+url.PathEscape(jobType.Id), []byte(body), r.httpClient); error != nil { |
elinuxhenrik | cce95ff | 2021-09-05 17:27:02 +0200 | [diff] [blame] | 69 | return error |
| 70 | } |
| 71 | log.Debugf("Registered type: %v", jobType) |
| 72 | } |
| 73 | return nil |
| 74 | } |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 75 | |
| 76 | func (r RegistratorImpl) RegisterProducer(producerId string, producerInfo *ProducerRegistrationInfo) error { |
| 77 | if body, marshalErr := json.Marshal(producerInfo); marshalErr == nil { |
elinuxhenrik | 65a53d2 | 2021-09-29 15:41:26 +0200 | [diff] [blame] | 78 | if putErr := restclient.Put(r.infoCoordinatorAddress+registerProducerPath+url.PathEscape(producerId), []byte(body), r.httpClient); putErr != nil { |
elinuxhenrik | a77cd65 | 2021-09-06 10:56:21 +0200 | [diff] [blame] | 79 | return putErr |
| 80 | } |
| 81 | log.Debugf("Registered producer: %v", producerId) |
| 82 | return nil |
| 83 | } else { |
| 84 | return marshalErr |
| 85 | } |
| 86 | } |