NONRTRIC - Enrichment Coordinator Service, making type availability subscriptions...
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / r1consumer / ConsumerCallbacks.java
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 org.oransc.enrichment.controllers.r1consumer;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import org.oransc.enrichment.clients.AsyncRestClient;
27 import org.oransc.enrichment.clients.AsyncRestClientFactory;
28 import org.oransc.enrichment.configuration.ApplicationConfig;
29 import org.oransc.enrichment.repository.InfoType;
30 import org.oransc.enrichment.repository.InfoTypeSubscriptions;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import reactor.core.publisher.Mono;
34
35 /**
36  * Callbacks to the Consumer. Notifies consumer according to the API (which this
37  * class adapts to)
38  */
39 @Component
40 public class ConsumerCallbacks implements InfoTypeSubscriptions.ConsumerCallbackHandler {
41
42     private static Gson gson = new GsonBuilder().create();
43
44     private final AsyncRestClient restClient;
45
46     public static final String API_VERSION = "version_1";
47
48     public ConsumerCallbacks(@Autowired ApplicationConfig config,
49         @Autowired InfoTypeSubscriptions infoTypeSubscriptions) {
50         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config.getWebClientConfig());
51         this.restClient = restClientFactory.createRestClientNoHttpProxy("");
52         infoTypeSubscriptions.registerCallbackhandler(this, API_VERSION);
53     }
54
55     @Override
56     public Mono<String> notifyTypeRegistered(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
57         String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.REGISTERED);
58         return restClient.post(subscriptionInfo.getCallbackUrl(), body);
59     }
60
61     @Override
62     public Mono<String> notifyTypeRemoved(InfoType type, InfoTypeSubscriptions.SubscriptionInfo subscriptionInfo) {
63         String body = body(type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues.DEREGISTERED);
64         return restClient.post(subscriptionInfo.getCallbackUrl(), body);
65     }
66
67     private String body(InfoType type, ConsumerTypeRegistrationInfo.ConsumerTypeStatusValues status) {
68         ConsumerTypeRegistrationInfo info =
69             new ConsumerTypeRegistrationInfo(type.getJobDataSchema(), status, type.getId());
70         return gson.toJson(info);
71     }
72
73 }