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 org.opendaylight.unimgr.mef.nrp.api.EndPoint;
11 import org.opendaylight.unimgr.mef.nrp.common.ResourceNotAvailableException;
12 import org.opendaylight.unimgr.mef.nrp.ovs.exception.VlanNotSetException;
13 import org.opendaylight.unimgr.mef.nrp.ovs.transaction.TopologyTransaction;
14 import org.opendaylight.unimgr.mef.nrp.ovs.util.VlanUtils;
15 import org.opendaylight.unimgr.utils.NullAwareDatastoreGetter;
16 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp_interface.rev170227.nrp.create.connectivity.service.end.point.attrs.NrpCgEthFrameFlowCpaAspec;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.util.HashMap;
24 import java.util.List;
28 * Helper class for OvsDriver activation.
30 * @author jakub.niezgoda@amartus.com
32 class OvsActivatorHelper {
33 private List<NullAwareDatastoreGetter<Node>> nodes;
34 private EndPoint endPoint;
35 private String tpName;
36 private Map<String, String> portMap;
38 private static final String CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE = "C-Tag VLAN Id not set for End Point '%s'.";
39 private static final String ATTRS_NOT_SET_ERROR_MESSAGE = "End Point '%s' does not have '%s' set.";
42 private static final Logger LOG = LoggerFactory.getLogger(OvsActivatorHelper.class);
44 OvsActivatorHelper(TopologyTransaction topologyTransaction, EndPoint endPoint) {
45 this.nodes = topologyTransaction.readNodes();
46 this.endPoint = endPoint;
47 tpName = getPortName(endPoint.getEndpoint().getServiceInterfacePoint().getValue());
48 this.portMap = createPortMap(nodes);
52 * Returns VLAN Id of the service
54 * @return Integer with VLAN Id
56 int getCeVlanId() throws ResourceNotAvailableException {
58 if( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCgEthFrameFlowCpaAspec()!=null) ){
59 NrpCgEthFrameFlowCpaAspec attr = endPoint.getAttrs().getNrpCgEthFrameFlowCpaAspec();
60 if( (attr.getCeVlanIdList()!=null) && !(attr.getCeVlanIdList().getVlanIdList().isEmpty()) ){
61 //for now we support only one CE VLAN
62 return attr.getCeVlanIdList().getVlanIdList().get(0).getVlanId().getValue().intValue();
64 LOG.warn(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
65 throw new VlanNotSetException(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
68 String className = NrpCgEthFrameFlowCpaAspec.class.toString();
69 LOG.warn(String.format(ATTRS_NOT_SET_ERROR_MESSAGE, tpName, className));
70 throw new ResourceNotAvailableException(String.format(ATTRS_NOT_SET_ERROR_MESSAGE, tpName, className));
75 * Returns VLAN Id to be used internally in OvSwitch network
77 * @return Integer with VLAN Id
79 int getInternalVlanId() throws ResourceNotAvailableException {
80 VlanUtils vlanUtils = new VlanUtils(nodes);
81 int serviceVlanId = getCeVlanId();
83 if (vlanUtils.isVlanInUse(serviceVlanId)) {
84 LOG.debug("VLAN ID = '" + serviceVlanId + "' already in use.");
85 return vlanUtils.generateVlanID();
87 LOG.debug("VLAN ID = '" + serviceVlanId + "' not in use.");
93 * Returns port name in openflow plugin convention (e.g. openflow:1:4)
95 * @return String with port name
97 String getOpenFlowPortName() {
98 return portMap.get(tpName);
101 private Map<String, String> createPortMap(List<NullAwareDatastoreGetter<Node>> nodes) {
102 Map<String, String> portMap = new HashMap<>();
103 for (NullAwareDatastoreGetter<Node> node : nodes) {
104 if (node.get().isPresent()){
105 for (NodeConnector nodeConnector : node.get().get().getNodeConnector()) {
106 String ofName = nodeConnector.getId().getValue();
107 FlowCapableNodeConnector flowCapableNodeConnector = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
108 String name = flowCapableNodeConnector.getName();
109 portMap.put(name, ofName);
116 protected static String getPortName(String sip){
117 String[] tab = sip.split(":");
118 return tab[tab.length-1];