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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.LinkedList;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import java.util.Optional;
20 import java.util.TreeMap;
21 import java.util.concurrent.atomic.AtomicLong;
22 import javax.ws.rs.core.UriInfo;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
25 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGenerator;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
30 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
32 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
41 private static final String DATASTORES_REVISION = "-";
42 private static final String DATASTORES_LABEL = "Datastores";
44 private final DOMSchemaService globalSchema;
45 private final DOMMountPointService mountService;
46 private final BaseYangSwaggerGenerator swaggerGenerator;
47 private final Map<YangInstanceIdentifier, Long> instanceIdToLongId =
48 new TreeMap<>((o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
49 private final Map<Long, YangInstanceIdentifier> longIdToInstanceId = new HashMap<>();
51 private final Object lock = new Object();
53 private final AtomicLong idKey = new AtomicLong(0);
55 private ListenerRegistration<DOMMountPointListener> registration;
57 public MountPointSwagger(final DOMSchemaService globalSchema, final DOMMountPointService mountService,
58 final BaseYangSwaggerGenerator swaggerGenerator) {
59 this.globalSchema = requireNonNull(globalSchema);
60 this.mountService = requireNonNull(mountService);
61 this.swaggerGenerator = requireNonNull(swaggerGenerator);
65 registration = mountService.registerProvisionListener(this);
70 if (registration != null) {
75 public Map<String, Long> getInstanceIdentifiers() {
76 final Map<String, Long> urlToId = new HashMap<>();
77 synchronized (this.lock) {
78 final SchemaContext context = this.globalSchema.getGlobalContext();
79 for (final Entry<YangInstanceIdentifier, Long> entry : this.instanceIdToLongId.entrySet()) {
80 final String modName = findModuleName(entry.getKey(), context);
81 urlToId.put(swaggerGenerator.generateUrlPrefixFromInstanceID(entry.getKey(), modName),
88 private static String findModuleName(final YangInstanceIdentifier id, final SchemaContext context) {
89 final PathArgument rootQName = id.getPathArguments().iterator().next();
90 for (final Module mod : context.getModules()) {
91 if (mod.findDataChildByName(rootQName.getNodeType()).isPresent()) {
98 private String getYangMountUrl(final YangInstanceIdentifier key) {
99 final String modName = findModuleName(key, this.globalSchema.getGlobalContext());
100 return swaggerGenerator.generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount";
103 public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
104 final YangInstanceIdentifier iid = getInstanceId(id);
106 return null; // indicating not found.
108 final SchemaContext context = getSchemaContext(iid);
109 if (context == null) {
110 return swaggerGenerator.createResourceList();
112 final List<Resource> resources = new LinkedList<>();
113 final Resource dataStores = new Resource();
114 dataStores.setDescription("Provides methods for accessing the data stores.");
115 dataStores.setPath(swaggerGenerator.generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
116 resources.add(dataStores);
117 final String urlPrefix = getYangMountUrl(iid);
118 final ResourceList list = swaggerGenerator.getResourceListing(uriInfo, context, urlPrefix);
119 resources.addAll(list.getApis());
120 list.setApis(resources);
124 private YangInstanceIdentifier getInstanceId(final Long id) {
125 final YangInstanceIdentifier instanceId;
126 synchronized (this.lock) {
127 instanceId = this.longIdToInstanceId.get(id);
132 private SchemaContext getSchemaContext(final YangInstanceIdentifier id) {
137 checkState(mountService != null);
138 final Optional<DOMMountPoint> mountPoint = this.mountService.getMountPoint(id);
139 if (!mountPoint.isPresent()) {
143 final SchemaContext context = mountPoint.get().getSchemaContext();
144 if (context == null) {
150 public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module,
151 final String revision) {
152 final YangInstanceIdentifier iid = getInstanceId(id);
153 final SchemaContext context = getSchemaContext(iid);
154 final String urlPrefix = getYangMountUrl(iid);
155 if (context == null) {
159 if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
160 return generateDataStoreApiDoc(uriInfo, urlPrefix);
162 return swaggerGenerator.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
165 private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
166 final List<Api> apis = new LinkedList<>();
167 apis.add(createGetApi("config", "Queries the config (startup) datastore on the mounted hosted.", context));
168 apis.add(createGetApi("operational", "Queries the operational (running) datastore on the mounted hosted.",
170 apis.add(createGetApi("operations", "Queries the available operations (RPC calls) on the mounted hosted.",
173 final ApiDeclaration declaration = swaggerGenerator.createApiDeclaration(
174 swaggerGenerator.createBasePathFromUriInfo(uriInfo));
175 declaration.setApis(apis);
180 private Api createGetApi(final String datastore, final String note, final String context) {
181 final Operation getConfig = new Operation();
182 getConfig.setMethod("GET");
183 getConfig.setNickname("GET " + datastore);
184 getConfig.setNotes(note);
186 final Api api = new Api();
187 api.setPath(swaggerGenerator.getDataStorePath(datastore, context).concat(
188 swaggerGenerator.getContent(datastore)));
189 api.setOperations(Collections.singletonList(getConfig));
195 public void onMountPointCreated(final YangInstanceIdentifier path) {
196 synchronized (this.lock) {
197 final Long idLong = this.idKey.incrementAndGet();
198 this.instanceIdToLongId.put(path, idLong);
199 this.longIdToInstanceId.put(idLong, path);
204 public void onMountPointRemoved(final YangInstanceIdentifier path) {
205 synchronized (this.lock) {
206 final Long id = this.instanceIdToLongId.remove(path);
207 this.longIdToInstanceId.remove(id);