Fix vFWCL Heat template for instantiation with Robot
[demo.git] / vnfs / honeycomb_plugin / stream-count / stream-count-impl / src / main / java / io / fd / honeycomb / lcmapi / write / ElementCustomizer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Modifications copyright (c) 2019 AT&T Intellectual Property
19  */
20
21 package io.fd.honeycomb.lcmapi.write;
22
23 import io.fd.honeycomb.lcmapi.CrudService;
24 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27
28 import java.io.IOException;
29
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.stream.count.rev190118.stream.count.params.Streams;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Writer for {@link Element} list node from our YANG model.
39  */
40
41 public final class ElementCustomizer implements WriterCustomizer<Streams> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
44
45     private final CrudService<Streams> crudService;
46
47     public ElementCustomizer(@Nonnull final CrudService<Streams> crudService) {
48         this.crudService = crudService;
49     }
50
51     @Override
52     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id, @Nonnull final Streams dataAfter,
53                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
54         //perform write of data,or throw exception
55         //invoked by PUT operation,if provided data doesn't exist in Config data
56         crudService.writeData(id, dataAfter);
57         try {
58             runScript(dataAfter.getActiveStreams().getValue());
59         }
60         catch (IOException e) {
61             String message = "Write operation failed " + e;
62             LOG.error(message);
63         }
64     }
65
66     @Override
67     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
68                                         @Nonnull final Streams dataBefore,
69                                         @Nonnull final Streams dataAfter, @Nonnull final WriteContext writeContext)
70             throws WriteFailedException {
71         //invoked by PUT operation,if provided data does exist in Config data
72         crudService.updateData(id, dataBefore, dataAfter);
73         try {
74             runScript(dataAfter.getActiveStreams().getValue());
75         }
76         catch (IOException e) {
77             String message = "Write operation failed " + e;
78             LOG.error(message);
79         }
80     }
81
82     @Override
83     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
84                                         @Nonnull final Streams dataBefore,
85                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
86         //perform delete of data,or throw exception
87         // Not supported, throw exception
88         throw new WriteFailedException.DeleteFailedException(id,
89                 new UnsupportedOperationException("Delete operation not supported"));
90     }
91
92     //Update the number of running streams running a custom script that uses the old vPacketGen REST APIs
93     private void runScript(long streams) throws IOException {
94
95         String script = new String("bash /opt/update_running_streams.sh " + streams);
96         Runtime.getRuntime().exec(script);
97         String message = "Number of running streams updated to " + streams;
98         LOG.info(message);
99     }
100 }