FIXED Create EVC
[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.info("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                         //update ovsdb qos-entry and queues with max-rate to match evc ingress BW
117                         UnimgrUtils.updateMaxRate(dataBroker, sourceUniAugmentation, destinationUniAugmentation, evc);
118                         Node sourceBr;
119                         Node destinationBr;
120                         if (optionalSourceBr.isPresent() && optionalDestinationBr.isPresent()) {
121                             sourceBr = optionalSourceBr.get();
122                             destinationBr = optionalDestinationBr.get();
123                             UnimgrUtils.createTerminationPointNode(dataBroker,
124                                                                    uniSource.getAugmentation(UniAugmentation.class),
125                                                                    sourceBr,
126                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
127                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
128                             UnimgrUtils.createGreTunnel(dataBroker,
129                                                         uniSource.getAugmentation(UniAugmentation.class),
130                                                         uniDestination.getAugmentation(UniAugmentation.class),
131                                                         sourceBr,
132                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
133                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
134                             UnimgrUtils.createTerminationPointNode(dataBroker,
135                                                                    uniDestination.getAugmentation(UniAugmentation.class),
136                                                                    destinationBr,
137                                                                    UnimgrConstants.DEFAULT_BRIDGE_NAME,
138                                                                    UnimgrConstants.DEFAULT_TUNNEL_IFACE);
139                             UnimgrUtils.createGreTunnel(dataBroker,
140                                                         uniDestination.getAugmentation(UniAugmentation.class),
141                                                         uniSource.getAugmentation(UniAugmentation.class), destinationBr,
142                                                         UnimgrConstants.DEFAULT_BRIDGE_NAME,
143                                                         UnimgrConstants.DEFAULT_GRE_TUNNEL_NAME);
144                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.CONFIGURATION,
145                                                       evcKey,
146                                                       evc,
147                                                       sourceUniIid,
148                                                       destinationUniIid,
149                                                       dataBroker);
150                             UnimgrUtils.updateEvcNode(LogicalDatastoreType.OPERATIONAL,
151                                                       evcKey,
152                                                       evc,
153                                                       sourceUniIid,
154                                                       destinationUniIid,
155                                                       dataBroker);
156                         } else {
157                             LOG.info("Unable to retrieve the source and/or destination bridge.");
158                         }
159                     } else {
160                         LOG.info("Uname to retrieve the source and/or destination ovsdbNode.");
161                     }
162                 } else {
163                     LOG.info("Unable to retrieve the source and/or destination Uni.");
164                 }
165             }
166         }
167     }
168
169 }