Presto NRP migrated to new TAPI-based model. Common infrastructure refactored to...
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / mef / nrp / common / ServicePort.java
1 /*
2  * Copyright (c) 2016 Cisco Systems Inc and others.  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 package org.opendaylight.unimgr.mef.nrp.common;
9
10 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
11 import org.opendaylight.unimgr.utils.SipHandler;
12 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp_interface.rev170227.nrp.create.connectivity.service.end.point.attrs.NrpCgEthFrameFlowCpaAspec;
13 import org.opendaylight.yang.gen.v1.urn.mef.yang.tapicommon.rev170227.UniversalId;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 /**
22  * Class representing port (replacement for FcPort)
23  *
24  * @author marek.ryznar@amartus.com
25  */
26 public class ServicePort {
27     private static final String pattern = ".+?(?=((((\\d+)/)+)\\d+))";
28     private static final Pattern interface_name_pattern = Pattern.compile(pattern);
29     private static final String default_interface_name = "GigabitEthernet";
30
31     //netconf topology
32     private TopologyId topoId;
33     //represents device ie dev-68 in netconf topology
34     private NodeId nodeId;
35     //defines port
36     private TpId tpId;
37     //defines cTag VLAN ID
38     private Long vlanId=null;
39
40     public ServicePort(TopologyId topoId, NodeId nodeId, TpId tpId){
41         this.topoId = topoId;
42         this.nodeId = nodeId;
43         this.tpId = tpId;
44     }
45
46     public TopologyId getTopology() {
47         return topoId;
48     }
49
50     public void setTopology(TopologyId topoId) {
51         this.topoId = topoId;
52     }
53
54     public NodeId getNode() {
55         return nodeId;
56     }
57
58     public void setNode(NodeId nodeId) {
59         this.nodeId = nodeId;
60     }
61
62     public TpId getTp() {
63         return tpId;
64     }
65
66     public void setTp(TpId tpId) {
67         this.tpId = tpId;
68     }
69
70     public Long getVlanId() {
71         return vlanId;
72     }
73
74     public void setVlanId(Long vlanId) {
75         this.vlanId = vlanId;
76     }
77
78     public static ServicePort toServicePort(EndPoint endPoint, String topologyName){
79         UniversalId sip = endPoint.getEndpoint().getServiceInterfacePoint();
80         TopologyId topologyId = new TopologyId(topologyName);
81         NodeId nodeId = new NodeId(SipHandler.getDeviceName(sip));
82         TpId tpId = new TpId(SipHandler.getPortName(sip));
83         ServicePort servicePort = new ServicePort(topologyId,nodeId,tpId);
84         if(hasVlan(endPoint)){
85             servicePort.setVlanId(Long.valueOf(getVlan(endPoint)));
86         }
87         return servicePort;
88     }
89
90     public static boolean hasVlan(EndPoint endPoint){
91         if( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCgEthFrameFlowCpaAspec()!=null) ){
92             NrpCgEthFrameFlowCpaAspec attr = endPoint.getAttrs().getNrpCgEthFrameFlowCpaAspec();
93             if( (attr.getCeVlanIdList()!=null) && !(attr.getCeVlanIdList().getVlanIdList().isEmpty()) ){
94                 return true;
95             } else {
96                 return false;
97             }
98         } else {
99             return false;
100         }
101     }
102
103     private static int getVlan(EndPoint endPoint){
104         return endPoint.getAttrs().getNrpCgEthFrameFlowCpaAspec().getCeVlanIdList().getVlanIdList().get(0).getVlanId().getValue().intValue();
105     }
106
107     public String getInterfaceName(){
108         TpId tpId = this.getTp();
109         Matcher matcher = interface_name_pattern.matcher(tpId.getValue());
110         return matcher.find() ? matcher.group() : default_interface_name;
111     }
112 }