416a1be71a651e8dcbc51368076bdfc2ee97c497
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / ControllerUpdateCommand.java
1 /*
2  * Copyright (c) 2014 Cisco 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 com.google.common.base.Optional;
13 import com.google.common.collect.Sets;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.ovsdb.lib.notation.Mutator;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
24 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
25 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
26 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class ControllerUpdateCommand implements TransactCommand {
36     private static final Logger LOG = LoggerFactory.getLogger(ControllerUpdateCommand.class);
37
38     @Override
39     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
40             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events,
41             InstanceIdentifierCodec instanceIdentifierCodec) {
42         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, ControllerEntry.class),
43                 TransactUtils.extractCreatedOrUpdated(events, OvsdbBridgeAugmentation.class));
44     }
45
46     @Override
47     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
48             Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
49         execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, ControllerEntry.class),
50                 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbBridgeAugmentation.class));
51     }
52
53     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
54                          Map<InstanceIdentifier<ControllerEntry>, ControllerEntry> controllers,
55                          Map<InstanceIdentifier<OvsdbBridgeAugmentation>, OvsdbBridgeAugmentation> bridges) {
56         LOG.info("Register ODL controllers : {}  bridges detail : {}",
57                 controllers, bridges);
58         for (Entry<InstanceIdentifier<ControllerEntry>, ControllerEntry> entry: controllers.entrySet()) {
59             Optional<ControllerEntry> operationalControllerEntryOptional =
60                     state.getControllerEntry(entry.getKey());
61             if (!operationalControllerEntryOptional.isPresent()) {
62                 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
63                         entry.getKey().firstIdentifierOf(OvsdbBridgeAugmentation.class);
64                 Optional<OvsdbBridgeAugmentation> bridgeOptional =
65                         state.getOvsdbBridgeAugmentation(bridgeIid);
66                 OvsdbBridgeAugmentation ovsdbBridge = bridgeOptional.or(bridges.get(bridgeIid));
67                 if (ovsdbBridge != null
68                         && ovsdbBridge.getBridgeName() != null
69                         && entry.getValue() != null
70                         && entry.getValue().getTarget() != null) {
71                     ControllerEntry controllerEntry = entry.getValue();
72                     Controller controller =
73                             TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Controller.class);
74                     controller.setTarget(controllerEntry.getTarget().getValue());
75                     if (controllerEntry.getMaxBackoff() != null) {
76                         controller.setMaxBackoff(Sets.newHashSet(controllerEntry.getMaxBackoff()));
77                     }
78                     if (controllerEntry.getInactivityProbe() != null) {
79                         controller.setInactivityProbe(Sets.newHashSet(controllerEntry.getInactivityProbe()));
80                     }
81                     String controllerNamedUuidString = SouthboundMapper.getRandomUuid();
82                     UUID controllerNamedUuid = new UUID(controllerNamedUuidString);
83                     transaction.add(op.insert(controller).withId(controllerNamedUuidString));
84
85                     Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
86                     bridge.setName(ovsdbBridge.getBridgeName().getValue());
87                     bridge.setController(Sets.newHashSet(controllerNamedUuid));
88                     LOG.trace("Added controller : {} for bridge : {}",
89                             controller.getTargetColumn(), bridge.getName());
90                     transaction.add(op.mutate(bridge)
91                             .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
92                                     bridge.getControllerColumn().getData())
93                             .where(bridge.getNameColumn().getSchema().opEqual(bridge.getNameColumn().getData()))
94                             .build());
95                 }
96             }
97         }
98         LOG.trace("Executed transaction: {}", transaction.build());
99
100     }
101
102 }