blob: 5078b5dc3fa5318cef083205aeef0e1ea97afc19 [file] [log] [blame]
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080016package org.onosproject.net.intent.impl.phase;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080017
Brian O'Connorf0c5a052015-04-27 00:34:53 -070018import org.onosproject.net.intent.Intent;
Brian O'Connor0e271dc2015-02-04 18:20:25 -080019import org.onosproject.net.intent.IntentData;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080020import org.onosproject.net.intent.IntentException;
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080021import org.onosproject.net.intent.impl.IntentProcessor;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Brian O'Connorf0c5a052015-04-27 00:34:53 -070025import java.util.List;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080026import java.util.Optional;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
Sho SHIMIZUd3dcaa12015-02-13 13:48:51 -080030/**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070031 * Represents a phase where an intent is being compiled or recompiled.
Sho SHIMIZUd3dcaa12015-02-13 13:48:51 -080032 */
Brian O'Connorf0c5a052015-04-27 00:34:53 -070033class Compiling implements IntentProcessPhase {
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080034
35 private static final Logger log = LoggerFactory.getLogger(Compiling.class);
36
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080037 private final IntentProcessor processor;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080038 private final IntentData data;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070039 private final Optional<IntentData> stored;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080040
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041 /**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070042 * Creates a intent recompiling phase.
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043 *
Brian O'Connorf0c5a052015-04-27 00:34:53 -070044 * @param processor intent processor that does work for recompiling
45 * @param data intent data containing an intent to be recompiled
46 * @param stored intent data stored in the store
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047 */
Brian O'Connorf0c5a052015-04-27 00:34:53 -070048 Compiling(IntentProcessor processor, IntentData data, Optional<IntentData> stored) {
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080049 this.processor = checkNotNull(processor);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050 this.data = checkNotNull(data);
Brian O'Connorf0c5a052015-04-27 00:34:53 -070051 this.stored = checkNotNull(stored);
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080052 }
53
54 @Override
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080055 public Optional<IntentProcessPhase> execute() {
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080056 try {
Brian O'Connorf0c5a052015-04-27 00:34:53 -070057 List<Intent> compiled = processor.compile(data.intent(),
58 //TODO consider passing an optional here in the future
59 stored.isPresent() ? stored.get().installables() : null);
60 data.setInstallables(compiled);
61 return Optional.of(new Installing(processor, data, stored));
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080062 } catch (IntentException e) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 log.debug("Unable to compile intent {} due to: {}", data.intent(), e);
Brian O'Connorf0c5a052015-04-27 00:34:53 -070064 if (stored.isPresent() && !stored.get().installables().isEmpty()) {
65 // removing orphaned flows and deallocating resources
66 data.setInstallables(stored.get().installables());
67 return Optional.of(new Withdrawing(processor, data));
68 } else {
69 return Optional.of(new Failed(data));
70 }
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080071 }
72 }
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080073}