Streamlined UnimgrMapper, cleaned up the code
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / EvcCreateCommand.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.unimgr.command;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.unimgr.impl.UnimgrConstants;
16 import org.opendaylight.unimgr.impl.UnimgrMapper;
17 import org.opendaylight.unimgr.impl.UnimgrUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Optional;
27
28 public class EvcCreateCommand extends AbstractCreateCommand {
29
30     private static final Logger LOG = LoggerFactory.getLogger(EvcCreateCommand.class);
31
32     public EvcCreateCommand(DataBroker dataBroker,
33                             Map<InstanceIdentifier<?>, DataObject> changes) {
34         super.dataBroker = dataBroker;
35         super.changes = changes;
36     }
37
38     @SuppressWarnings("unchecked")
39     @Override
40     public void execute() {
41         for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
42             if (created.getValue() != null && created.getValue() instanceof EvcAugmentation) {
43                 EvcAugmentation evc = (EvcAugmentation) created.getValue();
44                 LOG.info("New EVC created, source IP: {} destination IP {}.",
45                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
46                         evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
47                 // For now, we assume that there is 1 uni per source/destination
48                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
49                     LOG.error("Destination UNI cannot be null.");
50                     break;
51                 }
52                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
53                     LOG.error("Source UNI cannot be null.");
54                     break;
55                 }
56                 InstanceIdentifier<Node> sourceUniIid;
57                 InstanceIdentifier<Node> destinationUniIid;
58                 if (evc.getUniSource().iterator().next().getUni() != null) {
59                     sourceUniIid = (InstanceIdentifier<Node>) evc.getUniSource().iterator().next().getUni();
60                 } else {
61                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker, evc.getUniSource().iterator().next().getIpAddress());
62                 }
63                 if (evc.getUniDest().iterator().next().getUni() != null) {
64                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker, evc.getUniDest().iterator().next().getIpAddress());;
65                 } else {
66                     destinationUniIid = (InstanceIdentifier<Node>) evc.getUniDest().iterator().next().getUni();
67                 }
68                 // The user has specified the instance identifier of the
69                 // uni source and uni destination
70                 Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
71                                                                         LogicalDatastoreType.CONFIGURATION,
72                                                                         sourceUniIid);
73                 Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
74                                                                              LogicalDatastoreType.CONFIGURATION,
75                                                                              destinationUniIid);
76                 Node uniSource;
77                 Node uniDestination;
78                 // Retrieve the source and destination Unis
79                 if (!optionalUniSource.isPresent() || !optionalUniDestination.isPresent()) {
80                     LOG.info("Unable to retrieve the Source and/or Destination Unis.");
81                     break;
82                 } else {
83                     uniSource = optionalUniSource.get();
84                     uniDestination = optionalUniDestination.get();
85                 }
86                 // Set source and destination
87                 InstanceIdentifier<Node> sourceBridgeIid =
88                         UnimgrMapper.getOvsdbBridgeNodeIid(uniSource.getNodeId(),
89                                                            UnimgrConstants.DEFAULT_BRIDGE_NAME);
90                 Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
91                                                                        LogicalDatastoreType.CONFIGURATION,
92                                                                        sourceBridgeIid);
93                 InstanceIdentifier<Node> destinationBridgeIid =
94                         UnimgrMapper.getOvsdbBridgeNodeIid(uniSource.getNodeId(),
95                                                            UnimgrConstants.DEFAULT_BRIDGE_NAME);
96                 Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
97                                                                             LogicalDatastoreType.CONFIGURATION,
98                                                                             destinationBridgeIid);
99                 if (!optionalSourceBr.isPresent() || !optionalDestinationBr.isPresent()) {
100                     LOG.info("Unable to retrieve the source and/or destination bridge.");
101                     break;
102                 } else {
103                     Node sourceBr = optionalSourceBr.get();
104                     Node destinationBr = optionalDestinationBr.get();
105                     UnimgrUtils.createTerminationPointNode(dataBroker,
106                                                            uniSource.getAugmentation(UniAugmentation.class),
107                                                            sourceBr,
108                                                            UnimgrConstants.DEFAULT_BRIDGE_NAME,
109                                                            UnimgrConstants.DEFAULT_TUNNEL_IFACE,
110                                                            UnimgrConstants.DEFAULT_GRE_NAME);
111                     UnimgrUtils.createGreTunnel(dataBroker,
112                                                 uniSource.getAugmentation(UniAugmentation.class),
113                                                 uniDestination.getAugmentation(UniAugmentation.class),
114                                                 sourceBr,
115                                                 UnimgrConstants.DEFAULT_BRIDGE_NAME,
116                                                 "gre0");
117                     UnimgrUtils.createTerminationPointNode(dataBroker,
118                                                            uniSource.getAugmentation(UniAugmentation.class),
119                                                            destinationBr,
120                                                            UnimgrConstants.DEFAULT_BRIDGE_NAME,
121                                                            UnimgrConstants.DEFAULT_TUNNEL_IFACE,
122                                                            UnimgrConstants.DEFAULT_GRE_NAME);
123                     UnimgrUtils.createGreTunnel(dataBroker,
124                                                 uniDestination.getAugmentation(UniAugmentation.class),
125                                                 uniSource.getAugmentation(UniAugmentation.class),
126                                                 destinationBr,
127                                                 UnimgrConstants.DEFAULT_BRIDGE_NAME,
128                                                 "gre0");
129                 }
130             }
131         }
132     }
133
134 }