Bump to yangtools-2.0.0 and odlparent-3.0.2
[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 java.util.concurrent.ExecutionException;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class NeutronHostconfigUtils {
25     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigUtils.class);
26     private final DataBroker dataBroker;
27
28     public enum Action {
29         ADD,
30         UPDATE,
31         DELETE
32     }
33
34     public NeutronHostconfigUtils(final DataBroker dataBroker) {
35         this.dataBroker = dataBroker;
36     }
37
38     public void updateMdsal(Hostconfig hostConfig, Action action) {
39         InstanceIdentifier<Hostconfig> hostConfigId;
40         if (hostConfig == null) {
41             return;
42         }
43         try {
44             switch (action) {
45                 case ADD:
46                 case UPDATE:
47                     final WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
48                     hostConfigId = createInstanceIdentifier(hostConfig);
49                     writeTx.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig, true);
50                     writeTx.submit().get();
51                     LOG.trace("Hostconfig updated for node {}", hostConfig.getHostId());
52                     break;
53                 case DELETE:
54                     final WriteTransaction delTx = dataBroker.newWriteOnlyTransaction();
55                     hostConfigId = createInstanceIdentifier(hostConfig);
56                     delTx.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
57                     LOG.trace("Hostconfig deleted for node {}", hostConfig.getHostId());
58                     delTx.submit().get();
59                     break;
60                 default:
61                     break;
62             }
63         } catch (InterruptedException | ExecutionException e) {
64             LOG.warn("Hostconfig transaction commit failed to DS.", e);
65         }
66     }
67
68     public Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
69         HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
70         hostconfigBuilder.setHostId(hostId);
71         hostconfigBuilder.setHostType(hostType);
72         hostconfigBuilder.setConfig(hostConfig);
73         return hostconfigBuilder.build();
74     }
75
76     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
77         return InstanceIdentifier.create(Neutron.class).child(Hostconfigs.class)
78                 .child(Hostconfig.class, hostconfig.getKey());
79     }
80 }