2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.unimgr.mef.netvirt;
11 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
18 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.uni.PhysicalLayers;
19 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.uni.physical.layers.Links;
20 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.uni.physical.layers.links.Link;
21 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.evc.choice.evc.unis.Uni;
22 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.evc.choice.evc.unis.uni.EvcUniCeVlans;
23 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.evc.choice.evc.unis.uni.evc.uni.ce.vlans.EvcUniCeVlan;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.CheckedFuture;
34 public class EvcUniUtils {
36 private static final Logger logger = LoggerFactory.getLogger(EvcUniUtils.class);
38 public static Link getLink(DataBroker dataBroker, String uniId) {
39 Optional<org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni> optional = MdsalUtils
40 .read(dataBroker, LogicalDatastoreType.CONFIGURATION, MefUtils.getUniInstanceIdentifier(uniId));
42 if (!optional.isPresent()) {
43 logger.error("A matching Uni doesn't exist for EvcUni {}", uniId);
47 org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uni = optional
50 PhysicalLayers physicalLayers = uni.getPhysicalLayers();
51 if (physicalLayers == null) {
52 logger.warn("Uni {} is missing PhysicalLayers", uniId);
56 Links links = physicalLayers.getLinks();
57 if (links == null || links.getLink() == null) {
58 logger.warn("Uni {} is has no links", uniId);
62 Link link = links.getLink().get(0);
66 public static void removeUni(DataBroker dataBroker, Uni data) {
68 String uniId = data.getUniId().getValue();
69 WriteTransaction tx = createTransaction(dataBroker);
71 logger.info("Removing trunk {}", uniId);
73 NetvirtUtils.delete(uniId, tx);
75 Optional<List<EvcUniCeVlan>> ceVlansOptional = getCeVlans(data);
76 if (!ceVlansOptional.isPresent()) {
80 removeTrunkMemberInterfaces(uniId, ceVlansOptional.get(), tx);
81 commitTransaction(tx);
82 } catch (final Exception e) {
83 logger.error("Remove uni failed !", e);
87 public static void addUni(DataBroker dataBroker, Uni data) {
89 String uniId = data.getUniId().getValue();
90 WriteTransaction tx = createTransaction(dataBroker);
91 Link link = EvcUniUtils.getLink(dataBroker, uniId);
92 String interfaceName = uniId;
93 addTrunkInterface(interfaceName, MefUtils.getTrunkParentName(link), tx);
95 Optional<List<EvcUniCeVlan>> ceVlansOptional = getCeVlans(data);
96 if (ceVlansOptional.isPresent()) {
97 addTrunkMemberInterfaces(interfaceName, ceVlansOptional.get(), tx);
100 commitTransaction(tx);
101 } catch (final Exception e) {
102 logger.error("Add uni failed !", e);
106 private static void addTrunkInterface(String interfaceName, String parentInterfaceName, WriteTransaction tx) {
107 logger.info("Adding VLAN trunk {} ParentRef {}", interfaceName, parentInterfaceName);
108 Interface trunkInterface = NetvirtUtils.createTrunkInterface(interfaceName, parentInterfaceName);
109 NetvirtUtils.write(trunkInterface, tx);
112 private static void addTrunkMemberInterfaces(String parentInterfaceName, Iterable<EvcUniCeVlan> ceVlans,
113 WriteTransaction tx) {
114 for (EvcUniCeVlan ceVlan : ceVlans) {
115 Object vid = ceVlan.getVid();
116 if (!(vid instanceof Long)) {
117 String errorMessage = String.format("vlan id {} cannot be cast to Long", vid);
118 logger.error(errorMessage);
119 throw new UnsupportedOperationException(errorMessage);
122 Long vlanId = (Long) vid;
123 String interfaceName = NetvirtUtils.getInterfaceNameForVlan(parentInterfaceName, vlanId.toString());
124 logger.info("Adding VLAN trunk-member {} ParentRef {}", interfaceName, parentInterfaceName);
125 Interface trunkMemberInterface = NetvirtUtils.createTrunkMemberInterface(interfaceName, parentInterfaceName,
127 NetvirtUtils.write(trunkMemberInterface, tx);
131 private static void removeTrunkMemberInterfaces(String parentInterfaceName, Iterable<EvcUniCeVlan> ceVlans,
132 WriteTransaction tx) {
133 for (EvcUniCeVlan ceVlan : ceVlans) {
134 Object vid = ceVlan.getVid();
135 if (!(vid instanceof Long)) {
136 String errorMessage = String.format("vlan id {} cannot be cast to Long", vid);
137 logger.error(errorMessage);
138 throw new UnsupportedOperationException(errorMessage);
141 Long vlanId = (Long) vid;
142 String interfaceName = NetvirtUtils.getInterfaceNameForVlan(parentInterfaceName, vlanId.toString());
143 logger.info("Removing VLAN trunk-member {}", interfaceName);
144 NetvirtUtils.delete(interfaceName, tx);
148 private static WriteTransaction createTransaction(DataBroker dataBroker) {
149 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
153 private static void commitTransaction(WriteTransaction tx) {
155 CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
157 } catch (Exception e) {
158 logger.error("failed to commit transaction due to exception ", e);
162 private static Optional<List<EvcUniCeVlan>> getCeVlans(Uni uni) {
163 EvcUniCeVlans ceVlans = uni.getEvcUniCeVlans();
164 if (ceVlans == null) {
165 return Optional.absent();
168 return Optional.fromNullable(ceVlans.getEvcUniCeVlan());