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