import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
+import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import javax.annotation.Nullable;
import javax.ws.rs.core.Response.Status;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.restconf.common.patch.PatchEntity;
import org.opendaylight.restconf.common.patch.PatchStatusContext;
import org.opendaylight.restconf.common.patch.PatchStatusEntity;
+import org.opendaylight.restconf.common.util.DataChangeScope;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class BrokerFacade {
+@SuppressWarnings("checkstyle:FinalClass")
+public class BrokerFacade implements Closeable {
private static final Logger LOG = LoggerFactory.getLogger(BrokerFacade.class);
- private static final BrokerFacade INSTANCE = new BrokerFacade();
private volatile DOMRpcService rpcService;
- private DOMDataBroker domDataBroker;
- private DOMNotificationService domNotification;
+ private final DOMDataBroker domDataBroker;
+ private final DOMNotificationService domNotification;
+ private final ControllerContext controllerContext;
- BrokerFacade() {
-
- }
-
- public void setRpcService(final DOMRpcService router) {
- this.rpcService = router;
- }
-
- public void setDomNotificationService(final DOMNotificationService service) {
- this.domNotification = service;
+ private BrokerFacade(DOMRpcService rpcService, DOMDataBroker domDataBroker, DOMNotificationService domNotification,
+ ControllerContext controllerContext) {
+ this.rpcService = Objects.requireNonNull(rpcService);
+ this.domDataBroker = Objects.requireNonNull(domDataBroker);
+ this.domNotification = Objects.requireNonNull(domNotification);
+ this.controllerContext = Objects.requireNonNull(controllerContext);
}
- public static BrokerFacade getInstance() {
- return BrokerFacade.INSTANCE;
+ public static BrokerFacade newInstance(DOMRpcService rpcService, DOMDataBroker domDataBroker,
+ DOMNotificationService domNotification, ControllerContext controllerContext) {
+ return new BrokerFacade(rpcService, domDataBroker, domNotification, controllerContext);
}
- private void checkPreconditions() {
- if (this.domDataBroker == null) {
- throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
- }
+ @Override
+ public void close() {
}
/**
* @return read date
*/
public NormalizedNode<?, ?> readConfigurationData(final YangInstanceIdentifier path, final String withDefa) {
- checkPreconditions();
try (DOMDataReadOnlyTransaction tx = this.domDataBroker.newReadOnlyTransaction()) {
return readDataViaTransaction(tx, CONFIGURATION, path, withDefa);
}
* @return read data
*/
public NormalizedNode<?, ?> readOperationalData(final YangInstanceIdentifier path) {
- checkPreconditions();
-
try (DOMDataReadOnlyTransaction tx = this.domDataBroker.newReadOnlyTransaction()) {
return readDataViaTransaction(tx, OPERATIONAL, path);
}
Preconditions.checkNotNull(path);
Preconditions.checkNotNull(payload);
- checkPreconditions();
-
final DOMDataReadWriteTransaction newReadWriteTransaction = this.domDataBroker.newReadWriteTransaction();
final Status status = readDataViaTransaction(newReadWriteTransaction, CONFIGURATION, path) != null ? Status.OK
: Status.CREATED;
}
break;
case DELETE:
- if (withoutError) {
- try {
- deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
- .getTargetNode());
- editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
- } catch (final RestconfDocumentedException e) {
- LOG.error("Error call http Patch operation {} on target {}",
- operation,
- patchEntity.getTargetNode().toString());
-
- editErrors = new ArrayList<>();
- editErrors.addAll(e.getErrors());
- editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
- withoutError = false;
- }
- }
- break;
case REMOVE:
if (withoutError) {
try {
public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload,
final String insert, final String point) {
- checkPreconditions();
return postDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path, payload,
globalSchema, insert, point);
}
// DELETE configuration
public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
final YangInstanceIdentifier path) {
- checkPreconditions();
return deleteDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path);
}
// RPC
public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type,
final NormalizedNode<?, ?> input) {
- checkPreconditions();
if (this.rpcService == null) {
throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
}
public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
final ListenerAdapter listener) {
- checkPreconditions();
-
if (listener.isListening()) {
return;
}
throw new RestconfDocumentedException("Bad value used with with-defaults parameter : " + withDefa);
}
- final SchemaContext ctx = ControllerContext.getInstance().getGlobalSchema();
+ final SchemaContext ctx = controllerContext.getGlobalSchema();
final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx);
final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode();
if (result instanceof ContainerNode) {
builder.withChild(childBuilder.build());
} else if (child instanceof LeafNode) {
final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
- final Object nodeVal = ((LeafNode<?>) child).getValue();
+ final Object nodeVal = child.getValue();
final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
Builders.leafBuilder((LeafSchemaNode) childSchema);
if (keys.contains(child.getNodeType())) {
- leafBuilder.withValue(((LeafNode<?>) child).getValue());
+ leafBuilder.withValue(child.getValue());
builder.withChild(leafBuilder.build());
} else {
if (trim) {
if (defaultVal == null || !defaultVal.equals(nodeVal)) {
- leafBuilder.withValue(((LeafNode<?>) child).getValue());
+ leafBuilder.withValue(child.getValue());
builder.withChild(leafBuilder.build());
}
} else {
if (defaultVal != null && defaultVal.equals(nodeVal)) {
- leafBuilder.withValue(((LeafNode<?>) child).getValue());
+ leafBuilder.withValue(child.getValue());
builder.withChild(leafBuilder.build());
}
}
builder.withChild(childBuilder.build());
} else if (child instanceof LeafNode) {
final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
- final Object nodeVal = ((LeafNode<?>) child).getValue();
+ final Object nodeVal = child.getValue();
final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
Builders.leafBuilder((LeafSchemaNode) childSchema);
if (trim) {
if (defaultVal == null || !defaultVal.equals(nodeVal)) {
- leafBuilder.withValue(((LeafNode<?>) child).getValue());
+ leafBuilder.withValue(child.getValue());
builder.withChild(leafBuilder.build());
}
} else {
if (defaultVal != null && defaultVal.equals(nodeVal)) {
- leafBuilder.withValue(((LeafNode<?>) child).getValue());
+ leafBuilder.withValue(child.getValue());
builder.withChild(leafBuilder.build());
}
}
}
}
- private static void insertWithPointLeafListPost(final DOMDataReadWriteTransaction rwTransaction,
+ private void insertWithPointLeafListPost(final DOMDataReadWriteTransaction rwTransaction,
final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload,
final SchemaContext schemaContext, final String point, final OrderedLeafSetNode<?> readLeafList,
final boolean before) {
rwTransaction.delete(datastore, path.getParent().getParent());
- final InstanceIdentifierContext<?> instanceIdentifier =
- ControllerContext.getInstance().toInstanceIdentifier(point);
+ final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier(point);
int lastItemPosition = 0;
for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
}
}
- private static void insertWithPointListPost(final DOMDataReadWriteTransaction rwTransaction,
+ private void insertWithPointListPost(final DOMDataReadWriteTransaction rwTransaction,
final LogicalDatastoreType datastore,
final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext,
final String point, final MapNode readList, final boolean before) {
rwTransaction.delete(datastore, path.getParent().getParent());
- final InstanceIdentifierContext<?> instanceIdentifier =
- ControllerContext.getInstance().toInstanceIdentifier(point);
+ final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier(point);
int lastItemPosition = 0;
for (final MapEntryNode mapEntryNode : readList.getValue()) {
if (mapEntryNode.getIdentifier()
}
}
- private static void insertWithPointLeafListPut(final DOMDataWriteTransaction tx,
+ private void insertWithPointLeafListPut(final DOMDataWriteTransaction tx,
final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload,
final SchemaContext schemaContext, final String point, final OrderedLeafSetNode<?> readLeafList,
final boolean before) {
tx.delete(datastore, path.getParent());
- final InstanceIdentifierContext<?> instanceIdentifier =
- ControllerContext.getInstance().toInstanceIdentifier(point);
+ final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier(point);
int index1 = 0;
for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
}
}
- private static void insertWithPointListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore,
+ private void insertWithPointListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore,
final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext,
final String point, final OrderedMapNode readList, final boolean before) {
tx.delete(datastore, path.getParent());
- final InstanceIdentifierContext<?> instanceIdentifier =
- ControllerContext.getInstance().toInstanceIdentifier(point);
+ final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier(point);
int index1 = 0;
for (final MapEntryNode mapEntryNode : readList.getValue()) {
if (mapEntryNode.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
tx.merge(datastore, path, payload);
}
- public void setDomDataBroker(final DOMDataBroker domDataBroker) {
- this.domDataBroker = domDataBroker;
- }
-
public void registerToListenNotification(final NotificationListenerAdapter listener) {
- checkPreconditions();
-
if (listener.isListening()) {
return;
}