Reduce exception guard
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / AbstractRestconfApplication.java
1 /*
2  * Copyright (c) 2020 Lumina Networks, Inc. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.util.List;
14 import java.util.Set;
15 import javax.ws.rs.core.Application;
16 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
17 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
18 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyWriter;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyReader;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.XmlNormalizedNodeBodyWriter;
22 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YangSchemaExportBodyWriter;
23 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.YinSchemaExportBodyWriter;
24 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors.RestconfDocumentedExceptionMapper;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonPatchBodyReader;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.JsonPatchStatusBodyWriter;
27 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.XmlPatchBodyReader;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.XmlPatchStatusBodyWriter;
29
30 /**
31  * Abstract Restconf Application.
32  */
33 abstract class AbstractRestconfApplication extends Application {
34     private final SchemaContextHandler schemaContextHandler;
35     private final DOMMountPointService mountPointService;
36     private final List<Object> services;
37
38     AbstractRestconfApplication(final SchemaContextHandler schemaContextHandler,
39             final DOMMountPointService mountPointService, final List<Object> services) {
40         this.schemaContextHandler = requireNonNull(schemaContextHandler);
41         this.mountPointService = requireNonNull(mountPointService);
42         this.services = requireNonNull(services);
43     }
44
45     @Override
46     public final Set<Class<?>> getClasses() {
47         return Set.of(
48             JsonNormalizedNodeBodyWriter.class, XmlNormalizedNodeBodyWriter.class,
49             YinSchemaExportBodyWriter.class, YangSchemaExportBodyWriter.class,
50             JsonPatchStatusBodyWriter.class, XmlPatchStatusBodyWriter.class);
51     }
52
53     @Override
54     public final Set<Object> getSingletons() {
55         return ImmutableSet.<Object>builderWithExpectedSize(services.size() + 5)
56             .addAll(services)
57             .add(new JsonNormalizedNodeBodyReader(schemaContextHandler, mountPointService))
58             .add(new JsonPatchBodyReader(schemaContextHandler, mountPointService))
59             .add(new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService))
60             .add(new XmlPatchBodyReader(schemaContextHandler, mountPointService))
61             .add(new RestconfDocumentedExceptionMapper(schemaContextHandler))
62             .build();
63     }
64 }