Fix Metadata Eid processing for LISP
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / lisp / LispStateCommandExecutor.java
1 /*
2  * Copyright (c) 2017 Cisco Systems. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.lisp;
10
11 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.lisp.AbstractLispCommand;
12 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction;
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.LispUtil;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Function;
24
25 public class LispStateCommandExecutor {
26     private static final Logger LOG = LoggerFactory.getLogger(LispStateCommandExecutor.class);
27
28     public static <T extends DataObject> boolean executePutCommand(InstanceIdentifier<Node> nodeIid,
29                                                                    AbstractLispCommand<T> lispStateCommand) {
30         lispStateCommand.setOperation(General.Operations.PUT);
31         return executeCommand(nodeIid, lispStateCommand);
32     }
33
34     public static <T extends DataObject> boolean executePutCommand(String hostName,
35             AbstractLispCommand<T> lispStateCommand) {
36         lispStateCommand.setOperation(General.Operations.PUT);
37         return executeCommand(LispUtil.HOSTNAME_TO_IID.apply(hostName), lispStateCommand);
38     }
39
40     public static <T extends DataObject> boolean executeMergeCommand(String hostName,
41         AbstractLispCommand<T> lispStateCommand) {
42         lispStateCommand.setOperation(General.Operations.MERGE);
43         return executeCommand(LispUtil.HOSTNAME_TO_IID.apply(hostName), lispStateCommand);
44     }
45
46     public static <T extends DataObject> boolean executeDeleteCommand(InstanceIdentifier<Node> nodeIid,
47             AbstractLispCommand<T> lispStateCommand) {
48         lispStateCommand.setOperation(General.Operations.DELETE);
49         return executeCommand(nodeIid, lispStateCommand);
50     }
51
52     public static <T extends DataObject> boolean executeDeleteCommand(String hostName,
53             AbstractLispCommand<T> lispStateCommand) {
54         lispStateCommand.setOperation(General.Operations.DELETE);
55         return executeCommand(LispUtil.HOSTNAME_TO_IID.apply(hostName), lispStateCommand);
56     }
57
58     public static <T extends DataObject> boolean executeCommand(InstanceIdentifier<Node> nodeIid,
59             AbstractLispCommand<T> lispStateCommand) {
60         final boolean transactionState;
61         switch (lispStateCommand.getOperation()) {
62             case MERGE:
63             case PUT: {
64                 transactionState = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, lispStateCommand.getIid(),
65                         lispStateCommand.getData(), GbpNetconfTransaction.RETRY_COUNT);
66                 break;
67             }
68             case DELETE: {
69                 transactionState = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, lispStateCommand.getIid(),
70                         GbpNetconfTransaction.RETRY_COUNT);
71             }
72                 break;
73             default:
74                 throw new IllegalStateException("No supported operation specified.");
75         }
76         if (transactionState) {
77             LOG.trace("Successfully executed command: {}", lispStateCommand);
78         } else {
79             LOG.debug("Failed to execute command: {}", lispStateCommand);
80         }
81
82         return transactionState;
83     }
84 }