Move NodeUtils.java to ovsdb.utils
[ovsdb.git] / utils / mdsal-node / src / main / java / org / opendaylight / ovsdb / utils / mdsal / node / NodeUtils.java
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
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  * Authors : Sam Hague
9  */
10 package org.opendaylight.ovsdb.utils.mdsal.node;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.sal.core.ConstructionException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class NodeUtils {
23     protected static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class);
24
25     public static String getId (String identifier) {
26         String id = identifier;
27
28         String[] pair = identifier.split("\\|");
29         if (pair[0].equals("OVS")) {
30             id = pair[1];
31         }
32         return id;
33     }
34
35     public static Node getOpenFlowNode (String identifier) {
36         NodeId nodeId = new NodeId(identifier);
37         NodeKey nodeKey = new NodeKey(nodeId);
38         Node node = new NodeBuilder()
39                 .setId(nodeId)
40                 .setKey(nodeKey)
41                 .build();
42
43         return node;
44     }
45
46     public static Node getMdsalNode (org.opendaylight.controller.sal.core.Node salNode) {
47         String identifier = salNode.getNodeIDString();
48
49         NodeId nodeId = new NodeId("OVS" + "|" + identifier);
50         NodeKey nodeKey = new NodeKey(nodeId);
51         Node node = new NodeBuilder()
52                 .setId(nodeId)
53                 .setKey(nodeKey)
54                 .build();
55
56         return node;
57     }
58
59     public static org.opendaylight.controller.sal.core.Node getSalNode (Node mdsalNode) {
60         String identifier = NodeUtils.getId(mdsalNode.getId().getValue());
61         org.opendaylight.controller.sal.core.Node node = null;
62
63         try {
64             node = new org.opendaylight.controller.sal.core.Node("OVS", identifier);
65         } catch (ConstructionException e) {
66             LOG.error("Failed to allocate sal Node", e);
67         }
68
69         return node;
70     }
71
72     public static List<org.opendaylight.controller.sal.core.Node> getSalNodes (List<Node> mdsalNodes) {
73         List<org.opendaylight.controller.sal.core.Node> nodes = new ArrayList<>();
74
75         for (Node mdsalNode : mdsalNodes) {
76             nodes.add(NodeUtils.getSalNode(mdsalNode));
77         }
78         return nodes;
79     }
80 }