--- /dev/null
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 Nordix Foundation
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.oransc.policyagent.tasks;
+
+import org.oransc.policyagent.clients.A1Client;
+import org.oransc.policyagent.repository.Policies;
+import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
+import org.oransc.policyagent.repository.Ric.RicState;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+/**
+ * Regularly checks the exisiting rics towards the local repository to keep it consistent.
+ */
+@Component
+@EnableScheduling
+public class RepositorySupervision {
+ private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
+
+ private final Rics rics;
+ private final Policies policies;
+ private final A1Client a1Client;
+
+ @Autowired
+ public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client) {
+ this.rics = rics;
+ this.policies = policies;
+ this.a1Client = a1Client;
+ }
+
+ /**
+ * Regularly contacts all Rics to check if they are alive.
+ */
+ @Scheduled(fixedRate = 1000 * 60)
+ public void checkAllRics() {
+ logger.debug("Checking Rics starting");
+ createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
+
+ }
+
+ private Flux<Ric> createTask() {
+ return Flux.fromIterable(rics.getRics()) //
+ .groupBy(ric -> ric.state()) //
+ .flatMap(fluxGroup -> handleGroup(fluxGroup.key(), fluxGroup));
+ }
+
+ private Flux<Ric> handleGroup(Ric.RicState key, Flux<Ric> fluxGroup) {
+ logger.debug("Handling group {}", key);
+ switch (key) {
+ case ACTIVE:
+ return fluxGroup.flatMap(this::checkActive);
+
+ case NOT_REACHABLE:
+ return fluxGroup.flatMap(this::checkNotReachable);
+
+ default:
+ // If not initiated, leave it to the StartupService.
+ return Flux.empty();
+ }
+ }
+
+ private Mono<Ric> checkActive(Ric ric) {
+ logger.debug("Handling active ric {}", ric);
+ a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
+ .filter(policyId -> !policies.containsPolicy(policyId)) //
+ .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
+ .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
+ .subscribe();
+ return Mono.just(ric);
+ }
+
+ private Mono<Ric> checkNotReachable(Ric ric) {
+ logger.debug("Handling not reachable ric {}", ric);
+ a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
+ .filter(policyId -> !policies.containsPolicy(policyId)) //
+ .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
+ .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
+ .subscribe(null, null, () -> ric.setState(RicState.ACTIVE));
+ return Mono.just(ric);
+ }
+
+ private void onRicChecked(Ric ric) {
+ logger.info("Ric: " + ric.name() + " checked");
+ }
+
+ private void onError(Throwable t) {
+ logger.error("Rics supervision failed", t);
+ }
+
+ private void onComplete() {
+ logger.debug("Checking Rics completed");
+ }
+
+}
--- /dev/null
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 Nordix Foundation
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.oransc.policyagent.tasks;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.awaitility.Awaitility.await;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.Vector;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.oransc.policyagent.clients.A1Client;
+import org.oransc.policyagent.configuration.ImmutableRicConfig;
+import org.oransc.policyagent.repository.ImmutablePolicy;
+import org.oransc.policyagent.repository.ImmutablePolicyType;
+import org.oransc.policyagent.repository.Policies;
+import org.oransc.policyagent.repository.Policy;
+import org.oransc.policyagent.repository.PolicyType;
+import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
+import org.oransc.policyagent.repository.Ric.RicState;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@ExtendWith(MockitoExtension.class)
+@RunWith(MockitoJUnitRunner.class)
+public class RepositorySupervisionTest {
+ @Mock
+ A1Client a1ClientMock;
+
+ @Test
+ public void test() {
+ Ric ric1 = new Ric(ImmutableRicConfig.builder() //
+ .name("ric1") //
+ .baseUrl("baseUrl1") //
+ .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
+ .build());
+ ric1.setState(Ric.RicState.ACTIVE);
+ Ric ric2 = new Ric(ImmutableRicConfig.builder() //
+ .name("ric2") //
+ .baseUrl("baseUrl2") //
+ .managedElementIds(new Vector<String>(Arrays.asList("kista_3", "kista_4"))) //
+ .build());
+ ric2.setState(Ric.RicState.NOT_REACHABLE);
+ Ric ric3 = new Ric(ImmutableRicConfig.builder() //
+ .name("ric3") //
+ .baseUrl("baseUrl3") //
+ .managedElementIds(new Vector<String>(Arrays.asList("kista_5"))) //
+ .build());
+ Rics rics = new Rics();
+ rics.put(ric1);
+ rics.put(ric2);
+ rics.put(ric3);
+
+ PolicyType policyType = ImmutablePolicyType.builder() //
+ .name("type") //
+ .build();
+ Policy policy1 = ImmutablePolicy.builder() //
+ .id("policyId1") //
+ .json("") //
+ .ownerServiceName("service") //
+ .ric(ric1) //
+ .type(policyType) //
+ .lastModified("now") //
+ .build();
+ Policies policies = new Policies();
+ policies.put(policy1);
+
+ RepositorySupervision supervisorUnderTest = new RepositorySupervision(rics, policies, a1ClientMock);
+
+ when(a1ClientMock.getPolicyIdentities(anyString())).thenReturn(Flux.just("policyId1", "policyId2"));
+ when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
+
+ supervisorUnderTest.checkAllRics();
+
+ await().untilAsserted(() -> RicState.ACTIVE.equals(rics.getRic("ric2").state()));
+
+ verify(a1ClientMock).getPolicyIdentities("baseUrl1");
+ verify(a1ClientMock).deletePolicy("baseUrl1", "policyId2");
+ verify(a1ClientMock).getPolicyIdentities("baseUrl2");
+ verify(a1ClientMock).deletePolicy("baseUrl2", "policyId2");
+ verifyNoMoreInteractions(a1ClientMock);
+ }
+}