From 9ed2de339dc86499b5497cfb905fbcc8974d7e16 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Fri, 3 Jan 2020 17:12:23 +0100 Subject: [PATCH] Add first simple version of repository supervision Change-Id: Ic0dedd679e0737d5c4635c4959cffe8bd88e5695 Issue-ID: NONRTRIC-81 Signed-off-by: elinuxhenrik --- .../oransc/policyagent/repository/Policies.java | 4 + .../policyagent/tasks/RepositorySupervision.java | 119 +++++++++++++++++++++ .../tasks/RepositorySupervisionTest.java | 110 +++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 policy-agent/src/main/java/org/oransc/policyagent/tasks/RepositorySupervision.java create mode 100644 policy-agent/src/test/java/org/oransc/policyagent/tasks/RepositorySupervisionTest.java diff --git a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java index 7b7b17c3..ffbeb162 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/repository/Policies.java @@ -70,6 +70,10 @@ public class Policies { return map.values(); } + public synchronized boolean containsPolicy(String id) { + return policiesId.containsKey(id); + } + public synchronized Policy getPolicy(String id) throws ServiceException { Policy p = policiesId.get(id); if (p == null) { diff --git a/policy-agent/src/main/java/org/oransc/policyagent/tasks/RepositorySupervision.java b/policy-agent/src/main/java/org/oransc/policyagent/tasks/RepositorySupervision.java new file mode 100644 index 00000000..d68c1236 --- /dev/null +++ b/policy-agent/src/main/java/org/oransc/policyagent/tasks/RepositorySupervision.java @@ -0,0 +1,119 @@ +/*- + * ========================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 createTask() { + return Flux.fromIterable(rics.getRics()) // + .groupBy(ric -> ric.state()) // + .flatMap(fluxGroup -> handleGroup(fluxGroup.key(), fluxGroup)); + } + + private Flux handleGroup(Ric.RicState key, Flux 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 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 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"); + } + +} diff --git a/policy-agent/src/test/java/org/oransc/policyagent/tasks/RepositorySupervisionTest.java b/policy-agent/src/test/java/org/oransc/policyagent/tasks/RepositorySupervisionTest.java new file mode 100644 index 00000000..268c7a4e --- /dev/null +++ b/policy-agent/src/test/java/org/oransc/policyagent/tasks/RepositorySupervisionTest.java @@ -0,0 +1,110 @@ +/*- + * ========================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(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(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(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); + } +} -- 2.16.6