Bug 5374: Refactor UnimgrUtils class
[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     private EvcUtils() {
47         throw new AssertionError("Instantiating utility class.");
48     }
49
50     /**
51      * Delete EVC data from configuration datastore
52      * @param dataBroker The dataBroker instance to create transactions
53      * @param optionalUni Optional Uni Node
54      */
55     public static void deleteEvcData(DataBroker dataBroker, Optional<Node> optionalUni) {
56         if (optionalUni.isPresent()) {
57             final UniAugmentation uniAugmentation =
58                                 optionalUni
59                                     .get()
60                                     .getAugmentation(UniAugmentation.class);
61             final InstanceIdentifier<Node> ovsdbNodeIid =
62                                               uniAugmentation
63                                              .getOvsdbNodeRef()
64                                              .getValue()
65                                              .firstIdentifierOf(Node.class);
66             final Optional<Node> optionalOvsdNode =
67                     MdsalUtils.readNode(dataBroker,
68                                          LogicalDatastoreType.OPERATIONAL,
69                                          ovsdbNodeIid);
70             if (optionalOvsdNode.isPresent()) {
71                 final Node ovsdbNode = optionalOvsdNode.get();
72                 final OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
73                 for (final ManagedNodeEntry managedNodeEntry: ovsdbNodeAugmentation.getManagedNodeEntry()) {
74                     final InstanceIdentifier<Node> bridgeIid = managedNodeEntry
75                                                              .getBridgeRef()
76                                                              .getValue()
77                                                              .firstIdentifierOf(Node.class);
78                     final Optional<Node> optBridgeNode = MdsalUtils.readNode(dataBroker, bridgeIid);
79                     if (optBridgeNode.isPresent()) {
80                         final Node bridgeNode = optBridgeNode.get();
81                         final InstanceIdentifier<TerminationPoint> iidGreTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
82                                                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
83                         final InstanceIdentifier<TerminationPoint> iidEthTermPoint = UnimgrMapper.getTerminationPointIid(bridgeNode,
84                                                                                         UnimgrConstants.DEFAULT_TUNNEL_IFACE);
85                         MdsalUtils.deleteNode(dataBroker, iidGreTermPoint, LogicalDatastoreType.CONFIGURATION);
86                         MdsalUtils.deleteNode(dataBroker, iidEthTermPoint, LogicalDatastoreType.CONFIGURATION);
87                     }
88                 }
89             }
90         } else {
91             LOG.info("Unable to retrieve UNI from the EVC.");
92         }
93     }
94
95     /**
96      * Retrieve the list of links in the Operational DataStore
97      * @param dataBroker The dataBroker instance to create transactions
98      * @return A list of Links retrieved from the Operational DataStore
99      */
100     public static List<Link> getEvcLinks(DataBroker dataBroker) {
101         final List<Link> evcLinks = new ArrayList<>();
102         final InstanceIdentifier<Topology> evcTopology = UnimgrMapper.getEvcTopologyIid();
103         final Topology topology = MdsalUtils.read(dataBroker,
104                                              LogicalDatastoreType.OPERATIONAL,
105                                              evcTopology);
106         if ((topology != null) && (topology.getLink() != null)) {
107             for (final Link link : topology.getLink()) {
108                 final EvcAugmentation evcAugmentation = link.getAugmentation(EvcAugmentation.class);
109                 if (evcAugmentation != null) {
110                     evcLinks.add(link);
111                 }
112             }
113         }
114         return evcLinks;
115     }
116
117     /**
118      * Updates a specific EVC into a specific DataStore type
119      * @param dataStore The datastore type
120      * @param evcKey The EVC key
121      * @param evcAugmentation The EVC's data
122      * @param sourceUniIid The Source Uni Instance Identifier
123      * @param destinationUniIid The destination Uni Instance Identifier
124      * @param dataBroker The dataBroker instance to create transactions
125      * @return true if evc is updated
126      */
127     public static boolean updateEvcNode(LogicalDatastoreType dataStore,
128                                      InstanceIdentifier<?> evcKey,
129                                      EvcAugmentation evcAugmentation,
130                                      InstanceIdentifier<?> sourceUniIid,
131                                      InstanceIdentifier<?> destinationUniIid,
132                                      DataBroker dataBroker) {
133         final EvcAugmentationBuilder updatedEvcBuilder = new EvcAugmentationBuilder(evcAugmentation);
134         if ((sourceUniIid != null) && (destinationUniIid != null)) {
135             final List<UniSource> sourceList = new ArrayList<UniSource>();
136             final UniSourceKey sourceKey = evcAugmentation.getUniSource().iterator().next().getKey();
137             final short sourceOrder = evcAugmentation.getUniSource().iterator().next().getOrder();
138             final IpAddress sourceIp = evcAugmentation.getUniSource().iterator().next().getIpAddress();
139             final UniSource uniSource = new UniSourceBuilder()
140                                           .setOrder(sourceOrder)
141                                           .setKey(sourceKey)
142                                           .setIpAddress(sourceIp)
143                                           .setUni(sourceUniIid)
144                                           .build();
145             sourceList.add(uniSource);
146             updatedEvcBuilder.setUniSource(sourceList);
147
148             final List<UniDest> destinationList = new ArrayList<UniDest>();
149             final UniDestKey destKey = evcAugmentation.getUniDest().iterator().next().getKey();
150             final short destOrder = evcAugmentation.getUniDest().iterator().next().getOrder();
151             final IpAddress destIp = evcAugmentation.getUniDest().iterator().next().getIpAddress();
152             final UniDest uniDest = new UniDestBuilder()
153                                       .setIpAddress(destIp)
154                                       .setOrder(destOrder)
155                                       .setKey(destKey)
156                                       .setUni(destinationUniIid)
157                                       .build();
158             destinationList.add(uniDest);
159             updatedEvcBuilder.setUniDest(destinationList);
160             final Optional<Link> optionalEvcLink = MdsalUtils.readLink(dataBroker,
161                                                       LogicalDatastoreType.CONFIGURATION,
162                                                       evcKey);
163             if (optionalEvcLink.isPresent()) {
164                 final Link link = optionalEvcLink.get();
165                 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
166                 final LinkBuilder linkBuilder = new LinkBuilder();
167                 linkBuilder.setKey(link.getKey());
168                 linkBuilder.setLinkId(link.getLinkId());
169                 linkBuilder.setDestination(link.getDestination());
170                 linkBuilder.setSource(link.getSource());
171                 linkBuilder.addAugmentation(EvcAugmentation.class, updatedEvcBuilder.build());
172                 transaction.put(dataStore, evcKey.firstIdentifierOf(Link.class), linkBuilder.build());
173                 transaction.submit();
174                 return true;
175             } else {
176                 LOG.info("EvcLink is not present: " + optionalEvcLink.get().getKey());
177             }
178         } else {
179             LOG.info("Invalid instance identifiers for sourceUni and destUni.");
180         }
181         return false;
182     }
183 }