95bb1e60f565432aa08036668694452b83857798
[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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class LispStateCommandExecutor {
22     private static final Logger LOG = LoggerFactory.getLogger(LispStateCommandExecutor.class);
23
24     public static <T extends DataObject> boolean executePutCommand(InstanceIdentifier<Node> nodeIid,
25                                                                    AbstractLispCommand<T> lispStateCommand) {
26         lispStateCommand.setOperation(General.Operations.PUT);
27         return executeCommand(nodeIid, lispStateCommand);
28     }
29
30     static <T extends DataObject> boolean executePutCommand(String hostName, AbstractLispCommand<T> lispStateCommand) {
31         lispStateCommand.setOperation(General.Operations.PUT);
32         return executeCommand(LispUtil.hostnameToIid(hostName), lispStateCommand);
33     }
34
35     static <T extends DataObject> boolean executeMergeCommand(String hostName, AbstractLispCommand<T> lispStateCommand) {
36         lispStateCommand.setOperation(General.Operations.MERGE);
37         return executeCommand(LispUtil.hostnameToIid(hostName), lispStateCommand);
38     }
39
40     public static <T extends DataObject> boolean executeDeleteCommand(InstanceIdentifier<Node> nodeIid,
41             AbstractLispCommand<T> lispStateCommand) {
42         lispStateCommand.setOperation(General.Operations.DELETE);
43         return executeCommand(nodeIid, lispStateCommand);
44     }
45
46     static <T extends DataObject> boolean executeDeleteCommand(String hostName, AbstractLispCommand<T> lispStateCommand) {
47         lispStateCommand.setOperation(General.Operations.DELETE);
48         return executeCommand(LispUtil.hostnameToIid(hostName), lispStateCommand);
49     }
50
51     private static <T extends DataObject> boolean executeCommand(InstanceIdentifier<Node> nodeIid,
52         AbstractLispCommand<T> lispStateCommand) {
53         final boolean transactionState;
54         switch (lispStateCommand.getOperation()) {
55             case MERGE:
56             case PUT: {
57                 transactionState = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, lispStateCommand.getIid(),
58                         lispStateCommand.getData(), GbpNetconfTransaction.RETRY_COUNT);
59                 break;
60             }
61             case DELETE: {
62                 transactionState = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, lispStateCommand.getIid(),
63                         GbpNetconfTransaction.RETRY_COUNT);
64             }
65                 break;
66             default:
67                 throw new IllegalStateException("No supported operation specified.");
68         }
69         if (transactionState) {
70             LOG.trace("Successfully executed command: {}", lispStateCommand);
71         } else {
72             LOG.debug("Failed to execute command: {}", lispStateCommand);
73         }
74
75         return transactionState;
76     }
77 }