cbf3f95b2a776dc616ebd8606697b7b46aa06f2d
[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 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The class provides helper functions to create a node connector
25  *
26  *
27  */
28 public abstract class NodeConnectorCreator {
29     protected static final Logger logger = LoggerFactory
30     .getLogger(NodeConnectorCreator.class);
31     /**
32      * Generic NodeConnector creator
33      * The nodeConnector type is inferred from the node type
34      *
35      * @param portId
36      * @param node
37      * @return
38      */
39     public static NodeConnector createNodeConnector(Object portId, Node node) {
40         if (node.getType().equals(NodeIDType.OPENFLOW)) {
41             try {
42                 return new NodeConnector(NodeConnectorIDType.OPENFLOW,
43                         (Short) portId, node);
44             } catch (ConstructionException e1) {
45                 logger.error("",e1);
46                 return null;
47             }
48         }
49         return null;
50     }
51
52     /**
53      * NodeConnector creator where NodeConnector type can be specified
54      * Needed to create special internal node connectors (like software stack)
55      *
56      * @param nodeConnectorType
57      * @param portId
58      * @param node
59      * @return
60      */
61     public static NodeConnector createNodeConnector(
62             String nodeConnectorType, Object portId, Node node) {
63         try {
64             return new NodeConnector(nodeConnectorType, portId, node);
65         } catch (ConstructionException e1) {
66             logger.error("",e1);
67             return null;
68         }
69     }
70
71     public static NodeConnector createOFNodeConnector(Short portId, Node node) {
72         try {
73             return new NodeConnector(NodeConnectorIDType.OPENFLOW, portId, node);
74         } catch (ConstructionException e1) {
75             logger.error("",e1);
76             return null;
77         }
78     }
79
80     public static Set<NodeConnector> createOFNodeConnectorSet(
81             Set<Short> portIds, Node n) {
82         try {
83             Set<NodeConnector> nodeConnectors = new HashSet<NodeConnector>();
84             for (Short ofPortID : portIds) {
85                 NodeConnector p = new NodeConnector(
86                         NodeConnector.NodeConnectorIDType.OPENFLOW, Short
87                                 .valueOf(ofPortID), n);
88                 nodeConnectors.add(p);
89             }
90             return nodeConnectors;
91         } catch (ConstructionException e1) {
92             logger.error("",e1);
93             return null;
94         }
95     }
96 }