2 * Copyright (c) 2017 Intel Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.hostconfig.utils;
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;
23 public class NeutronHostconfigUtils {
24 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigUtils.class);
25 private final DataBroker dataBroker;
33 public NeutronHostconfigUtils(final DataBroker dataBroker) {
34 this.dataBroker = dataBroker;
37 public void updateMdsal(Hostconfig hostConfig, Action action) throws TransactionCommitFailedException {
38 InstanceIdentifier<Hostconfig> hostConfigId;
39 if (hostConfig == null) {
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());
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();
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();
71 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
72 return InstanceIdentifier.create(Neutron.class).child(Hostconfigs.class)
73 .child(Hostconfig.class, hostconfig.key());