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