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.controller.sal.rest.doc.mountpoints;
10 import java.util.Collections;
11 import java.util.Comparator;
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;
21 import javax.ws.rs.core.UriInfo;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
25 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
26 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService.MountProvisionListener;
27 import org.opendaylight.controller.sal.rest.doc.impl.BaseYangSwaggerGenerator;
28 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
29 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
30 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
31 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
32 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 public class MountPointSwagger extends BaseYangSwaggerGenerator implements MountProvisionListener {
42 private static final String DATASTORES_REVISION = "-";
43 private static final String DATASTORES_LABEL = "Datastores";
45 private MountProvisionService mountService;
46 private final Map<InstanceIdentifier, Long> instanceIdToLongId = new TreeMap<>(
47 new Comparator<InstanceIdentifier>() {
49 public int compare(InstanceIdentifier o1, InstanceIdentifier o2) {
50 return o1.toString().compareToIgnoreCase(o2.toString());
53 private final Map<Long, InstanceIdentifier> longIdToInstanceId = new HashMap<>();
54 private final Object lock = new Object();
56 private final AtomicLong idKey = new AtomicLong(0);
58 private static AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
59 private SchemaService globalSchema;
61 public Map<String, Long> getInstanceIdentifiers() {
62 Map<String, Long> urlToId = new HashMap<>();
64 SchemaContext context = globalSchema.getGlobalContext();
65 for (Entry<InstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
66 String modName = findModuleName(entry.getKey(), context);
67 urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
74 public void setGlobalSchema(SchemaService globalSchema) {
75 this.globalSchema = globalSchema;
78 private String findModuleName(InstanceIdentifier id, SchemaContext context) {
79 PathArgument rootQName = id.getPath().get(0);
80 for (Module mod : context.getModules()) {
81 if (mod.getDataChildByName(rootQName.getNodeType()) != null) {
88 private String generateUrlPrefixFromInstanceID(InstanceIdentifier key, String moduleName) {
89 List<PathArgument> path = key.getPath();
90 StringBuilder builder = new StringBuilder();
91 if (moduleName != null) {
92 builder.append(moduleName);
96 for (PathArgument arg : path) {
98 String name = arg.getNodeType().getLocalName();
104 builder.append(name);
105 if (arg instanceof InstanceIdentifier.NodeIdentifierWithPredicates) {
106 NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
107 for (Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
108 builder.append("/").append(entry.getValue());
113 return builder.append("/").toString();
116 private String getYangMountUrl(InstanceIdentifier key) {
117 String modName = findModuleName(key, globalSchema.getGlobalContext());
118 return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount/";
121 public ResourceList getResourceList(UriInfo uriInfo, Long id) {
122 InstanceIdentifier iid = getInstanceId(id);
124 return null; // indicating not found.
126 SchemaContext context = getSchemaContext(iid);
127 String urlPrefix = getYangMountUrl(iid);
128 if (context == null) {
129 return createResourceList();
131 List<Resource> resources = new LinkedList<>();
132 Resource dataStores = new Resource();
133 dataStores.setDescription("Provides methods for accessing the data stores.");
134 dataStores.setPath(generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
135 resources.add(dataStores);
136 ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
137 resources.addAll(list.getApis());
138 list.setApis(resources);
142 private InstanceIdentifier getInstanceId(Long id) {
143 InstanceIdentifier instanceId;
144 synchronized (lock) {
145 instanceId = longIdToInstanceId.get(id);
150 private SchemaContext getSchemaContext(InstanceIdentifier id) {
156 MountProvisionInstance mountPoint = mountService.getMountPoint(id);
157 if (mountPoint == null) {
161 SchemaContext context = mountPoint.getSchemaContext();
162 if (context == null) {
168 public ApiDeclaration getMountPointApi(UriInfo uriInfo, Long id, String module, String revision) {
169 InstanceIdentifier iid = getInstanceId(id);
170 SchemaContext context = getSchemaContext(iid);
171 String urlPrefix = getYangMountUrl(iid);
172 if (context == null) {
176 if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
177 return generateDataStoreApiDoc(uriInfo, urlPrefix);
179 return super.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
182 private ApiDeclaration generateDataStoreApiDoc(UriInfo uriInfo, String context) {
184 ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
185 List<Api> apis = new LinkedList<>();
186 apis.add(createGetApi("config",
187 "Queries the config (startup) datastore on the mounted hosted.", context));
188 apis.add(createGetApi("operational",
189 "Queries the operational (running) datastore on the mounted hosted.", context));
190 apis.add(createGetApi("operations",
191 "Queries the available operations (RPC calls) on the mounted hosted.", context));
192 declaration.setApis(apis);
197 private Api createGetApi(String datastore, String note, String context) {
198 Operation getConfig = new Operation();
199 getConfig.setMethod("GET");
200 getConfig.setNickname("GET " + datastore);
201 getConfig.setNotes(note);
204 api.setPath(getDataStorePath("/" + datastore + "/", context));
205 api.setOperations(Collections.singletonList(getConfig));
210 public void setMountService(MountProvisionService mountService) {
211 this.mountService = mountService;
215 public void onMountPointCreated(InstanceIdentifier path) {
216 synchronized (lock) {
217 Long idLong = idKey.incrementAndGet();
218 instanceIdToLongId.put(path, idLong);
219 longIdToInstanceId.put(idLong, path);
224 public void onMountPointRemoved(InstanceIdentifier path) {
225 synchronized (lock) {
226 Long id = instanceIdToLongId.remove(path);
227 longIdToInstanceId.remove(id);
231 public static MountPointSwagger getInstance() {
232 MountPointSwagger swagger = selfRef.get();
233 if (swagger == null) {
234 selfRef.compareAndSet(null, new MountPointSwagger());
235 swagger = selfRef.get();