+
+ public static Instruction buildAndGetPopVlanActionInstruction(int actionKey, int instructionKey) {
+ Action popVlanAction = new ActionBuilder().setAction(
+ new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
+ .setKey(new ActionKey(actionKey)).build();
+ List<Action> listAction = new ArrayList<Action> ();
+ listAction.add(popVlanAction);
+ ApplyActions applyActions = new ApplyActionsBuilder().setAction(listAction).build();
+ ApplyActionsCase applyActionsCase = new ApplyActionsCaseBuilder().setApplyActions(applyActions).build();
+ InstructionBuilder instructionBuilder = new InstructionBuilder();
+
+ instructionBuilder.setInstruction(applyActionsCase);
+ instructionBuilder.setKey(new InstructionKey(instructionKey));
+ return instructionBuilder.build();
+ }
+
+ public static Instruction buildAndGetWriteMetadaInstruction(BigInteger metadata,
+ BigInteger mask, int instructionKey) {
+ return new InstructionBuilder()
+ .setInstruction(
+ new WriteMetadataCaseBuilder().setWriteMetadata(
+ new WriteMetadataBuilder().setMetadata(metadata).setMetadataMask(mask).build())
+ .build()).setKey(new InstructionKey(instructionKey)).build();
+ }
+
+ public static Instruction buildAndGetGotoTableInstruction(short tableId, int instructionKey) {
+ return new InstructionBuilder()
+ .setInstruction(
+ new GoToTableCaseBuilder().setGoToTable(
+ new GoToTableBuilder().setTableId(tableId).build()).build())
+ .setKey(new InstructionKey(instructionKey)).build();
+ }
+
+ public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
+ InstanceIdentifier<T> path, DataBroker broker) {
+
+ ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
+
+ Optional<T> result = Optional.absent();
+ try {
+ result = tx.read(datastoreType, path).get();
+ } catch (Exception e) {
+ logger.error("An error occured while reading data from the path {} with the exception {}", path, e);
+ }
+ return result;
+ }
+
+ public static <T extends DataObject> Optional<T> read(DataBroker broker,
+ LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
+
+ ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
+
+ Optional<T> result = Optional.absent();
+ try {
+ result = tx.read(datastoreType, path).get();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ return result;
+ }
+
+ public static <T extends DataObject> void syncWrite(DataBroker broker,
+ LogicalDatastoreType datastoreType, InstanceIdentifier<T> path,
+ T data) {
+ WriteTransaction tx = broker.newWriteOnlyTransaction();
+ tx.put(datastoreType, path, data, true);
+ CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
+ try {
+ futures.get();
+ } catch (InterruptedException | ExecutionException e) {
+ logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public static <T extends DataObject> void syncUpdate(DataBroker broker,
+ LogicalDatastoreType datastoreType, InstanceIdentifier<T> path,
+ T data) {
+ WriteTransaction tx = broker.newWriteOnlyTransaction();
+ tx.merge(datastoreType, path, data, true);
+ CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
+ try {
+ futures.get();
+ } catch (InterruptedException | ExecutionException e) {
+ logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public static <T extends DataObject> void syncDelete(DataBroker broker,
+ LogicalDatastoreType datastoreType, InstanceIdentifier<T> obj) {
+ WriteTransaction tx = broker.newWriteOnlyTransaction();
+ tx.delete(datastoreType, obj);
+ CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
+ try {
+ futures.get();
+ } catch (InterruptedException | ExecutionException e) {
+ logger.error("Error deleting from datastore (path) : ({})", obj);
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public static byte[] getMacAddressForNodeConnector(DataBroker broker,
+ InstanceIdentifier<NodeConnector> nodeConnectorId) {
+ Optional<NodeConnector> optNc = MDSALDataStoreUtils.read(broker,
+ LogicalDatastoreType.OPERATIONAL, nodeConnectorId);
+ if(optNc.isPresent()) {
+ NodeConnector nc = optNc.get();
+ FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class);
+ MacAddress macAddress = fcnc.getHardwareAddress();
+ return HexEncode.bytesFromHexString(macAddress.getValue());
+ }
+ return null;
+ }
+
+ public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
+ return new NodeId(ncId.getValue().substring(0,
+ ncId.getValue().lastIndexOf(":")));
+ }
+
+ public static String getInterfaceName(NodeConnectorRef ref, DataBroker dataBroker) {
+ NodeConnectorId nodeConnectorId = getNodeConnectorId(dataBroker, ref);
+ NodeId nodeId = getNodeIdFromNodeConnectorId(nodeConnectorId);
+ InstanceIdentifier<NodeConnector> ncIdentifier = InstanceIdentifier
+ .builder(Nodes.class)
+ .child(Node.class, new NodeKey(nodeId))
+ .child(NodeConnector.class,
+ new NodeConnectorKey(nodeConnectorId)).build();
+
+ Optional<NodeConnector> nodeConnectorOptional = read(
+ dataBroker,
+ LogicalDatastoreType.OPERATIONAL, ncIdentifier);
+ if (!nodeConnectorOptional.isPresent()) {
+ return null;
+ }
+ NodeConnector nc = nodeConnectorOptional.get();
+ FlowCapableNodeConnector fc = nc
+ .getAugmentation(FlowCapableNodeConnector.class);
+ return fc.getName();
+ }
+
+ public static NodeConnectorId getNodeConnectorId(DataBroker dataBroker,
+ NodeConnectorRef ref) {
+ Optional<NodeConnector> nc = (Optional<NodeConnector>) read(
+ dataBroker,
+ LogicalDatastoreType.CONFIGURATION, ref.getValue());
+ return nc.get().getId();
+ }
+