Refactor OperationsContent
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RootResourceDiscoveryServiceImpl.java
1 /*
2  * Copyright (c) 2020 ZTE Corp. and others.  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.rests.services.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import javax.ws.rs.GET;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.Response;
17 import javax.ws.rs.core.Response.Status;
18 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
19
20 /**
21  * Controller for determining the {@code Root Resource} of the RESTCONF API. This interface serves up a
22  * {@code host-meta} document as defined in
23  * <a href="https://tools.ietf.org/html/rfc8040#section-3">RFC6415 section 3</a>.
24  */
25 // FIXME: this really should be the endpoint's job to aggregate these. Once JAX-RS (or any other wiring) can provide it,
26 //        integrate with that framework, so we co-exist with others.
27 @Path("/")
28 public final class RootResourceDiscoveryServiceImpl {
29     private final String restconfRoot;
30
31     public RootResourceDiscoveryServiceImpl(final String restconfRoot) {
32         this.restconfRoot = requireNonNull(restconfRoot);
33     }
34
35     /**
36      * Root Resource Discovery as an XRD.
37      *
38      * @see <a href="https://tools.ietf.org/html/rfc8040#section-3.1">RFC8040, section 3.1</a>
39      */
40     @GET
41     @Path("/host-meta")
42     @Produces(MediaTypes.APPLICATION_XRD_XML)
43     public Response readXrdData() {
44         return Response.status(Status.OK)
45             .entity("<?xml version='1.0' encoding='UTF-8'?>\n"
46                 + "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>\n"
47                 + "  <Link rel='restconf' href='/" + restconfRoot + "'/>\n"
48                 + "</XRD>")
49             .build();
50     }
51
52     /**
53      * Root Resource Discovery as a JRD.
54      *
55      *  @see <a href="https://tools.ietf.org/html/rfc6415#appendix-A">RFC6415, appendix A</a>
56      */
57     @GET
58     @Path("/host-meta.json")
59     @Produces(MediaType.APPLICATION_JSON)
60     public Response readJsonData() {
61         return Response.status(Status.OK)
62             .entity("{\n"
63                 + "  \"links\" : {\n"
64                 + "    \"rel\" : \"restconf\",\n"
65                 + "    \"href\" : \"/" + restconfRoot + "\"\n"
66                 + "  }\n"
67                 + "}")
68             .build();
69     }
70 }
71