2 * Copyright (c) 2014, 2015 Cisco 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
9 package org.opendaylight.yangtools.restconf.client;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.sun.jersey.api.client.ClientResponse;
15 import java.io.InputStream;
16 import java.util.Map.Entry;
17 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
18 import org.opendaylight.yangtools.restconf.client.api.data.Datastore;
19 import org.opendaylight.yangtools.restconf.client.api.data.DefaultRetrievalStrategy;
20 import org.opendaylight.yangtools.restconf.client.api.data.RetrievalStrategy;
21 import org.opendaylight.yangtools.restconf.utils.RestconfUtils;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 public abstract class AbstractDataStore implements Datastore {
30 private final RestconfClientImpl client;
31 private static final int STATUS_OK = 200;
32 private static final int STATUS_NOT_FOUND = 404;
34 public AbstractDataStore(RestconfClientImpl client) {
39 protected RestconfClientImpl getClient() {
44 public <T extends DataObject> ListenableFuture<Optional<T>> readData(InstanceIdentifier<T> path) {
45 return readData(path, DefaultRetrievalStrategy.getInstance());
49 public <T extends DataObject> ListenableFuture<Optional<T>> readData(final InstanceIdentifier<T> path,
50 RetrievalStrategy strategy) {
52 final SchemaContext schemaContext = client.getSchemaContext();
53 final BindingNormalizedNodeCodecRegistry mappingService = client.getMappingService();
54 final YangInstanceIdentifier domPath = mappingService.toYangInstanceIdentifier(path);
55 final Entry<String, DataSchemaNode> pathWithSchema = RestconfUtils.toRestconfIdentifier(domPath,schemaContext);
56 String restconfPath = getStorePrefix() + pathWithSchema.getKey();
58 return client.get(restconfPath, "application/xml",new Function<ClientResponse, Optional<T>>() {
60 @SuppressWarnings("unchecked")
62 public com.google.common.base.Optional<T> apply(ClientResponse response) {
63 switch (response.getStatus()) {
65 DataObject dataObject = deserialize(domPath,response.getEntityInputStream());
66 return (Optional<T>) Optional.of(dataObject);
67 case STATUS_NOT_FOUND:
68 return Optional.<T> absent();
70 throw new IllegalStateException("Failed : HTTP error code : " + response.getStatus());
78 protected final DataObject deserialize(YangInstanceIdentifier domPath, InputStream entityInputStream) {
79 throw new UnsupportedOperationException("Not implemented yet.");
82 protected abstract String getStorePrefix();