Migrate to 171221 revision of MEF NRP API
[unimgr.git] / ovs-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / ovs / activator / OvsActivatorHelper.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.ovs.activator;
9
10 import java.util.List;
11
12 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
13 import org.opendaylight.unimgr.mef.nrp.common.ResourceNotAvailableException;
14 import org.opendaylight.unimgr.mef.nrp.ovs.exception.VlanNotSetException;
15 import org.opendaylight.unimgr.mef.nrp.ovs.transaction.TopologyTransaction;
16 import org.opendaylight.unimgr.utils.NullAwareDatastoreGetter;
17 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrm.connectivity.rev171221.carrier.eth.connectivity.end.point.resource.IngressBwpFlow;
18 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev171221.nrp.connectivity.service.end.point.attrs.NrpCarrierEthConnectivityEndPointResource;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.collect.BiMap;
26 import com.google.common.collect.HashBiMap;
27
28 /**
29  * Helper class for OvsDriver activation.
30  *
31  * @author jakub.niezgoda@amartus.com
32  */
33 class OvsActivatorHelper {
34     private List<NullAwareDatastoreGetter<Node>> nodes;
35     private EndPoint endPoint;
36     private String tpName;
37     private BiMap<String, String> portMap;
38
39     private static final String CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE = "C-Tag VLAN Id not set for End Point '%s'.";
40     private static final String INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE = "Ingress bwp flow is not set for End Point '%s'.";
41     private static final String ATTRS_NOT_SET_ERROR_MESSAGE = "End Point '%s' does not have '%s' set.";
42
43
44     private static final Logger LOG = LoggerFactory.getLogger(OvsActivatorHelper.class);
45
46     OvsActivatorHelper(TopologyTransaction topologyTransaction, EndPoint endPoint) {
47         this.nodes = topologyTransaction.readNodes();
48         this.endPoint = endPoint;
49         tpName = getPortName(endPoint.getEndpoint().getServiceInterfacePoint().getValue());
50         this.portMap = createPortMap(nodes);
51     }
52
53     /**
54      * Returns VLAN Id of the service
55      *
56      * @return Integer with VLAN Id
57      */
58     int getCeVlanId() throws ResourceNotAvailableException {
59
60         if ( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() != null) ) {
61             NrpCarrierEthConnectivityEndPointResource attr = endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
62             if ( (attr.getCeVlanIdListAndUntag()!=null) && !(attr.getCeVlanIdListAndUntag().getVlanId().isEmpty()) ) {
63                 //for now we support only one CE VLAN
64                 return attr.getCeVlanIdListAndUntag().getVlanId().get(0).getVlanId().getValue().intValue();
65             } else {
66                 LOG.warn(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
67                 throw new VlanNotSetException(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
68             }
69         } else {
70             String className = NrpCarrierEthConnectivityEndPointResource.class.toString();
71             LOG.warn(String.format(ATTRS_NOT_SET_ERROR_MESSAGE, tpName, className));
72             throw new ResourceNotAvailableException(String.format(ATTRS_NOT_SET_ERROR_MESSAGE, tpName, className));
73         }
74     }
75
76     /**
77      * Returns VLAN Id to be used internally in OvSwitch network
78      *
79      * @return Integer with VLAN Id
80      */
81     int getInternalVlanId() throws ResourceNotAvailableException {
82
83         return getCeVlanId();
84 //              VlanUtils vlanUtils = new VlanUtils(nodes);
85 //              Disable VLAN pool, refactor in the future
86 //        if (vlanUtils.isVlanInUse(serviceVlanId)) {
87 //            LOG.debug("VLAN ID = '" + serviceVlanId + "' already in use.");
88 //            return vlanUtils.generateVlanID();
89 //        } else {
90 //            LOG.debug("VLAN ID = '" + serviceVlanId + "' not in use.");
91 //            return serviceVlanId;
92 //        }
93     }
94
95     /**
96      * Returns port name in openflow plugin naming convention (e.g. openflow:1:4)
97      *
98      * @return String with openflow port name
99      */
100     String getOpenFlowPortName() {
101         return portMap.get(tpName);
102     }
103
104     /**
105      * Returns port name for specifiec port name in openflow convention
106      * @param openFlowPortName port in openflow plugin naming convention
107      * @return String with port name
108      */
109     String getTpNameFromOpenFlowPortName(String openFlowPortName) {
110         return portMap.inverse().get(openFlowPortName);
111     }
112
113     private BiMap<String, String> createPortMap(List<NullAwareDatastoreGetter<Node>> nodes) {
114         BiMap<String, String> portMap = HashBiMap.create();
115         for (NullAwareDatastoreGetter<Node> node : nodes) {
116             if (node.get().isPresent()) {
117                 for (NodeConnector nodeConnector : node.get().get().getNodeConnector()) {
118                     String ofName = nodeConnector.getId().getValue();
119                     FlowCapableNodeConnector flowCapableNodeConnector = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
120                     String name = flowCapableNodeConnector.getName();
121                     portMap.put(name, ofName);
122                 }
123             }
124         }
125         return portMap;
126     }
127
128     protected static String getPortName(String sip) {
129         String[] tab = sip.split(":");
130         return tab[tab.length-1];
131     }
132
133         public long getQosMinRate() throws ResourceNotAvailableException {
134                 if ( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() != null) ) {
135                         NrpCarrierEthConnectivityEndPointResource attr = endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
136                         IngressBwpFlow ingressBwpFlow = attr.getIngressBwpFlow();
137                         if(ingressBwpFlow != null) {
138                                 //TODO add validation
139                                 return ingressBwpFlow.getCir().getValue();
140                         } else {
141                 LOG.warn(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
142                 throw new ResourceNotAvailableException(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
143                         }
144                 }
145                 return 0;
146         }
147
148         public long getQosMaxRate() throws ResourceNotAvailableException {
149                 if ( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() != null) ) {
150                         NrpCarrierEthConnectivityEndPointResource attr = endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
151                         IngressBwpFlow ingressBwpFlow = attr.getIngressBwpFlow();
152                         if(ingressBwpFlow != null) {
153                                 //TODO add validation
154                                 return ingressBwpFlow.getCir().getValue() + ingressBwpFlow.getEir().getValue();
155                         } else {
156                 LOG.warn(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
157                 throw new ResourceNotAvailableException(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
158                         }
159                 }
160                 return 0;
161         }
162 }