Merge dev/fluorine work across to master
[unimgr.git] / cisco-xr-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / cisco / xr / l2vpn / activator / AbstractL2vpnActivator.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.cisco.xr.l2vpn.activator;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
17 import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.ServicePort;
18 import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.helper.InterfaceHelper;
19 import org.opendaylight.unimgr.mef.nrp.cisco.xr.l2vpn.helper.L2vpnHelper;
20 import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.MountPointHelper;
21 import org.opendaylight.unimgr.mef.nrp.common.ResourceActivator;
22 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.asr9k.policymgr.cfg.rev150518.PolicyManager;
23 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730.InterfaceActive;
24 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730.InterfaceConfigurations;
25 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfiguration;
26 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfigurationKey;
27 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.L2vpn;
28 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.Database;
29 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.XconnectGroups;
30 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.XconnectGroup;
31 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.XconnectGroupKey;
32 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.P2pXconnects;
33 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.P2pXconnect;
34 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.P2pXconnectKey;
35 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.l2vpn.cfg.rev151109.l2vpn.database.xconnect.groups.xconnect.group.p2p.xconnects.p2p.xconnect.Pseudowires;
36 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.xr.types.rev150629.CiscoIosXrString;
37 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev180321.nrp.connectivity.service.end.point.attrs.NrpCarrierEthConnectivityEndPointResource;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import java.util.List;
43
44 import static org.opendaylight.unimgr.mef.nrp.cisco.xr.common.ServicePort.toServicePort;
45
46
47 /**
48  * Abstarct activator of VPLS-based L2 VPN on IOS-XR devices. It is responsible for handling activation and deactivation
49  * process of VPN configuration and it provides generic transaction designated for this purpose.
50  *
51  * @author krzysztof.bijakowski@amartus.com
52  */
53 public abstract class AbstractL2vpnActivator implements ResourceActivator {
54
55     private static final Logger LOG = LoggerFactory.getLogger(AbstractL2vpnActivator.class);
56     private static final String NETCONF_TOPOLODY_NAME = "topology-netconf";
57     private static final long mtu = 1500;
58
59     protected DataBroker dataBroker;
60
61     private MountPointService mountService;
62
63     protected AbstractL2vpnActivator(DataBroker dataBroker, MountPointService mountService) {
64         this.dataBroker = dataBroker;
65         this.mountService = mountService;
66     }
67
68     @Override
69     public void activate(List<EndPoint> endPoints, String serviceId) throws TransactionCommitFailedException {
70         String innerName = getInnerName(serviceId);
71         String outerName = getOuterName(serviceId);
72         ServicePort port = null;
73         ServicePort neighbor = null;
74         for (EndPoint endPoint: endPoints) {
75             if (port==null) {
76                 port = toServicePort(endPoint, NETCONF_TOPOLODY_NAME);
77                 NrpCarrierEthConnectivityEndPointResource attrs = endPoint.getAttrs() == null ? null : endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
78                 if(attrs != null) {
79                     port.setEgressBwpFlow(attrs.getEgressBwpFlow());
80                     port.setIngressBwpFlow(attrs.getIngressBwpFlow());
81
82                 }
83             } else {
84                 neighbor = toServicePort(endPoint, NETCONF_TOPOLODY_NAME);
85             }
86         }
87
88         java.util.Optional<PolicyManager> qosConfig = activateQos(innerName, port);
89         InterfaceConfigurations interfaceConfigurations = activateInterface(port, neighbor, mtu);
90         Pseudowires pseudowires = activatePseudowire(neighbor);
91         XconnectGroups xconnectGroups = activateXConnect(outerName, innerName, port, neighbor, pseudowires);
92         L2vpn l2vpn = activateL2Vpn(xconnectGroups);
93
94         doActivate(port.getNode().getValue(), interfaceConfigurations, l2vpn, qosConfig);
95     }
96
97     @Override
98     public void deactivate(List<EndPoint> endPoints, String serviceId) throws TransactionCommitFailedException {
99         String innerName = getInnerName(serviceId);
100         String outerName = getOuterName(serviceId);
101         ServicePort port = toServicePort(endPoints.stream().findFirst().get(), NETCONF_TOPOLODY_NAME);
102
103         InstanceIdentifier<P2pXconnect> xconnectId = deactivateXConnect(outerName, innerName);
104         InstanceIdentifier<InterfaceConfiguration> interfaceConfigurationId = deactivateInterface(port);
105
106         doDeactivate(port.getNode().getValue(), xconnectId, interfaceConfigurationId);
107     }
108
109     // for now QoS is ignored
110     protected void doActivate(String nodeName,
111                               InterfaceConfigurations interfaceConfigurations,
112                               L2vpn l2vpn,
113                               java.util.Optional<PolicyManager> qosConfig) throws TransactionCommitFailedException {
114
115         Optional<DataBroker> optional = MountPointHelper.getDataBroker(mountService, nodeName);
116         if (!optional.isPresent()) {
117             LOG.error("Could not retrieve MountPoint for {}", nodeName);
118             return;
119         }
120
121         WriteTransaction transaction = optional.get().newWriteOnlyTransaction();
122         transaction.merge(LogicalDatastoreType.CONFIGURATION, InterfaceHelper.getInterfaceConfigurationsId(), interfaceConfigurations);
123         transaction.merge(LogicalDatastoreType.CONFIGURATION, L2vpnHelper.getL2vpnId(), l2vpn);
124         transaction.submit().checkedGet();
125     }
126
127     protected void doDeactivate(String nodeName,
128                                 InstanceIdentifier<P2pXconnect> xconnectId,
129                                 InstanceIdentifier<InterfaceConfiguration> interfaceConfigurationId) throws TransactionCommitFailedException {
130
131         Optional<DataBroker> optional = MountPointHelper.getDataBroker(mountService, nodeName);
132         if (!optional.isPresent()) {
133             LOG.error("Could not retrieve MountPoint for {}", nodeName);
134             return;
135         }
136
137         WriteTransaction transaction = optional.get().newWriteOnlyTransaction();
138         transaction.delete(LogicalDatastoreType.CONFIGURATION, xconnectId);
139         transaction.delete(LogicalDatastoreType.CONFIGURATION, interfaceConfigurationId);
140         transaction.submit().checkedGet();
141     }
142
143     protected abstract java.util.Optional<PolicyManager> activateQos(String name, ServicePort port);
144
145     protected abstract InterfaceConfigurations activateInterface(ServicePort portA, ServicePort portZ, long mtu);
146
147     protected abstract Pseudowires activatePseudowire(ServicePort neighbor);
148
149     protected abstract XconnectGroups activateXConnect(String outerName, String innerName, ServicePort portA, ServicePort portZ, Pseudowires pseudowires);
150
151     protected abstract L2vpn activateL2Vpn(XconnectGroups xconnectGroups);
152
153     private InstanceIdentifier<P2pXconnect> deactivateXConnect(String outerName, String innerName) {
154         return InstanceIdentifier.builder(L2vpn.class)
155                 .child(Database.class)
156                 .child(XconnectGroups.class)
157                 .child(XconnectGroup.class, new XconnectGroupKey(new CiscoIosXrString(outerName)))
158                 .child(P2pXconnects.class).child(P2pXconnect.class, new P2pXconnectKey(new CiscoIosXrString(innerName)))
159                 .build();
160     }
161
162     private InstanceIdentifier<InterfaceConfiguration> deactivateInterface(ServicePort port) {
163         return InstanceIdentifier.builder(InterfaceConfigurations.class)
164                 .child(InterfaceConfiguration.class, new InterfaceConfigurationKey(new InterfaceActive("act"), InterfaceHelper.getInterfaceName(port)))
165                 .build();
166     }
167
168     protected abstract String getInnerName(String serviceId);
169     protected abstract String getOuterName(String serviceId);
170 }