fdbb1b2f0a425e6ad3f91f0b9af4e78ece49bce0
[netconf.git] /
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.netconf.sal.rest.doc.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
13 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
14
15 /**
16  * Base class for an RFC 8040 implementation.
17  *
18  * @author Thomas Pantelis
19  */
20 public abstract class BaseYangSwaggerGeneratorRFC8040 extends BaseYangSwaggerGenerator {
21     private final String basePath;
22
23     protected BaseYangSwaggerGeneratorRFC8040(final Optional<DOMSchemaService> schemaService) {
24         this(schemaService, "rests");
25     }
26
27     protected BaseYangSwaggerGeneratorRFC8040(final Optional<DOMSchemaService> schemaService, final String basePath) {
28         super(schemaService);
29         this.basePath = requireNonNull(basePath);
30     }
31
32     @Override
33     public String getResourcePath(final String resourceType, final String context) {
34         if (isData(resourceType)) {
35             return "/" + basePath + "/data" + context;
36         }
37         return "/" + basePath + "/operations" + context;
38     }
39
40     @Override
41     public String getResourcePathPart(final String resourceType) {
42         if (isData(resourceType)) {
43             return "data";
44         }
45         return "operations";
46     }
47
48     private static boolean isData(final String dataStore) {
49         return "config".contains(dataStore) || "operational".contains(dataStore);
50     }
51
52     @Override
53     protected ListPathBuilder newListPathBuilder() {
54         return new ListPathBuilder() {
55             private String prefix = "=";
56
57             @Override
58             public String nextParamIdentifier(final String key) {
59                 final String str = prefix + "{" + key + "}";
60                 prefix = ",";
61                 return str;
62             }
63         };
64     }
65
66     @Override
67     protected void appendPathKeyValue(final StringBuilder builder, final Object value) {
68         builder.deleteCharAt(builder.length() - 1).append("=").append(value).append('/');
69     }
70 }