Fix gre tunnel connectivity
[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     @Override
39     public void execute() {
40         for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
41             if (created.getValue() != null && created.getValue() instanceof EvcAugmentation) {
42                 EvcAugmentation evc = (EvcAugmentation) created.getValue();
43                 InstanceIdentifier<?> evcKey = created.getKey();
44                 // For now, we assume that there is 1 uni per source/destination
45                 if (evc.getUniDest() == null || evc.getUniDest().isEmpty()) {
46                     LOG.error("Destination UNI cannot be null.");
47                     break;
48                 }
49                 if (evc.getUniSource() == null || evc.getUniSource().isEmpty()) {
50                     LOG.error("Source UNI cannot be null.");
51                     break;
52                 }
53                 LOG.trace("New EVC created, source IP: {} destination IP {}.",
54                         evc.getUniSource().iterator().next().getIpAddress().getIpv4Address(),
55                         evc.getUniDest().iterator().next().getIpAddress().getIpv4Address());
56                 InstanceIdentifier<Node> sourceUniIid;
57                 InstanceIdentifier<Node> destinationUniIid;
58                 //FIXME we are assuming that there is only 1 UNI source and destination
59                 // per evc
60                 InstanceIdentifier<?> iidSource = evc.getUniSource().iterator().next().getUni();
61                 if (iidSource != null) {
62                     sourceUniIid = iidSource.firstIdentifierOf(Node.class);
63                 } else {
64                     sourceUniIid = UnimgrMapper.getUniIid(dataBroker,
65                                                           evc.getUniSource().iterator().next().getIpAddress(),
66                                                           LogicalDatastoreType.OPERATIONAL);
67                 }
68                 InstanceIdentifier<?> iidDest = evc.getUniDest().iterator().next().getUni();
69                 if (iidDest != null) {
70                     destinationUniIid = iidDest.firstIdentifierOf(Node.class);
71                 } else {
72                     destinationUniIid = UnimgrMapper.getUniIid(dataBroker,
73                                                                evc.getUniDest().iterator().next().getIpAddress(),
74                                                                LogicalDatastoreType.OPERATIONAL);
75                 }
76                 Optional<Node> optionalUniSource = UnimgrUtils.readNode(dataBroker,
77                                                                         LogicalDatastoreType.OPERATIONAL,
78                                                                         sourceUniIid);
79                 Optional<Node> optionalUniDestination = UnimgrUtils.readNode(dataBroker,
80                                                                              LogicalDatastoreType.OPERATIONAL,
81                                                                              destinationUniIid);
82                 Node uniSource;
83                 Node uniDestination;
84                 // Retrieve the source and destination Unis
85                 if (optionalUniSource.isPresent() && optionalUniDestination.isPresent()) {
86                     uniSource = optionalUniSource.get();
87                     uniDestination = optionalUniDestination.get();
88                     // Set source and destination
89                     UniAugmentation sourceUniAugmentation =
90                                         uniSource.getAugmentation(UniAugmentation.class);
91                     UniAugmentation destinationUniAugmentation =
92                                         uniDestination.getAugmentation(UniAugmentation.class);
93                     Optional<Node> optionalSourceOvsdbNode =
94                                        UnimgrUtils.readNode(dataBroker,
95                                                             LogicalDatastoreType.OPERATIONAL,
96                                                             sourceUniAugmentation
97                                                                 .getOvsdbNodeRef()
98                                                                 .getValue());
99                     Optional<Node> optionalDestinationOvsdbNode =
100                                        UnimgrUtils.readNode(dataBroker,
101                                                             LogicalDatastoreType.OPERATIONAL,
102                                                             destinationUniAugmentation
103                                                                 .getOvsdbNodeRef()
104                                                                 .getValue());
105                     if (optionalSourceOvsdbNode.isPresent() && optionalDestinationOvsdbNode.isPresent()) {
106                         InstanceIdentifier<Node> sourceBridgeIid =
107                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalSourceOvsdbNode.get());
108                         Optional<Node> optionalSourceBr = UnimgrUtils.readNode(dataBroker,
109                                                                                LogicalDatastoreType.OPERATIONAL,
110                                                                                sourceBridgeIid);
111                         InstanceIdentifier<Node> destinationBridgeIid =
112                                 UnimgrMapper.getOvsdbBridgeNodeIid(optionalDestinationOvsdbNode.get());
113                         Optional<Node> optionalDestinationBr = UnimgrUtils.readNode(dataBroker,
114                                                                                     LogicalDatastoreType.OPERATIONAL,
115                                                                                     destinationBridgeIid);
116                         Node sourceBr;
117                         Node destinationBr;
118                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
119                             sourceBr = optionalSourceBr.get();
120                             destinationBr = optionalDestinationBr.get();
121                             UnimgrUtils.createTerminationPointNode(dataBroker,
122                                                                    uniSource.getAugmentation(UniAugmentation.class),
123                                                                    sourceBr,
124                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
125                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
126                             UnimgrUtils.createGreTunnel(dataBroker,
127                                                         uniSource.getAugmentation(UniAugmentation.class),
128                                                         uniDestination.getAugmentation(UniAugmentation.class),
129                                                         sourceBr,
130                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
131                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
132                             UnimgrUtils.createTerminationPointNode(dataBroker,
133                                                                    uniSource.getAugmentation(UniAugmentation.class),
134                                                                    destinationBr,
135                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
136                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
137                             UnimgrUtils.createGreTunnel(dataBroker,
138                                                         uniDestination.getAugmentation(UniAugmentation.class),
139                                                         uniSource.getAugmentation(UniAugmentation.class), destinationBr,
140                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
141                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
142                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
143                                                       evcKey,
144                                                       evc,
145                                                       sourceUniIid,
146                                                       destinationUniIid,
147                                                       dataBroker);
148                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
149                                                       evcKey,
150                                                       evc,
151                                                       sourceUniIid,
152                                                       destinationUniIid,
153                                                       dataBroker);
154                         } else {
155                             LOG.info("Unable to retrieve the source and/or destination bridge.");
156                         }
157                     } else {
158                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
159                     }
160                 } else {
161                     LOG.info("Unable to retrieve the source and/or destination Uni.");
162                 }
163             }
164         }
165     }
166
167 }