propagate datastore exceptions all the way to northbound
[neutron.git] / neutron-hostconfig / utils / src / main / java / org / opendaylight / neutron / hostconfig / utils / NeutronHostconfigUtils.java
1 /*
2  * Copyright (c) 2017 Intel Corporation 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
9 package org.opendaylight.neutron.hostconfig.utils;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class NeutronHostconfigUtils {
24     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigUtils.class);
25     private final DataBroker dataBroker;
26
27     public enum Action {
28         ADD,
29         UPDATE,
30         DELETE
31     }
32
33     public NeutronHostconfigUtils(final DataBroker dataBroker) {
34         this.dataBroker = dataBroker;
35     }
36
37     public void updateMdsal(Hostconfig hostConfig, Action action) throws TransactionCommitFailedException {
38         InstanceIdentifier<Hostconfig> hostConfigId;
39         if (hostConfig == null) {
40             return;
41         }
42         switch (action) {
43             case ADD:
44             case UPDATE:
45                 final WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
46                 hostConfigId = createInstanceIdentifier(hostConfig);
47                 writeTx.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig, true);
48                 writeTx.submit().checkedGet();
49                 LOG.trace("Hostconfig updated for node {}", hostConfig.getHostId());
50                 break;
51             case DELETE:
52                 final WriteTransaction delTx = dataBroker.newWriteOnlyTransaction();
53                 hostConfigId = createInstanceIdentifier(hostConfig);
54                 delTx.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
55                 LOG.trace("Hostconfig deleted for node {}", hostConfig.getHostId());
56                 delTx.submit().checkedGet();
57                 break;
58             default:
59                 break;
60         }
61     }
62
63     public Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
64         HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
65         hostconfigBuilder.setHostId(hostId);
66         hostconfigBuilder.setHostType(hostType);
67         hostconfigBuilder.setConfig(hostConfig);
68         return hostconfigBuilder.build();
69     }
70
71     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
72         return InstanceIdentifier.create(Neutron.class).child(Hostconfigs.class)
73                 .child(Hostconfig.class, hostconfig.key());
74     }
75 }