Update UNI
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / command / UniUpdateCommand.java
1 /*
2  * Copyright (c) 2016 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.UnimgrMapper;
16 import org.opendaylight.unimgr.impl.UnimgrUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27
28 public class UniUpdateCommand extends AbstractUpdateCommand {
29
30     private static final Logger LOG = LoggerFactory.getLogger(UniUpdateCommand.class);
31
32     public UniUpdateCommand(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 (final Entry<InstanceIdentifier<?>, DataObject> updated : changes.entrySet()) {
41             if (updated.getValue() != null && updated.getValue() instanceof UniAugmentation) {
42                 final UniAugmentation uni = (UniAugmentation) updated.getValue();
43                 final InstanceIdentifier<?> uniIID = UnimgrMapper.getUniIid(dataBroker,
44                         uni.getIpAddress(), LogicalDatastoreType.OPERATIONAL);
45                 final UniAugmentation uniAug = UnimgrUtils.getUni(dataBroker, LogicalDatastoreType.OPERATIONAL, uni.getIpAddress());
46                 Preconditions.checkArgument(uniAug != null, "No UNI %s on OPERATION Topoligy",
47                         uni.getIpAddress().getIpv4Address().getValue());
48                 Preconditions.checkArgument(uniAug.getIpAddress().getIpv4Address().getValue().equals(
49                         uni.getIpAddress().getIpv4Address().getValue()), "New IpAddress %s should be the same than %s",
50                         uniAug.getIpAddress().getIpv4Address().getValue(),
51                         uni.getIpAddress().getIpv4Address().getValue());
52                 Node ovsdbNode;
53                 if (uni.getOvsdbNodeRef() != null) {
54                     final OvsdbNodeRef ovsdbNodeRef = uni.getOvsdbNodeRef();
55                     final Optional<Node> ovsdbNodeIID = UnimgrUtils.readNode(dataBroker,
56                             LogicalDatastoreType.OPERATIONAL, ovsdbNodeRef.getValue());
57                     if(ovsdbNodeIID.isPresent()){
58                         ovsdbNode= ovsdbNodeIID.get();
59                     } else {
60                         final Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
61                         if (optionalOvsdbNode.isPresent()) {
62                             ovsdbNode = optionalOvsdbNode.get();
63                         } else {
64                             ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker, uni);
65                         }
66                     }
67                     LOG.trace("UNI updated {}.", uni.getIpAddress().getIpv4Address());
68                 } else {
69                     final Optional<Node> optionalOvsdbNode = UnimgrUtils.findOvsdbNode(dataBroker, uni);
70                     if (optionalOvsdbNode.isPresent()) {
71                         ovsdbNode = optionalOvsdbNode.get();
72                     } else {
73                         ovsdbNode = UnimgrUtils.createOvsdbNode(dataBroker, uni);
74                     }
75                 }
76                 UnimgrUtils.deleteNode(dataBroker, uniIID, LogicalDatastoreType.OPERATIONAL);
77                 UnimgrUtils.updateUniNode(LogicalDatastoreType.OPERATIONAL, uniIID,
78                         uni, ovsdbNode, dataBroker);
79             }
80         }
81     }
82 }