Merge "Add multiple options to port via MDSAL"
[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.Set;
15
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.ovsdb.lib.notation.Mutator;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
23 import org.opendaylight.ovsdb.schema.openvswitch.Port;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.Sets;
34
35 public class TerminationPointCreateCommand implements TransactCommand {
36     private AsyncDataChangeEvent<InstanceIdentifier<?>, OvsdbTerminationPointAugmentation> changes;
37     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
38
39     public TerminationPointCreateCommand(AsyncDataChangeEvent<InstanceIdentifier<?>,
40             OvsdbTerminationPointAugmentation> changes) {
41         this.changes = changes;
42     }
43
44     @Override
45     public void execute(TransactionBuilder transaction) {
46         for (OvsdbTerminationPointAugmentation terminationPoint: changes.getCreatedData().values()) {
47             LOG.debug("Received request to create termination point {} at managed node {}",
48                     terminationPoint.getName(),
49                     terminationPoint.getAttachedTo().getValue().firstIdentifierOf(Node.class));
50
51
52             // Configure interface
53             String interfaceUuid = "Interface_" + terminationPoint.getName();
54             Interface ovsInterface = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
55             ovsInterface.setName(terminationPoint.getName());
56             ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
57             Integer ofPort = terminationPoint.getOfport();
58             if (ofPort != null) {
59                 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
60             }
61             Integer ofPortRequest = terminationPoint.getOfportRequest();
62             if (ofPortRequest != null) {
63                 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest));
64             }
65
66             //Configure optional input
67             if (terminationPoint.getOptions() != null) {
68                 HashMap<String, String> optionsMap = new HashMap<String, String>();
69                 for (Options option : terminationPoint.getOptions()) {
70                     optionsMap.put(option.getOption(), option.getValue());
71                 }
72                 try {
73                     ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
74                 } catch (NullPointerException e) {
75                     LOG.warn("Incomplete OVSDB interface options");
76                 }
77             }
78             transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
79
80             // Configure port with the above interface details
81             String portUuid = "Port_" + terminationPoint.getName();
82             Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
83             port.setName(terminationPoint.getName());
84             port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
85             if (terminationPoint.getVlanTag() != null) {
86                 Set<Long> vlanTag = new HashSet<Long>();
87                 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
88                 port.setTag(vlanTag);
89             }
90             transaction.add(op.insert(port).withId(portUuid));
91
92             //Configure bridge with the above port details
93             Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
94             bridge.setName(terminationPoint.getBridgeName());
95             bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
96
97             transaction.add(op.mutate(bridge)
98                     .addMutation(bridge.getPortsColumn().getSchema(), Mutator.INSERT,bridge.getPortsColumn().getData())
99                     .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData())).build());
100         }
101
102     }
103
104 }