b04ce9a07591fdd6c976fdf391c0747c26db5c89
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / NodeConnectorCreator.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.utils;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.opendaylight.controller.sal.core.ConstructionException;
16 import org.opendaylight.controller.sal.core.Node;
17 import org.opendaylight.controller.sal.core.NodeConnector;
18 import org.opendaylight.controller.sal.core.Node.NodeIDType;
19 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
20
21 /**
22  * The class provides helper functions to create a node connector
23  *
24  *
25  */
26 public abstract class NodeConnectorCreator {
27     /**
28      * Generic NodeConnector creator
29      * The nodeConnector type is inferred from the node type
30      *
31      * @param portId
32      * @param node
33      * @return
34      */
35     public static NodeConnector createNodeConnector(Object portId, Node node) {
36         if (node.getType().equals(NodeIDType.OPENFLOW)) {
37             try {
38                 return new NodeConnector(NodeConnectorIDType.OPENFLOW,
39                         (Short) portId, node);
40             } catch (ConstructionException e1) {
41                 e1.printStackTrace();
42                 return null;
43             }
44         }
45         return null;
46     }
47
48     /**
49      * NodeConnector creator where NodeConnector type can be specified
50      * Needed to create special internal node connectors (like software stack)
51      *
52      * @param nodeConnectorType
53      * @param portId
54      * @param node
55      * @return
56      */
57     public static NodeConnector createNodeConnector(
58             String nodeConnectorType, Object portId, Node node) {
59         try {
60             return new NodeConnector(nodeConnectorType, portId, node);
61         } catch (ConstructionException e1) {
62             e1.printStackTrace();
63             return null;
64         }
65     }
66
67     public static NodeConnector createOFNodeConnector(Short portId, Node node) {
68         try {
69             return new NodeConnector(NodeConnectorIDType.OPENFLOW, portId, node);
70         } catch (ConstructionException e1) {
71             e1.printStackTrace();
72             return null;
73         }
74     }
75
76     public static Set<NodeConnector> createOFNodeConnectorSet(
77             Set<Short> portIds, Node n) {
78         try {
79             Set<NodeConnector> nodeConnectors = new HashSet<NodeConnector>();
80             for (Short ofPortID : portIds) {
81                 NodeConnector p = new NodeConnector(
82                         NodeConnector.NodeConnectorIDType.OPENFLOW, Short
83                                 .valueOf(ofPortID), n);
84                 nodeConnectors.add(p);
85             }
86             return nodeConnectors;
87         } catch (ConstructionException e1) {
88             e1.printStackTrace();
89             return null;
90         }
91     }
92 }