From d43e7c5e0765d728a53854df63f6a57aff243203 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 10 Nov 2022 23:37:11 +0100 Subject: [PATCH] Turn SchemaContextHandler into a component This is an independent component now, convert it to OSGi DS. We are now free to further evolve it. Change-Id: I98103e657461504b88c3dd3b09433a77baf7c351 Signed-off-by: Robert Varga --- .../handlers/SchemaContextHandler.java | 30 +++++++++---------- .../OSGI-INF/blueprint/restconf-bp.xml | 4 --- .../handlers/SchemaContextHandlerTest.java | 15 ++++------ 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java index b8444132fd..2684032fc0 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandler.java @@ -17,7 +17,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import javax.inject.Singleton; @@ -67,6 +66,10 @@ import org.opendaylight.yangtools.yang.model.api.FeatureDefinition; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleLike; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,7 +80,8 @@ import org.slf4j.LoggerFactory; // should live in MD-SAL and be a dynamic store fragment. As a first step we should be turning this into a // completely standalone application. @Singleton -public class SchemaContextHandler implements EffectiveModelContextListener, AutoCloseable { +@Component(service = { }) +public final class SchemaContextHandler implements EffectiveModelContextListener, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class); @VisibleForTesting @@ -96,31 +100,25 @@ public class SchemaContextHandler implements EffectiveModelContextListener, Auto private static final NodeIdentifier MODULE_SCHEMA_NODEID = NodeIdentifier.create(QName.create(IetfYangLibrary.MODULE_QNAME, "schema").intern()); - private final AtomicInteger moduleSetId = new AtomicInteger(0); - + private final AtomicInteger moduleSetId = new AtomicInteger(); private final DOMDataBroker domDataBroker; - private final DOMSchemaService domSchemaService; - private Registration listenerRegistration; + private final Registration listenerRegistration; private volatile EffectiveModelContext schemaContext; @Inject - public SchemaContextHandler(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService) { + @Activate + public SchemaContextHandler(@Reference final DOMDataBroker domDataBroker, + @Reference final DOMSchemaService domSchemaService) { this.domDataBroker = requireNonNull(domDataBroker); - this.domSchemaService = requireNonNull(domSchemaService); - } - - @PostConstruct - public void init() { listenerRegistration = domSchemaService.registerSchemaContextListener(this); } - @Override @PreDestroy + @Deactivate + @Override public void close() { - if (listenerRegistration != null) { - listenerRegistration.close(); - } + listenerRegistration.close(); } @Override diff --git a/restconf/restconf-nb/src/main/resources/OSGI-INF/blueprint/restconf-bp.xml b/restconf/restconf-nb/src/main/resources/OSGI-INF/blueprint/restconf-bp.xml index 4089f6f904..adfcbf0165 100644 --- a/restconf/restconf-nb/src/main/resources/OSGI-INF/blueprint/restconf-bp.xml +++ b/restconf/restconf-nb/src/main/resources/OSGI-INF/blueprint/restconf-bp.xml @@ -58,10 +58,6 @@ - - - - diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java index 87c12f7c8d..094a947c39 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/handlers/SchemaContextHandlerTest.java @@ -12,6 +12,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -54,6 +55,8 @@ public class SchemaContextHandlerTest { @Mock private DOMSchemaService mockDOMSchemaService; + @Mock + private ListenerRegistration mockListenerReg; @BeforeClass public static void beforeClass() throws FileNotFoundException { @@ -73,7 +76,10 @@ public class SchemaContextHandlerTest { doReturn(wTx).when(dataBroker).newWriteOnlyTransaction(); doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit(); + doReturn(mockListenerReg).when(mockDOMSchemaService).registerSchemaContextListener(any()); + schemaContextHandler = new SchemaContextHandler(dataBroker, mockDOMSchemaService); + verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler); schemaContextHandler.onModelContextUpdated(SCHEMA_CONTEXT); } @@ -83,16 +89,7 @@ public class SchemaContextHandlerTest { */ @Test public void testInitAndClose() { - ListenerRegistration mockListenerReg = mock(ListenerRegistration.class); - doReturn(mockListenerReg).when(mockDOMSchemaService) - .registerSchemaContextListener(schemaContextHandler); - - schemaContextHandler.init(); - - verify(mockDOMSchemaService).registerSchemaContextListener(schemaContextHandler); - schemaContextHandler.close(); - verify(mockListenerReg).close(); } -- 2.36.6