Merge dev/fluorine work across to master
[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.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.stream.Collectors;
15
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
17 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriver;
18 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriverBuilder;
19 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
20 import org.opendaylight.unimgr.mef.nrp.common.ResourceActivatorException;
21 import org.opendaylight.unimgr.mef.nrp.template.TemplateConstants;
22 import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev180321.NrpConnectivityServiceAttrs;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Example driver builder.
28  * @author bartosz.michalik@amartus.com
29  */
30 public class TemplateDriver implements ActivationDriverBuilder {
31     private static final Logger LOG = LoggerFactory.getLogger(TemplateDriver.class);
32
33     @Override
34     public Optional<ActivationDriver> driverFor(BuilderContext context) {
35         // build a stateful driver
36         // the API contract is following
37         // 1. initialize is called
38         // 2. activate or deactivate metod is called depending on NRP call (update will be supported soon)
39         // 3. if activation/deactivation succeeds for all drivers the commit metod is called
40         // 3a. if activation/deactivation fails for any driver rollback is called
41         return Optional.of(new ActivationDriver() {
42
43             public List<EndPoint> endpoints;
44             public String serviceId;
45
46             @Override
47             public void commit() {
48                 LOG.info("commit was triggered for {}", serviceId);
49             }
50
51             @Override
52             public void rollback() {
53                 LOG.info("rollback was triggered for {}", serviceId);
54             }
55
56             @Override
57             public void initialize(List<EndPoint> endPoints, String serviceId, NrpConnectivityServiceAttrs context) {
58                 this.serviceId = serviceId;
59                 this.endpoints = new ArrayList<>(endPoints);
60
61                 LOG.info("Driver initialized with: " + epsInfo());
62             }
63
64             @Override
65             public void activate() throws TransactionCommitFailedException, ResourceActivatorException {
66                 // method can fail if you wish
67                 LOG.info("activate was triggered for {}", serviceId);
68             }
69
70             @Override
71             public void deactivate() throws TransactionCommitFailedException, ResourceActivatorException {
72                 // method can fail if you wish
73                 LOG.info("dectivate was triggered for {}", serviceId);
74             }
75
76             @Override
77             public void update() throws TransactionCommitFailedException, ResourceActivatorException {
78
79             }
80
81             @Override
82             public int priority() {
83                 //if you would like to make your driver first on the list
84
85                 return 0;
86             }
87
88             private String epsInfo() {
89                 return endpoints.stream().map(e -> e.getNepRef().getNodeId().getValue() + ":"
90                         + e.getNepRef().getOwnedNodeEdgePointId().getValue())
91                         .collect(Collectors.joining(",", "[", "]"));
92             }
93         });
94     }
95
96     @Override
97     public String getActivationDriverId() {
98         return TemplateConstants.DRIVER_ID;
99     }
100 }