ovs-driver refactored to support new TAPI-based presto NRP model.
[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 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;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 /**
28  * Helper class for OvsDriver activation.
29  *
30  * @author jakub.niezgoda@amartus.com
31  */
32 class OvsActivatorHelper {
33     private List<NullAwareDatastoreGetter<Node>> nodes;
34     private EndPoint endPoint;
35     private String tpName;
36     private Map<String, String> portMap;
37
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.";
40
41
42     private static final Logger LOG = LoggerFactory.getLogger(OvsActivatorHelper.class);
43
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);
49     }
50
51     /**
52      * Returns VLAN Id of the service
53      *
54      * @return Integer with VLAN Id
55      */
56     int getCeVlanId() throws ResourceNotAvailableException {
57
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();
63             } else {
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));
66             }
67         } else {
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));
71         }
72     }
73
74     /**
75      * Returns VLAN Id to be used internally in OvSwitch network
76      *
77      * @return Integer with VLAN Id
78      */
79     int getInternalVlanId() throws ResourceNotAvailableException {
80         VlanUtils vlanUtils = new VlanUtils(nodes);
81         int serviceVlanId = getCeVlanId();
82
83         if (vlanUtils.isVlanInUse(serviceVlanId)) {
84             LOG.debug("VLAN ID = '" + serviceVlanId + "' already in use.");
85             return vlanUtils.generateVlanID();
86         } else {
87             LOG.debug("VLAN ID = '" + serviceVlanId + "' not in use.");
88             return serviceVlanId;
89         }
90     }
91
92     /**
93      * Returns port name in openflow plugin convention (e.g. openflow:1:4)
94      *
95      * @return String with port name
96      */
97     String getOpenFlowPortName() {
98         return portMap.get(tpName);
99     }
100
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);
110                 }
111             }
112         }
113         return portMap;
114     }
115
116     protected static String getPortName(String sip){
117         String[] tab = sip.split(":");
118         return tab[tab.length-1];
119     }
120 }