2 * Copyright (c) 2016 Cisco Systems Inc and others. All rights reserved.
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
8 package org.opendaylight.unimgr.mef.nrp.ovs.activator;
10 import java.util.List;
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;
25 import com.google.common.collect.BiMap;
26 import com.google.common.collect.HashBiMap;
29 * Helper class for OvsDriver activation.
31 * @author jakub.niezgoda@amartus.com
33 class OvsActivatorHelper {
34 private List<NullAwareDatastoreGetter<Node>> nodes;
35 private EndPoint endPoint;
36 private String tpName;
37 private BiMap<String, String> portMap;
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.";
44 private static final Logger LOG = LoggerFactory.getLogger(OvsActivatorHelper.class);
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);
54 * Returns VLAN Id of the service
56 * @return Integer with VLAN Id
58 int getCeVlanId() throws ResourceNotAvailableException {
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();
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));
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));
77 * Returns VLAN Id to be used internally in OvSwitch network
79 * @return Integer with VLAN Id
81 int getInternalVlanId() throws ResourceNotAvailableException {
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();
90 // LOG.debug("VLAN ID = '" + serviceVlanId + "' not in use.");
91 // return serviceVlanId;
96 * Returns port name in openflow plugin naming convention (e.g. openflow:1:4)
98 * @return String with openflow port name
100 String getOpenFlowPortName() {
101 return portMap.get(tpName);
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
109 String getTpNameFromOpenFlowPortName(String openFlowPortName) {
110 return portMap.inverse().get(openFlowPortName);
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);
128 protected static String getPortName(String sip) {
129 String[] tab = sip.split(":");
130 return tab[tab.length-1];
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();
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));
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();
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));