2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.rest.doc.mountpoints;
10 import com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.LinkedList;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.TreeMap;
18 import java.util.concurrent.atomic.AtomicLong;
19 import java.util.concurrent.atomic.AtomicReference;
20 import javax.ws.rs.core.UriInfo;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
25 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGenerator;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
27 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
30 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 public class MountPointSwagger extends BaseYangSwaggerGenerator implements MountProvisionListener {
40 private static final String DATASTORES_REVISION = "-";
41 private static final String DATASTORES_LABEL = "Datastores";
43 private DOMMountPointService mountService;
44 private final Map<YangInstanceIdentifier, Long> instanceIdToLongId = new TreeMap<>(
45 (o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
46 private final Map<Long, YangInstanceIdentifier> longIdToInstanceId = new HashMap<>();
47 private final Object lock = new Object();
49 private final AtomicLong idKey = new AtomicLong(0);
51 private static final AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
52 private SchemaService globalSchema;
54 public Map<String, Long> getInstanceIdentifiers() {
55 final Map<String, Long> urlToId = new HashMap<>();
57 final SchemaContext context = globalSchema.getGlobalContext();
58 for (final Entry<YangInstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
59 final String modName = findModuleName(entry.getKey(), context);
60 urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
67 public void setGlobalSchema(final SchemaService globalSchema) {
68 this.globalSchema = globalSchema;
71 private String findModuleName(final YangInstanceIdentifier id, final SchemaContext context) {
72 final PathArgument rootQName = id.getPathArguments().iterator().next();
73 for (final Module mod : context.getModules()) {
74 if (mod.getDataChildByName(rootQName.getNodeType()) != null) {
81 private String generateUrlPrefixFromInstanceID(final YangInstanceIdentifier key, final String moduleName) {
82 final StringBuilder builder = new StringBuilder();
83 if (moduleName != null) {
84 builder.append(moduleName);
87 for (final PathArgument arg : key.getPathArguments()) {
88 final String name = arg.getNodeType().getLocalName();
89 if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
90 final NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
91 for (final Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
92 builder.append(entry.getValue()).append('/');
99 return builder.toString();
102 private String getYangMountUrl(final YangInstanceIdentifier key) {
103 final String modName = findModuleName(key, globalSchema.getGlobalContext());
104 return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount/";
107 public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
108 final YangInstanceIdentifier iid = getInstanceId(id);
110 return null; // indicating not found.
112 final SchemaContext context = getSchemaContext(iid);
113 if (context == null) {
114 return createResourceList();
116 final List<Resource> resources = new LinkedList<>();
117 final Resource dataStores = new Resource();
118 dataStores.setDescription("Provides methods for accessing the data stores.");
119 dataStores.setPath(generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
120 resources.add(dataStores);
121 final String urlPrefix = getYangMountUrl(iid);
122 final ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
123 resources.addAll(list.getApis());
124 list.setApis(resources);
128 private YangInstanceIdentifier getInstanceId(final Long id) {
129 final YangInstanceIdentifier instanceId;
130 synchronized (lock) {
131 instanceId = longIdToInstanceId.get(id);
136 private SchemaContext getSchemaContext(final YangInstanceIdentifier id) {
142 final Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
143 if (!mountPoint.isPresent()) {
147 final SchemaContext context = mountPoint.get().getSchemaContext();
148 if (context == null) {
154 public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module, final String revision) {
155 final YangInstanceIdentifier iid = getInstanceId(id);
156 final SchemaContext context = getSchemaContext(iid);
157 final String urlPrefix = getYangMountUrl(iid);
158 if (context == null) {
162 if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
163 return generateDataStoreApiDoc(uriInfo, urlPrefix);
165 return super.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
168 private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
169 final List<Api> apis = new LinkedList<>();
170 apis.add(createGetApi("config",
171 "Queries the config (startup) datastore on the mounted hosted.", context));
172 apis.add(createGetApi("operational",
173 "Queries the operational (running) datastore on the mounted hosted.", context));
174 apis.add(createGetApi("operations",
175 "Queries the available operations (RPC calls) on the mounted hosted.", context));
177 final ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
178 declaration.setApis(apis);
183 private Api createGetApi(final String datastore, final String note, final String context) {
184 final Operation getConfig = new Operation();
185 getConfig.setMethod("GET");
186 getConfig.setNickname("GET " + datastore);
187 getConfig.setNotes(note);
189 final Api api = new Api();
190 api.setPath(getDataStorePath("/" + datastore + "/", context));
191 api.setOperations(Collections.singletonList(getConfig));
196 public void setMountService(final DOMMountPointService mountService) {
197 this.mountService = mountService;
201 public void onMountPointCreated(final YangInstanceIdentifier path) {
202 synchronized (lock) {
203 final Long idLong = idKey.incrementAndGet();
204 instanceIdToLongId.put(path, idLong);
205 longIdToInstanceId.put(idLong, path);
210 public void onMountPointRemoved(final YangInstanceIdentifier path) {
211 synchronized (lock) {
212 final Long id = instanceIdToLongId.remove(path);
213 longIdToInstanceId.remove(id);
217 public static MountPointSwagger getInstance() {
218 MountPointSwagger swagger = selfRef.get();
219 if (swagger == null) {
220 selfRef.compareAndSet(null, new MountPointSwagger());
221 swagger = selfRef.get();