Migrate to YANG generated on 2017-07-12
[unimgr.git] / template-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / template / driver / TemplateDriver.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.unimgr.mef.nrp.template.driver;
10
11 import java.util.List;
12 import java.util.Optional;
13
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriver;
16 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriverBuilder;
17 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
18 import org.opendaylight.unimgr.mef.nrp.common.ResourceActivatorException;
19 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev170712.NrpConnectivityServiceAttrs;
20 import org.opendaylight.yang.gen.v1.urn.mef.yang.tapi.common.rev170712.Uuid;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Example driver builder
26  * @author bartosz.michalik@amartus.com
27  */
28 public class TemplateDriver implements ActivationDriverBuilder {
29     private static final Logger log = LoggerFactory.getLogger(TemplateDriver.class);
30     @Override
31     public Optional<ActivationDriver> driverFor(BuilderContext context) {
32         // build a stateful driver
33         // the API contract is following
34         // 1. initialize is called
35         // 2. activate or deactivate metod is called depending on NRP call (update will be supported soon)
36         // 3. if activation/deactivation succeeds for all drivers the commit metod is called
37         // 3a. if activation/deactivation fails for any driver rollback is called
38         return Optional.of(new ActivationDriver() {
39
40             public String serviceId;
41
42             @Override
43             public void commit() {
44                 log.info("commit was triggered for {}", serviceId);
45             }
46
47             @Override
48             public void rollback() {
49                 log.info("rollback was triggered for {}", serviceId);
50             }
51
52             @Override
53             public void initialize(List<EndPoint> endPoints, String serviceId, NrpConnectivityServiceAttrs context) {
54                 this.serviceId = serviceId;
55             }
56
57             @Override
58             public void activate() throws TransactionCommitFailedException, ResourceActivatorException {
59                 // method can fail if you wish
60                 log.info("activate was triggered for {}", serviceId);
61             }
62
63             @Override
64             public void deactivate() throws TransactionCommitFailedException, ResourceActivatorException {
65                 // method can fail if you wish
66                 log.info("adectivate was triggered for {}", serviceId);
67             }
68
69             @Override
70             public int priority() {
71                 //if you would like to make your driver first on the list
72
73                 return 0;
74             }
75         });
76     }
77
78     @Override
79     public Uuid getNodeUuid() {
80         return null;
81     }
82 }