29973b7b330887454c48dbc976712041e428b3d4
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / NodeMapping.xtend
1 package org.opendaylight.controller.sal.compatibility
2
3 import org.opendaylight.controller.sal.core.Node
4 import org.opendaylight.controller.sal.core.NodeConnector
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
6 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
7
8 import static com.google.common.base.Preconditions.*;
9 import static extension org.opendaylight.controller.sal.common.util.Arguments.*;
10
11 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
15 import org.opendaylight.controller.sal.core.ConstructionException
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
22 import org.opendaylight.controller.sal.core.Bandwidth
23 import org.opendaylight.controller.sal.core.AdvertisedBandwidth
24 import org.opendaylight.controller.sal.core.SupportedBandwidth
25 import org.opendaylight.controller.sal.core.PeerBandwidth
26 import org.opendaylight.controller.sal.core.Name
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
28 import org.opendaylight.controller.sal.core.Config
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State
30
31 public class NodeMapping {
32
33     public static val MD_SAL_TYPE = "MD_SAL";
34     private static val NODE_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35     private static val NODECONNECTOR_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
36
37     private new() {
38         throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
39     }
40
41     public static def toADNode(InstanceIdentifier<?> node) throws ConstructionException {
42         checkNotNull(node);
43         checkNotNull(node.getPath());
44         checkArgument(node.getPath().size() >= 2);
45         val arg = node.getPath().get(1);
46         val item = arg.checkInstanceOf(IdentifiableItem);
47         val nodeKey = item.getKey().checkInstanceOf(NodeKey);
48         return new Node(MD_SAL_TYPE, nodeKey.id.toADNodeId);
49     }
50     
51     public static def toADNodeId(NodeId nodeId) {
52         checkNotNull(nodeId);
53         return nodeId.value
54     }
55
56
57     public static def toADNodeConnector(NodeConnectorRef source) throws ConstructionException {
58         checkNotNull(source);
59         val InstanceIdentifier<?> path = checkNotNull(source.getValue());
60         val node = path.toADNode();
61         checkArgument(path.path.size() >= 3);
62         val arg = path.getPath().get(2);
63         val item = arg.checkInstanceOf(IdentifiableItem);
64         val connectorKey = item.getKey().checkInstanceOf(NodeConnectorKey);
65         return new NodeConnector(MD_SAL_TYPE, connectorKey.id.toADNodeConnectorId, node);
66     }
67     
68     public static def toADNodeConnectorId(NodeConnectorId nodeConnectorId) {
69         return nodeConnectorId.value
70     }
71     
72     public static def toNodeRef(Node node) {
73         checkArgument(MD_SAL_TYPE.equals(node.getType()));
74         var nodeId = node.ID.checkInstanceOf(String)
75         val nodeKey = new NodeKey(new NodeId(nodeId));
76         val nodePath = InstanceIdentifier.builder().node(Nodes).child(NODE_CLASS, nodeKey).toInstance();
77         return new NodeRef(nodePath);
78     }
79     
80     public static def toNodeConnectorRef(NodeConnector nodeConnector) {
81         val node = nodeConnector.node.toNodeRef();
82         val nodePath = node.getValue() as InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node>
83         var nodeConnectorId = nodeConnector.ID.checkInstanceOf(String)
84         val connectorKey = new NodeConnectorKey(new NodeConnectorId(nodeConnectorId));
85         val path = InstanceIdentifier.builder(nodePath).child(NODECONNECTOR_CLASS, connectorKey).toInstance();
86         return new NodeConnectorRef(path);
87     }
88
89     public static def toADNode(NodeRef node) throws ConstructionException {
90         return toADNode(node.getValue());
91     }
92     
93     public static def toADNodeConnectorProperties(NodeConnectorUpdated nc) {
94         val props = new java.util.HashSet<org.opendaylight.controller.sal.core.Property>();
95         val fcncu = nc.getAugmentation(FlowCapableNodeConnectorUpdated)
96         if(fcncu != null) {
97             if(fcncu.currentFeature != null && fcncu.currentFeature.toAdBandwidth != null) {
98                 props.add(fcncu.currentFeature.toAdBandwidth)
99             }
100             if(fcncu.advertisedFeatures != null && fcncu.advertisedFeatures.toAdAdvertizedBandwidth != null) {
101                 props.add(fcncu.advertisedFeatures.toAdAdvertizedBandwidth)
102             }
103             if(fcncu.supported != null && fcncu.supported.toAdSupportedBandwidth != null) {
104                 props.add(fcncu.supported.toAdSupportedBandwidth)
105             }
106             if(fcncu.peerFeatures != null && fcncu.peerFeatures.toAdPeerBandwidth != null) {
107                 props.add(fcncu.peerFeatures.toAdPeerBandwidth)
108             }
109             if(fcncu.name != null && fcncu.name.toAdName != null) {
110                 props.add(fcncu.name.toAdName)
111             }
112             if(fcncu.configuration != null && fcncu.configuration.toAdConfig != null) {
113                 props.add(fcncu.configuration.toAdConfig)
114             }
115             if(fcncu.state != null && fcncu.state.toAdState != null) {
116                 props.add(fcncu.state.toAdState)
117             }
118         }
119         return props
120     }
121     
122     public static def toAdName(String name) {
123         return new Name(name)
124     } 
125     
126     public static def toAdConfig(PortConfig pc) {
127         var Config config;
128         if(pc.PORTDOWN){
129             config = new Config(Config.ADMIN_DOWN)
130         } else {
131             config = new Config(Config.ADMIN_UP)
132         }
133         return config
134     }
135     
136     public static def toAdState(State s) {
137         var org.opendaylight.controller.sal.core.State state
138         if(s.linkDown) {
139             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN)
140         } else {
141             state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP)
142         }
143         return state
144     }
145     
146     public static def toAdBandwidth(PortFeatures pf) {
147         var Bandwidth bw = null
148         if (pf.is_10mbHd || pf.is_10mbFd ) {
149             bw= new Bandwidth(Bandwidth.BW10Mbps)
150         } else if (pf.is_100mbHd || pf.is_100mbFd ) {
151             bw= new Bandwidth(Bandwidth.BW100Mbps)
152         } else if (pf.is_1gbHd || pf.is_1gbFd ) {
153             bw= new Bandwidth(Bandwidth.BW1Gbps)
154         } else if (pf.is_1gbFd ) {
155             bw= new Bandwidth(Bandwidth.BW10Gbps)
156         } else if ( pf.is_10gbFd ) {
157             bw= new Bandwidth(Bandwidth.BW10Gbps)
158         } else if ( pf.is_40gbFd ) {
159             bw= new Bandwidth(Bandwidth.BW40Gbps)
160         } else if ( pf.is_100gbFd ) {
161             bw= new Bandwidth(Bandwidth.BW100Gbps)
162         } else if ( pf.is_1tbFd ) {
163             bw= new Bandwidth(Bandwidth.BW1Tbps)
164         } 
165         return bw;
166     }
167     
168     public static def toAdAdvertizedBandwidth(PortFeatures pf) {
169         var AdvertisedBandwidth abw
170         val bw = pf.toAdBandwidth
171         if(bw != null) {
172             abw = new AdvertisedBandwidth(bw.value)
173         }
174         return abw
175     }
176     
177     public static def toAdSupportedBandwidth(PortFeatures pf) {
178         var SupportedBandwidth sbw
179         val bw = pf.toAdBandwidth
180         if(bw != null ) {
181             sbw = new SupportedBandwidth(bw.value)
182         }
183         return sbw
184     }
185     
186     public static def toAdPeerBandwidth(PortFeatures pf) {
187         var PeerBandwidth pbw
188         val bw = pf.toAdBandwidth
189         if(bw != null) {
190             pbw = new PeerBandwidth(bw.value)
191         }
192         return pbw
193     }
194     
195     
196 }