Introducing Bridge Operational State
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommand.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.ovsdb.southbound.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
25 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
26 import org.opendaylight.ovsdb.schema.openvswitch.Port;
27 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.collect.ImmutableMap;
41 import com.google.common.collect.Sets;
42
43 public class TerminationPointCreateCommand implements TransactCommand {
44     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes;
45     private BridgeOperationalState operationalState;
46     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
47
48     public TerminationPointCreateCommand(BridgeOperationalState state, AsyncDataChangeEvent<InstanceIdentifier<?>,
49             DataObject> changes) {
50         this.operationalState = state;
51         this.changes = changes;
52     }
53
54     @Override
55     public void execute(TransactionBuilder transaction) {
56         for (Entry<InstanceIdentifier<?>, DataObject> entry: changes.getCreatedData().entrySet()) {
57             DataObject dataObject = entry.getValue();
58             if (dataObject instanceof OvsdbTerminationPointAugmentation) {
59                 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
60                 LOG.debug("Received request to create termination point {}",
61                         terminationPoint.getName());
62
63
64                 // Configure interface
65                 String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
66                 Interface ovsInterface =
67                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
68                 ovsInterface.setName(terminationPoint.getName());
69                 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
70                 Long ofPort = terminationPoint.getOfport();
71                 if (ofPort != null) {
72                     ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
73                 }
74                 Integer ofPortRequest = terminationPoint.getOfportRequest();
75                 if (ofPortRequest != null) {
76                     ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
77                 }
78
79                 //Configure optional input
80                 if (terminationPoint.getOptions() != null) {
81                     HashMap<String, String> optionsMap = new HashMap<String, String>();
82                     for (Options option : terminationPoint.getOptions()) {
83                         optionsMap.put(option.getOption(), option.getValue());
84                     }
85                     try {
86                         ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
87                     } catch (NullPointerException e) {
88                         LOG.warn("Incomplete OVSDB interface options");
89                     }
90                 }
91
92                 List<InterfaceExternalIds> interfaceExternalIds =
93                         terminationPoint.getInterfaceExternalIds();
94                 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
95                     HashMap<String, String> externalIdsMap = new HashMap<String, String>();
96                     for (InterfaceExternalIds externalId: interfaceExternalIds) {
97                         externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
98                     }
99                     try {
100                         ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
101                     } catch (NullPointerException e) {
102                         LOG.warn("Incomplete OVSDB interface external_ids");
103                     }
104                 }
105                 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
106
107                 // Configure port with the above interface details
108                 String portUuid = "Port_" + SouthboundMapper.getRandomUUID();;
109                 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
110                 port.setName(terminationPoint.getName());
111                 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
112                 if (terminationPoint.getVlanTag() != null) {
113                     Set<Long> vlanTag = new HashSet<Long>();
114                     vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
115                     port.setTag(vlanTag);
116                 }
117                 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
118                     Set<Long> portTrunks = new HashSet<Long>();
119                     List<Trunks> modelTrunks = terminationPoint.getTrunks();
120                     for (Trunks trunk: modelTrunks) {
121                         if (trunk.getTrunk() != null) {
122                             portTrunks.add(trunk.getTrunk().getValue().longValue());
123                         }
124                     }
125                     port.setTrunks(portTrunks);
126                 }
127
128                 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
129                 if (portExternalIds != null && !portExternalIds.isEmpty()) {
130                     HashMap<String, String> externalIdsMap = new HashMap<String, String>();
131                     for (PortExternalIds externalId: portExternalIds) {
132                         externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
133                     }
134                     try {
135                         port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
136                     } catch (NullPointerException e) {
137                         LOG.warn("Incomplete OVSDB port external_ids");
138                     }
139                 }
140                 transaction.add(op.insert(port).withId(portUuid));
141
142                 //Configure bridge with the above port details
143                 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
144                 bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
145                 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
146
147                 transaction.add(op.mutate(bridge)
148                         .addMutation(bridge.getPortsColumn().getSchema(),
149                                 Mutator.INSERT,bridge.getPortsColumn().getData())
150                         .where(bridge.getNameColumn().getSchema()
151                                 .opEqual(bridge.getNameColumn().getData())).build());
152             }
153         }
154
155     }
156
157     private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
158         InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
159         Map<InstanceIdentifier<Node>, Node> nodes =
160                 TransactUtils.extractCreatedOrUpdated(changes,Node.class);
161         if (nodes != null && nodes.get(nodeIid) != null) {
162             Node node = nodes.get(nodeIid);
163             OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
164             if (bridge != null) {
165                 return bridge;
166             }
167         }
168         return null;
169     }
170
171 }