manually cherry pick this patch https://git.opendaylight.org/gerrit/#/c/34579/
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / utils / EvcUtils.java
1 /*
2  * Copyright (c) 2016 CableLabs 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
9 package org.opendaylight.unimgr.utils;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.unimgr.impl.UnimgrConstants;
18 import org.opendaylight.unimgr.impl.UnimgrMapper;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Optional;
41
42 public class EvcUtils {
43
44     private static final Logger LOG = LoggerFactory.getLogger(EvcUtils.class);
45
46     /**
47      * Delete EVC data from configuration datastore
48      * @param dataBroker The dataBroker instance to create transactions
49      * @param optionalUni Optional Uni Node
50      */
51     public static void deleteEvcData(final DataBroker dataBroker, final Optional<Node> optionalUni) {
52         if (optionalUni.isPresent()) {
53             final UniAugmentation uniAugmentation =
54                                 optionalUni
55                                     .get()
56                                     .getAugmentation(UniAugmentation.class);
57             final InstanceIdentifier<Node> ovsdbNodeIid =
58                                               uniAugmentation
59                                              .getOvsdbNodeRef()
60                                              .getValue()
61                                              .firstIdentifierOf(Node.class);
62             final Optional<Node> optionalOvsdNode =
63                     MdsalUtils.readNode(dataBroker,
64                                          LogicalDatastoreType.OPERATIONAL,
65                                          ovsdbNodeIid);
66             if (optionalOvsdNode.isPresent()) {
67                 final Node ovsdbNode = optionalOvsdNode.get();
68                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
69                 for (final ManagedNodeEntry managedNodeEntry: ovsdbNodeAugmentation.getManagedNodeEntry()) {
70                     final InstanceIdentifier<Node> bridgeIid = managedNodeEntry
71                                                              .getBridgeRef()
72                                                              .getValue()
73                                                              .firstIdentifierOf(Node.class);
74                     final Optional<Node> optBridgeNode = MdsalUtils.readNode(dataBroker, bridgeIid);
75                     if (optBridgeNode.isPresent()) {
76                         final Node bridgeNode = optBridgeNode.get();
77                         final InstanceIdentifier<TerminationPoint> iidGreTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
78                                                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
79                         final InstanceIdentifier<TerminationPoint> iidEthTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
80                                                                                         UnimgrConstants.DEFAULT_TUNNEL_IFACE);
81                         MdsalUtils.deleteNode(dataBroker, iidGreTermPoint, LogicalDatastoreType.CONFIGURATION);
82                         MdsalUtils.deleteNode(dataBroker, iidEthTermPoint, LogicalDatastoreType.CONFIGURATION);
83                     }
84                 }
85             }
86         } else {
87             LOG.info("Unable to retrieve UNI from the EVC.");
88         }
89     }
90
91     /**
92      * Retrieve the list of links in the Operational DataStore
93      * @param dataBroker The dataBroker instance to create transactions
94      * @return A list of Links retrieved from the Operational DataStore
95      */
96     public static List<Link> getEvcLinks(final DataBroker dataBroker) {
97         final List<Link> evcLinks = new ArrayList<>();
98         final InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
99         final Topology topology = MdsalUtils.read(dataBroker,
100                                              LogicalDatastoreType.OPERATIONAL,
101                                              evcTopology);
102         if ((topology != null) && (topology.getLink() != null)) {
103             for (final Link link : topology.getLink()) {
104                 final EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
105                 if (evcAugmentation != null) {
106                     evcLinks.add(link);
107                 }
108             }
109         }
110         return evcLinks;
111     }
112
113     /**
114      * Updates a specific EVC into a specific DataStore type
115      * @param dataStore The datastore type
116      * @param evcKey The EVC key
117      * @param evcAugmentation The EVC's data
118      * @param sourceUniIid The Source Uni Instance Identifier
119      * @param destinationUniIid The destination Uni Instance Identifier
120      * @param dataBroker The dataBroker instance to create transactions
121      * @return true if evc is updated
122      */
123     public static boolean updateEvcNode(final LogicalDatastoreType dataStore,
124                                      final InstanceIdentifier<?> evcKey,
125                                      final EvcAugmentation evcAugmentation,
126                                      final InstanceIdentifier<?> sourceUniIid,
127                                      final InstanceIdentifier<?> destinationUniIid,
128                                      final DataBroker dataBroker) {
129         final EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
130         if ((sourceUniIid != null) && (destinationUniIid != null)) {
131             final List<UniSource> sourceList = new ArrayList<UniSource>();
132             final UniSource evcUniSource = evcAugmentation.getUniSource().iterator().next();
133             final UniSourceKey sourceKey = evcUniSource.getKey();
134             final short sourceOrder = evcUniSource.getOrder();
135             final IpAddress sourceIp = evcUniSource.getIpAddress();
136             final UniSource uniSource = new UniSourceBuilder()
137                                           .setOrder(sourceOrder)
138                                           .setKey(sourceKey)
139                                           .setIpAddress(sourceIp)
140                                           .setUni(sourceUniIid)
141                                           .build();
142             sourceList.add(uniSource);
143             updatedEvcBuilder.setUniSource(sourceList);
144
145             final List<UniDest> destinationList = new ArrayList<UniDest>();
146             final UniDest evcUniDest = evcAugmentation.getUniDest().iterator().next();
147             final UniDestKey destKey = evcUniDest.getKey();
148             final short destOrder = evcUniDest.getOrder();
149             final IpAddress destIp = evcUniDest.getIpAddress();
150             final UniDest uniDest = new UniDestBuilder()
151                                       .setIpAddress(destIp)
152                                       .setOrder(destOrder)
153                                       .setKey(destKey)
154                                       .setUni(destinationUniIid)
155                                       .build();
156             destinationList.add(uniDest);
157             updatedEvcBuilder.setUniDest(destinationList);
158             final Optional<Link> optionalEvcLink = MdsalUtils.readLink(dataBroker,
159                                                       LogicalDatastoreType.CONFIGURATION,
160                                                       evcKey);
161             if (optionalEvcLink.isPresent()) {
162                 final Link link = optionalEvcLink.get();
163                 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
164                 final LinkBuilder linkBuilder = new LinkBuilder();
165                 linkBuilder.setKey(link.getKey());
166                 linkBuilder.setLinkId(link.getLinkId());
167                 linkBuilder.setDestination(link.getDestination());
168                 linkBuilder.setSource(link.getSource());
169                 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
170                 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
171                 transaction.submit();
172                 return true;
173             } else {
174                 LOG.info("EvcLink is not present: " + optionalEvcLink.get().getKey());
175             }
176         } else {
177             LOG.info("Invalid instance identifiers for sourceUni and destUni.");
178         }
179         return false;
180     }
181
182     private EvcUtils() {
183         throw new AssertionError("Instantiating utility class.");
184     }
185 }